I am getting below error in my application while trying to persist an object into database.
nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.delivery.Message
Entity:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter @Setter @ToString
@AllArgsConstructor
public class Message {
@Id
@Basic
@GeneratedValue(generator = "MESSAGE_SEQ", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "MESSAGE_SEQ", sequenceName = "MESSAGE_ID_SEQ")
@Column(name = "MESSAGE_ID")
protected long messageId;
@Column(name = "MESSAGE")
private String message;
@Column(name = "STATUS")
private String status;
}
DAO Layer:
@Transactional
public void saveEntity(@Nullable T entity) {
if (entity = null) {
return;
}
entityManager.persist(entity);
entityManager.flush();
entityManager.detach(entity);
}
Service Layer:
public void saveMessage(HttpEntity<Message> entity) throws Exception{
try{
repository.saveEntity(entity);
}catch(Exception e){
throw e;
}
}
Not sure why am getting this error. Error is thrown at persist method. Any reason for this issue?