I am getting the below error when saving child table data. If I will save without set Parent then works fine. Basic operation is when saving user it should also generate token through JPA EventListner.
org.springframework.dao.PessimisticLockingFailureException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.PessimisticLockException: could not execute statement.
Please have a look at the below code for a better understanding of issue. Here is the parent table
@Entity
@Getter
@Setter
@EntityListeners({AuditingEntityListener.class, UserEventListner.class})
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
....
}
Child table
@Entity
@Getter
@Setter
public class UserEmailVerification {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
@JoinColumn(name = "userId")
private User user;
@Column(name = "token")
private String token;
}
And UserEventListner code is something like this
@Component
@RequiredArgsConstructor
public class UserEventListner {
@PostPersist
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onPostPersist(final User user) {
UserEmailVerificationRepository userEmailVerificationRepository = (UserEmailVerificationRepository) BeanUtility.getBean("userEmailVerificationRepository");
UserEmailVerification verification = new UserEmailVerification();
verification.setUser(user); // issue is here if remove or comment it then works
verification.setToken(Utils.generateRandomToken());
userEmailVerificationRepository.save(verification);
}
}