Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

java - JPA save failing and transaction rolling back

I am trying to get a record from db and comparing with dto for auditing purpose. If i found a record, I am updating record with updated field and setting the primary key id to null and trying to save it as a new record. If there is no record in db, i am conveting the dto to entity and just saving it.

List<AuditReconciliation> auditiedList = reconDao.getAuditedReconActionDtlList(Arrays.asList(entity.getReconActionDtlId()));
    AuditReconciliation auditRecon = !auditiedList.isEmpty() ?auditiedList.get(0) : new AuditReconciliation();
    if(!entity.getDisposition().equals(auditRecon.getDisposition())) {
    auditRecon.setLastEditedDate(entity.getLastEditedDate()!= null? entity.getLastEditedDate():presentDate);
        auditRecon.setReconActionDtlAuditId(null);
        return auditRecon;

while saving i am getting org.hibernate.HibernateException: identifier of an instance was altered from 1 to null 2021-02-04 21:02:27.983 ERROR 14048 --- [io-8080-exec-24] o.s.t.i.TransactionInterceptor : Application exception overridden by commit exception. Any help would be appreciated.

question from:https://stackoverflow.com/questions/66057176/jpa-save-failing-and-transaction-rolling-back

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As per your explanation, setting null to the primary will not insert the new record, also it will not update null to the primary key so resulting in the error, as an object is tied with persistence context

What should be your solution
Scenario 1: If I found a record

Solution 1: Try to update the same record with new fields without setting null to the primary key.
Solution 2: Delete an existing record and insert the new one.

Scenario 2: If no record found

Solution: Insert new record directly as you mentioned.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...