oracle数据库转mysql数据库,导致@GeneratedValue注解报错Could not instantiate id generator



错误描述

报错内容

Caused by: org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.gp.identity.domain.RoleAgmAttribute]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:123)
    at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:195)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:314)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 72 more
Caused by: org.hibernate.MappingException: org.hibernate.dialect.MySQLDialect does not support sequences
    at org.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:807)
    at org.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:110)
    at org.hibernate.id.SequenceHiLoGenerator.configure(SequenceHiLoGenerator.java:55)
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:117)
    ... 81 more

项目之前原本连接的为oracle数据库,现在需要更改连接mysql数据库,因为数据库更换基本可以定位是主键生成策略的问题

解决方法

@GeneratedValue 用于标注主键的生成策略,通过strategy 属性指定。默认情况下,JPA 自动选择一个最适合底层数据库的主键生成策略:Oracle对应SEQUENCE
在javax.persistence.GenerationType中定义了以下几种可供选择的策略:
–IDENTITY:主键由数据库生成, 采用数据库自增长, Oracle不支持这种方式
–AUTO: JPA自动选择合适的策略,是默认选项
–SEQUENCE:通过数据库的序列产生主键,MySql不支持这种方式
–TABLE:通过表产生主键,框架借由表模拟序列产生主键,使用该策略可以使应用更易于数据库移植

结论:将strategy 属性的SEQUENCE修改为IDENTITY即可

声明:Wayen|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - oracle数据库转mysql数据库,导致@GeneratedValue注解报错Could not instantiate id generator


-当你感到最无助困难的时候,那就是离成功最近的时候。-Martin Frohm