碰到的问题
在使用jpa进行添加操作的时候报错
2019-05-21 20:44:04.992 ERROR 13460 --- [ctor-http-nio-3] o.h.engine.jdbc.spi.SqlExceptionHelper : Table 'national_marketing.hibernate_sequence' doesn't exist
277
2019-05-21 20:44:05.044 ERROR 13460 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [a8da1078] 500 Server Error for HTTP PUT "/admin/admin_user/add_admin_user"
org.springframework.dao.InvalidDataAccessResourceUsageException: error performing isolated work; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: error performing isolated work
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:279) ~[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253) ~[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527) ~[spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) ~[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153) ~[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
原因
在使用Jpa对数据库进行操作是时,设置的自增字段在进行插入操作时也必须set,否则会报错添加失败。
通过使用 @GeneratedValue
注解能实现自增字段自动添加。
但是只使用 @GeneratedValue
注解在注解中不传入参数,会报错 error performing isolated work
解决办法
给 @GeneratedValue
注解中添加参数:@GeneratedValue(strategy = GenerationType.IDENTITY)
这样即可正常添加成功