尝试使用JPA序列生成器设置id:
@Entity
@Table(name=CommitmentRegisterDetailTable.TABLE)
public class TestCrd {
@Id
@Column(name=CommitmentRegisterDetailTable.COMMIT_REG_DETAIL_ID)
@SequenceGenerator(name="CRD_ID", sequenceName="COMMIT_REG_DETAIL_ID_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CRD_ID")
private int id;
@Column(name=CommitmentRegisterDetailTable.COMMIT_REG_ID)
private int commitRegId = 89915;
}
通过这个实体,我可以加载并合并。但是当我执行persist()时,SQL输出显示它尝试执行id = 0的INSERT。我错过了一个步骤吗?
这是一个Oracle DB。
我坚持的代码:
TestCrd testCrd = new TestCrd();
EntityManagerFactory factory = Persistence.createEntityManagerFactory( "incepPersistence" );
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
em.persist( testCrd );
em.getTransaction().commit();
em.close();
factory.close();
序列详情:
CREATED 07-JUN-16
LAST_DDL_TIME 07-JUN-16
SEQUENCE_OWNER RMS
SEQUENCE_NAME COMMIT_REG_DETAIL_ID_SEQ
MIN_VALUE 0
MAX_VALUE 999999999999999999999999999
INCREMENT_BY 1
CYCLE_FLAG N
ORDER_FLAG N
CACHE_SIZE 0
LAST_NUMBER 107568
这是发给DB的电话。如您所见,第一个参数COMMIT_REG_DETAIL_ID被赋值为0。
Call: INSERT INTO RMS.COMMITMENT_REGISTER_DETAIL (COMMIT_REG_DETAIL_ID, AMOUNT, COMMITTED, LINE_DESCRIPTION, DISBURSED, INVOICED, LINE_NUMBER, OBLIGATED, RECEIVED, ACCOUNT_COMBO_ID, COMMIT_REG_ID, OBJECT_CLASS_CODE, SP_DETAIL_VALUE_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [0, 5127, null, null, null, null, 0, null, null, 2097, 89843, 31510, null]
Query: InsertObjectQuery(com.incep.commitmentregister.CommitmentRegisterDetail@2ee77e0f)
这是完整的JPA日志设置为FINEST:
[EL Finest]: jpa: 2016-07-06 13:56:00.506--ServerSession(786709615)--Thread(Thread[main,5,main])--Begin predeploying Persistence Unit incepPersistence; session file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/_incepPersistence; state Initial; factoryCount 0
[EL Finest]: properties: 2016-07-06 13:56:00.535--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.orm.throw.exceptions; default value=true
[EL Finest]: properties: 2016-07-06 13:56:00.536--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.weaving.changetracking; default value=true
[EL Finest]: properties: 2016-07-06 13:56:00.536--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.weaving.lazy; default value=true
[EL Finest]: properties: 2016-07-06 13:56:00.536--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.weaving.eager; default value=false
[EL Finest]: properties: 2016-07-06 13:56:00.537--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.weaving.fetchgroups; default value=true
[EL Finest]: properties: 2016-07-06 13:56:00.537--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.weaving.internal; default value=true
[EL Finest]: properties: 2016-07-06 13:56:00.539--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.multitenant.tenants-share-emf; default value=true
[EL Finest]: properties: 2016-07-06 13:56:00.539--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.multitenant.tenants-share-cache; default value=false
[EL Finer]: metadata: 2016-07-06 13:56:00.573--ServerSession(786709615)--Thread(Thread[main,5,main])--Searching for default mapping file in file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/ (There is no English translation for this message.)
[EL Finer]: metadata: 2016-07-06 13:56:00.976--ServerSession(786709615)--Thread(Thread[main,5,main])--Found a default mapping file at file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/META-INF/orm.xml for root URL file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/ (There is no English translation for this message.)
[EL Finer]: metadata: 2016-07-06 13:56:02.326--ServerSession(786709615)--Thread(Thread[main,5,main])--Searching for default mapping file in file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/ (There is no English translation for this message.)
[EL Config]: metadata: 2016-07-06 13:56:02.775--ServerSession(786709615)--Thread(Thread[main,5,main])--The access type for the persistent class [class com.incep.test.commitmentregister.TestCrd] is set to [FIELD].
[EL Config]: metadata: 2016-07-06 13:56:02.815--ServerSession(786709615)--Thread(Thread[main,5,main])--The alias name for the entity class [class com.incep.test.commitmentregister.TestCrd] is being defaulted to: TestCrd.
[EL Finer]: weaver: 2016-07-06 13:56:02.889--ServerSession(786709615)--Thread(Thread[main,5,main])--Class [com.incep.test.commitmentregister.TestCrd] registered to be processed by weaver.
[EL Finest]: jpa: 2016-07-06 13:56:02.898--ServerSession(786709615)--Thread(Thread[main,5,main])--End predeploying Persistence Unit incepPersistence; session file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/_incepPersistence; state Predeployed; factoryCount 0
[EL Finer]: weaver: 2016-07-06 13:56:02.899--Thread(Thread[main,5,main])--JavaSECMPInitializer - transformer is null.
[EL Finest]: jpa: 2016-07-06 13:56:02.9--ServerSession(786709615)--Thread(Thread[main,5,main])--Begin predeploying Persistence Unit incepPersistence; session file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/_incepPersistence; state Predeployed; factoryCount 0
[EL Finest]: jpa: 2016-07-06 13:56:02.901--ServerSession(786709615)--Thread(Thread[main,5,main])--End predeploying Persistence Unit incepPersistence; session file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/_incepPersistence; state Predeployed; factoryCount 1
[EL Finest]: jpa: 2016-07-06 13:56:22.435--ServerSession(786709615)--Thread(Thread[main,5,main])--Begin deploying Persistence Unit incepPersistence; session file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/_incepPersistence; state Predeployed; factoryCount 1
[EL Finer]: 2016-07-06 13:56:22.453--ServerSession(786709615)--Thread(Thread[main,5,main])--Could not initialize Validation Factory. Encountered following exception: java.lang.NoClassDefFoundError: javax/validation/Validation
[EL Finest]: properties: 2016-07-06 13:56:22.481--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.logging.logger; value=DefaultLogger; translated value=org.eclipse.persistence.logging.DefaultSessionLog
[EL Finest]: properties: 2016-07-06 13:56:22.482--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.logging.level; value=FINEST; translated value=FINEST
[EL Finest]: properties: 2016-07-06 13:56:22.483--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.logging.level; value=FINEST; translated value=FINEST
[EL Finest]: properties: 2016-07-06 13:56:22.484--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.target-database; value=org.eclipse.persistence.platform.database.oracle.OraclePlatform
[EL Finest]: properties: 2016-07-06 13:56:22.493--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.id-validation; value=NULL
[EL Finest]: properties: 2016-07-06 13:56:22.493--ServerSession(786709615)--Thread(Thread[main,5,main])--property=eclipselink.session.customizer; value=com.apprio.acquisition.JPAEclipseLinkSessionCustomizer
_JPAEclipseLinkSessionCustomizer: configured java:comp/env/jdbc/OracleRMS_DS
[EL Info]: 2016-07-06 13:56:22.496--ServerSession(786709615)--Thread(Thread[main,5,main])--EclipseLink, version: Eclipse Persistence Services - 2.4.1.v20121003-ad44345
[EL Config]: connection: 2016-07-06 13:56:22.509--ServerSession(786709615)--Connection(1167290948)--Thread(Thread[main,5,main])--connecting(DatabaseLogin(
platform=>OraclePlatform
user name=> ""
connector=>JNDIConnector datasource name=>java:comp/env/jdbc/OracleRMS_DS
))
[EL Config]: connection: 2016-07-06 13:56:23.129--ServerSession(786709615)--Connection(452044444)--Thread(Thread[main,5,main])--Connected: jdbc:oracle:thin:@localhost:1521:INCEPDEV
User: RMS
Database: Oracle Version: Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
Driver: Oracle JDBC driver Version: 11.2.0.2.0
[EL Finest]: connection: 2016-07-06 13:56:23.143--ServerSession(786709615)--Connection(839511213)--Thread(Thread[main,5,main])--Connection acquired from connection pool [read].
[EL Finest]: connection: 2016-07-06 13:56:23.144--ServerSession(786709615)--Connection(839511213)--Thread(Thread[main,5,main])--Connection released to connection pool [read].
[EL Config]: connection: 2016-07-06 13:56:23.144--ServerSession(786709615)--Connection(697396101)--Thread(Thread[main,5,main])--connecting(DatabaseLogin(
platform=>OraclePlatform
user name=> ""
connector=>JNDIConnector datasource name=>java:comp/env/jdbc/OracleRMS_DS
))
[EL Config]: connection: 2016-07-06 13:56:23.31--ServerSession(786709615)--Connection(170524181)--Thread(Thread[main,5,main])--Connected: jdbc:oracle:thin:@localhost:1521:INCEPDEV
User: RMS
Database: Oracle Version: Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
Driver: Oracle JDBC driver Version: 11.2.0.2.0
[EL Finest]: sequencing: 2016-07-06 13:56:23.327--ServerSession(786709615)--Thread(Thread[main,5,main])--sequencing connected, state is Preallocation_NoTransaction_State
[EL Finest]: sequencing: 2016-07-06 13:56:23.327--ServerSession(786709615)--Thread(Thread[main,5,main])--sequence COMMIT_REG_DETAIL_ID_SEQ: preallocation size 1
[EL Info]: connection: 2016-07-06 13:56:23.365--ServerSession(786709615)--Thread(Thread[main,5,main])--file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/_incepPersistence login successful
[EL Finer]: metamodel: 2016-07-06 13:56:23.441--ServerSession(786709615)--Thread(Thread[main,5,main])--Canonical Metamodel class [com.incep.test.commitmentregister.TestCrd_] not found during initialization.
[EL Finest]: jpa: 2016-07-06 13:56:23.442--ServerSession(786709615)--Thread(Thread[main,5,main])--End deploying Persistence Unit incepPersistence; session file:/D:/Development/Workspaces/InCEP/incep-aspr-aqc-local/codeCharge/WEB-INF/classes/_incepPersistence; state Deployed; factoryCount 1
[EL Finer]: connection: 2016-07-06 13:56:29.205--ServerSession(786709615)--Thread(Thread[main,5,main])--client acquired: 1882760631
[EL Finer]: transaction: 2016-07-06 13:56:29.235--ClientSession(1882760631)--Thread(Thread[main,5,main])--acquire unit of work: 102009560
[EL Finest]: transaction: 2016-07-06 13:56:30.565--UnitOfWork(102009560)--Thread(Thread[main,5,main])--persist() operation called on: com.incep.test.commitmentregister.TestCrd@3c07590f.
[EL Finer]: transaction: 2016-07-06 13:56:33.25--UnitOfWork(102009560)--Thread(Thread[main,5,main])--begin unit of work commit
[EL Finest]: query: 2016-07-06 13:56:33.261--UnitOfWork(102009560)--Thread(Thread[main,5,main])--Execute query InsertObjectQuery(com.incep.test.commitmentregister.TestCrd@3c07590f)
[EL Finest]: connection: 2016-07-06 13:56:33.267--ServerSession(786709615)--Connection(13402762)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].
[EL Finer]: transaction: 2016-07-06 13:56:33.267--ClientSession(1882760631)--Connection(13402762)--Thread(Thread[main,5,main])--begin transaction
[EL Finest]: connection: 2016-07-06 13:56:33.268--ClientSession(1882760631)--Thread(Thread[main,5,main])--reconnecting to external connection pool
[EL Fine]: sql: 2016-07-06 13:56:33.557--ClientSession(1882760631)--Connection(1921921646)--Thread(Thread[main,5,main])--INSERT INTO RMS.COMMITMENT_REGISTER_DETAIL (COMMIT_REG_DETAIL_ID, COMMIT_REG_ID) VALUES (?, ?)
bind => [0, 89915]
[EL Fine]: sql: 2016-07-06 13:56:33.9--ClientSession(1882760631)--Thread(Thread[main,5,main])--SELECT 1 FROM DUAL
[EL Warning]: 2016-07-06 13:56:33.927--UnitOfWork(102009560)--Thread(Thread[main,5,main])--Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (RMS.COMMITMENT_REGISTER_DETAIL_PK) violated
Error Code: 1
Call: INSERT INTO RMS.COMMITMENT_REGISTER_DETAIL (COMMIT_REG_DETAIL_ID, COMMIT_REG_ID) VALUES (?, ?)
bind => [0, 89915]
Query: InsertObjectQuery(com.incep.test.commitmentregister.TestCrd@3c07590f)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:851)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:913)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:594)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:537)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1800)
at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:286)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:207)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:342)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:162)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:177)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:471)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:286)
at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:852)
at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:751)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2875)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1602)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1584)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1535)
at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:224)
at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:123)
at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:3914)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1419)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:634)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1509)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:266)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1147)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:84)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
at com.incep.test.commitmentregister.TestCommitmentRegisterManager.testLoadSave(TestCommitmentRegisterManager.java:729)
at com.incep.test.commitmentregister.TestCommitmentRegisterManager.testAll(TestCommitmentRegisterManager.java:73)
at com.incep.test.TestMaster.testAll(TestMaster.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (RMS.COMMITMENT_REGISTER_DETAIL_PK) violated
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1010)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1315)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3657)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1350)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:842)
... 59 more
[EL Finer]: transaction: 2016-07-06 13:56:33.942--ClientSession(1882760631)--Connection(1921921646)--Thread(Thread[main,5,main])--rollback transaction
[EL Finest]: connection: 2016-07-06 13:56:33.966--ServerSession(786709615)--Connection(13402762)--Thread(Thread[main,5,main])--Connection released to connection pool [default].
[EL Finer]: transaction: 2016-07-06 13:56:33.967--UnitOfWork(102009560)--Thread(Thread[main,5,main])--release unit of work
[EL Finer]: connection: 2016-07-06 13:56:33.967--ClientSession(1882760631)--Thread(Thread[main,5,main])--client released
答案 0 :(得分:0)
Seems like I found my problem. Removing the ID validation property from persistence.xml appears to make the sequence work properly.
<property name="eclipselink.id-validation" value="NULL" />
Further update:
The way I ultimately managed to fix my problem is to leave the id validation as above, set to NULL. Meaning IDs with 0 do not throw error, but do not trigger the fetching of a new sequence on persist.
So I changed the id on my object from an int to an Integer object. When id is null, a new sequence id is fetched. When id is Integer(0), no error is thrown.
答案 1 :(得分:0)
两个案例。
(1)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int idx;
Auto_increment,序列对象 - &gt; strategy = GenerationType.AUTO
(2) 你的情况。
元素明细
public abstract String name (必需)唯一的生成器名称 可以被一个或多个类引用为生成器 主键值。
public abstract String sequenceName (可选)的名称 数据库序列对象,从中获取主键值。 默认为提供者选择的值。默认值:hibernate_sequence
public abstract int initialValue (可选)从中获取的值 sequence对象是开始生成的。默认值:1
public abstract int allocationSize (可选)要增加的金额 当从序列中分配序列号时。默认值:50
DDL
create sequence RTDS_ADSINPUT_SEQ start with 1 increment by 1;
实体
@Entity
@SequenceGenerator(
name = "CRD_ID",
sequenceName = "RTDS_ADSINPUT_SEQ"
initiaValue = 1, allocationSize = 1)
public class XXX {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CRD_ID")
private long id;
//getter, setter
}