Spring Boot Sping Boot 的Hibernate字段命名问题(命名策略)

dzjeubhm  于 2023-04-30  发布在  Spring
关注(0)|答案(7)|浏览(143)

请注意,此代码可以与普通Spring一起使用,但不能与Sping Boot (v1.3.3),是不是有什么我错过了,因为这是从一个Spring的应用程序,工程导入.下面的代码来自Sping Boot 应用程序

@Entity
@Table(name="project")
public class Project implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private int id;

    @Column(name="teamId")
    private int teamId;

    //private String Rentabiliteit;

    @Column
    //@Index(name="IProject_status",columnNames="Status")
    private String status;

    @Column
    //@Index(name="IProject_naam",columnNames="Naam")
    private String naam;
    //public Prototype m_Prototype;
    //public Team m_Team;

}

SQL

CREATE TABLE IF NOT EXISTS `project` (
`id` int(11) NOT NULL,
`teamId` int(11) DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`naam` varchar(255) DEFAULT NULL
 ) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=latin1;

错误

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:           
 Unknown column 'project0_.team_id' in 'field list'

编辑:应用程序。YML

spring:

mvc:
  view:
    prefix: /WEB-INF/jsp/
    suffix: .jsp

datasource:
    url: jdbc:mysql://localhost:3306/oxyplast
    username: oxyplastuser
    password: oxyplastuserpw

jpa:
  properties:
    hibernate:
      current_session_context_class: org.springframework.orm.hibernate4.SpringSessionContext 
      namingStrategy: org.hibernate.cfg.DefaultNamingStrategy
yrwegjxp

yrwegjxp1#

自Spring Boot1.***

从1开始。4,由于切换到Hibernate 5,命名策略已更新为SpringPhysicalNamingStrategy,应该非常接近1。3个默认。

参见:

Sping Boot 提供ImprovedNamingStrategy作为默认命名策略,这使得Hibernate搜索team_id列(从int teamId字段推断)。由于表中不存在此列,因此这就是错误的原因。Hibernate文档:
改进的命名策略,更喜欢嵌入下划线而不是混合大小写的名称
你有两个选择
1.显式提供列名@Column(name="teamId")。在早期的 Boot 版本中曾经有一个bug,现在没有了。
1.更改Sping Boot 属性中的命名策略,告诉它使用EJB3NamingStrategy,它不会将camelCase转换为snake_case,而是保持原样。

vq8itlhq

vq8itlhq2#

如果你使用Sping Boot 2。0.2和Hibernate 5。3.4然后设置以下属性将修复此问题。

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
ac1kyiln

ac1kyiln3#

下面的策略对我很有效

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultComponentSafeNamingStrategy
xxls0lw8

xxls0lw84#

最新版本:

spring-boot-starter-data-jpa: ➡ 1.5.2.RELEASE
    hibernate-core:5.0.12.Final

本课

PhysicalNamingStrategyStandardImpl

需要扩展并添加到hibernate属性中。
这里是一个完整的工作版本

public class PhysicalNamingStrategyImpl extends PhysicalNamingStrategyStandardImpl implements Serializable {

    public static final PhysicalNamingStrategyImpl INSTANCE = new PhysicalNamingStrategyImpl();

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        String nameModified;
        // Do whatever you want with the name modification
        return new Identifier(nameModified, name.isQuoted());
    }

}

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
         String nameModified;
        // Do whatever you want with the name modification
        return new Identifier(nameModified, name.isQuoted());
    }

在配置数据源时,应该像这样将它链接到hibernate。

properties.put("hibernate.physical_naming_strategy", "my.Package.PhysicalNamingStrategyImpl");

下面是datasource config完整工作版本

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactory",
        basePackages = { "com.xxxxxx.repository" }
)
public class SharedDataSourceConfig {

    @Value("${startup.ddl-auto}")
    String hbm2ddl;

    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties("spring.datasource.shared")
    public DataSource customerDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource) {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", hbm2ddl);
        properties.put("hibernate.physical_naming_strategy", "my.package.PhysicalNamingStrategyImpl");
        return builder
                .dataSource(dataSource)
                .packages(PackageScannerHelper.getPackagesToScan())
                .persistenceUnit("shared")
                .properties(properties)
                .build();
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory") EntityManagerFactory
                    entityManagerFactory
    ) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}
mctunoxg

mctunoxg5#

这对我来说是一个Spring Boot 开始。4.0和hibernate entitymanager 4.3.8.Final

应用程序。属性

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.物理命名策略标准实施

g9icjywg

g9icjywg6#

application.properties

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultComponentSafeNamingStrategy

上述属性为我工作。休眠4.3.11.最终 Spring Boot 1.4.2.RELEASE

c9x0cxw0

c9x0cxw07#

Spring Boot 3

spring:
  jpa:
    open-in-view: false
    show-sql: true
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy
        implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
  • 物理策略:org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy
  • 隐式策略:org.springframework.boot.orm.jpa.Hibernate.SpringImplicitNamingStrategy

相关问题