We have a before insert trigger that gets the next value from the sequence. When an object is saved using the save () method, hibernate gets the value from the sequence and adds it to the object. and when the transaction is completed from the Spring service level, the ID value again increases in the database. how can I avoid getting nextval () if the object already has an identifier.
Here is what I am trying to do.
Userdao
public User saveUser(User user){ session.getCurrentSession.save(user);
User service
public void saveUserAndWriteToAudit(User user, UserAudit userAudit){ userDao.saveUser(user);
And user class
@Entity public class User{ @Id @GeneratedValue(strategy=GenerationType.AUTO, generator="a1") @SequenceGenerator(name="a1", sequenceName="usersequence") private Long id;
When the cursor reaches the line1 and line2 objects, the user object is null in the id attribute. after row2, it has the next number from the sequence - say 1. on row4, I added the user ID = 1 to the useraudit object .. when the transaction is committed after row 5, 2 is inserted in the user ID column and 1 in the UserAudit userId column. For me, this is impractical :( How can I avoid this problem? Thanks!
RKodakandla
source share