我试图像这样将新行保存到数据库
@SuppressWarnings("unchecked")
@Modifying
public SMSIncoming save(@Param("smsIncoming") SMSIncoming smsIncoming);
在这里,我没有像下面那样设置ID:-
SMSIncoming smsIncoming = new SMSIncoming();
smsIncoming.setSender("6476963460");
smsIncoming.setReceiver("6475603720");
smsIncoming.setMsg("Testing Incoming messages");
@Id
@GeneratedValue
private Long id;
在保存之前应自动生成
@SuppressWarnings("unchecked")
@Modifying
public SMSIncoming save(@Param("smsIncoming") SMSIncoming smsIncoming);
JpaRepository<SMSIncoming, Long>
但是在这里我也遇到了很多异常,我尝试了所有无法通过的选项(AUTO,TABLE,SEQUENCE和IDENTITY),所以接下来我想从表中获取最大id并通过递增来保存。在这种方法中,我也面临着问题。
在使用IDENTITY关键字保存ID时发布问题跟踪:-
`2019-03-26 11:32:15.347 INFO 5828 --- [ main]
c.a.s.service.SmsPluginApplicationTests : --Application Started--
smsrec.toString():::::::com.avaal.sms.model.SMSIncoming@4c6bba7d
Hibernate: insert into dbo.ozekimessagein (id, msg, msgtype, operator,
receivedtime, receiver, reference, sender, senttime) values (null, ?, ?,
?, ?, ?, ?, ?, ?)
2019-03-26 11:32:16.029 WARN 5828 --- [ main]
o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 339, SQLState: S0001
2019-03-26 11:32:16.029 ERROR 5828 --- [ main]
o.h.engine.jdbc.spi.SqlExceptionHelper : DEFAULT or NULL are not allowed
as explicit identity values.
Inside test catch block::::::::::
org.springframework.dao.InvalidDataAccessResourceUsageException: could
not execute statement; SQL [n/a]; nested exception is
org.hibernate.exception.SQLGrammarException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:279)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
在
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
在使用ID中的SEQUENCE或AUTO关键字保存时发出问题跟踪:-
`@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE/AUTO)
private long id;`
smsrec.toString():::::::com.avaal.sms.model.SMSIncoming@5d717f19
Hibernate: call next value for hibernate_sequence
2019-03-26 12:29:06.493 WARN 15412 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 102, SQLState: S0001
2019-03-26 12:29:06.494 ERROR 15412 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Incorrect syntax near 'value'.
Inside test catch block::::::::::
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:279)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)
使用ID为TABLE的关键字保存时发出的跟踪:-
`@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private long id;`
`smsrec.toString():::::::com.avaal.sms.model.SMSIncoming@69e2fe3b
Hibernate: select tbl.next_val from hibernate_sequences tbl where
tbl.sequence_name=? for update
2019-03-26 12:33:33.157 ERROR 15896 --- [main]
o.hibernate.id.enhanced.TableGenerator : HHH000351: Could not read or
init a hi value com.microsoft.sqlserver.jdbc.SQLServerException: Line 1: FOR UPDATE clause allowed only for DECLARE CURSOR. at
com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult()上的
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:259)〜[mssql-jdbc-6.4.0.jre8.jar:na] SQLServerStatement.java:1547)〜[mssql-jdbc-6.4.0.jre8.jar:na]在com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:548)〜[mssql-jdbc-6.4.0 .jre8.jar:na]`