ImprovedNamingStrategy不再适用于Hibernate 5 [英] ImprovedNamingStrategy no longer working in Hibernate 5

查看:855
本文介绍了ImprovedNamingStrategy不再适用于Hibernate 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的spring-jpa配置,我配置了Hibernate的 ImprovedNamingStrategy 。这意味着如果我的实体类有一个变量 userName ,那么Hibernate应该将它转换为用于查询数据库的 user_name 。但是这个命名转换在我升级到Hibernate 5后停止了工作。我收到了错误:
$ b


错误:未知列'user0_.userName'在'字段列表'中


这是我的Hibernate配置:

<$ p $
$ @ b
@EnableJpaRepositories(com.springJpa.repository)
@EnableTransactionManagement
公共类DataConfig {

@Bean
public DataSource dataSource(){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(com.mysql.jdbc.Driver);
ds.setUrl(jdbc:mysql:// localhost:3306 / test);
ds.setUsername(root);
ds.setPassword(admin);
return ds;




public LocalContainerEntityManagerFactoryBean entityManagerFactory(){

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(Boolean.TRUE);
vendorAdapter.setDatabase(Database.MYSQL);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setDataSource(dataSource());
factory.setPackagesToScan(com.springJpa.entity);


属性jpaProperties = new Properties();

jpaProperties.put(hibernate.ejb.naming_strategy,org.hibernate.cfg.ImprovedNamingStrategy);
jpaProperties.put(hibernate.dialect,org.hibernate.dialect.MySQL5InnoDBDialect);

factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
退货工厂;


$Be $ b $ public SharedEntityManagerBean entityManager(){
SharedEntityManagerBean entityManager = new SharedEntityManagerBean();
entityManager.setEntityManagerFactory(entityManagerFactory()。getObject());
返回entityManager;



$ b @Bean
public PlatformTransactionManager transactionManager(){
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory()。getObject());
返回txManager;
}

@Bean
public ImprovedNamingStrategy namingStrategy(){
return new ImprovedNamingStrategy();




$ b $ p $这是我的实体类:

  @Getter 
@Setter
@Entity
@Table(name =user)
公共类用户{

@Id
@GeneratedValue
私人长ID;

private String userName;
私人字符串电子邮件;
私人字符串密码;
私人字符串角色;


$ / code>

我不想在数据库中明确地命名我的数据库字段@Column注释。我希望我的配置可以隐式地将camel case转换为下划线。



请指导

解决方案

刚刚发现了这个问题,使用Hibernate版本< 5.0但不适用于Hibernate> = 5.0。



我在Spring 4.2.0.RELEASE中使用Hibernate 5.0.0.Final。我猜Hibernate 5与Spring 4.2并不完全兼容。我只是将Hibernate降级到4.2.1.Final,并开始正常工作。



Hibernate的 NamingStrategy 类在Hibernate 5中被弃用。


I have simple spring-jpa configuration where I have configured Hibernate's ImprovedNamingStrategy. This means if my entity class has a variable userName, then Hibernate should convert it to user_name for querying the database. But this naming conversion stopped working after I upgraded to Hibernate 5. I am getting the error:

ERROR: Unknown column 'user0_.userName' in 'field list'

This is my Hibernate config:

@Configuration
@EnableJpaRepositories("com.springJpa.repository")
@EnableTransactionManagement
public class DataConfig {

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/test");
        ds.setUsername("root");
        ds.setPassword("admin");
        return ds;
    }


    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){ 

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(Boolean.TRUE);
        vendorAdapter.setDatabase(Database.MYSQL);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setDataSource(dataSource());
        factory.setPackagesToScan("com.springJpa.entity");


        Properties jpaProperties = new Properties();

        jpaProperties.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy");
        jpaProperties.put("hibernate.dialect","org.hibernate.dialect.MySQL5InnoDBDialect");

        factory.setJpaProperties(jpaProperties);
        factory.afterPropertiesSet();
        return factory;
    }

    @Bean
    public SharedEntityManagerBean entityManager() {
        SharedEntityManagerBean entityManager = new SharedEntityManagerBean();
        entityManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return entityManager;
    }



    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return txManager;
    }

    @Bean
    public ImprovedNamingStrategy namingStrategy(){
        return new ImprovedNamingStrategy();
    }
}

This is my Entity class:

@Getter
@Setter
@Entity
@Table(name="user")
public class User{

    @Id
    @GeneratedValue
    private Long id;

    private String userName;
    private String email;
    private String password;
    private String role;

}

I don't want to explicitly name my database fields within the @Column annotations. I want my configuration which can implicitly convert camel case to underscore.

Please guide.

解决方案

Just figured out the issue, the configuration is absolutely fine when using a Hibernate version < 5.0 but not for Hibernate >= 5.0.

I was using Hibernate 5.0.0.Final with Spring 4.2.0.RELEASE. I guess the Hibernate 5 is not fully compatible with Spring 4.2 . I just downgraded Hibernate to 4.2.1.Final and things started working fine.

Hibernate's NamingStrategy class is deprecated in Hibernate 5.

这篇关于ImprovedNamingStrategy不再适用于Hibernate 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆