Spring Data JPA auditing fails when persisting detached entity -


i've setup jpa auditing spring data jpa auditingentitylistener , auditoraware bean. want able persist auditor details on entities predefined identifiers. problem when jpa entity predefined id being persisted , flushed it's auditor details cannot persisted:

object references unsaved transient instance - save transient instance before flushing: me.auditing.dao.auditordetails

the interesting part when entity generated id saved - everything's fine. in both cases entities new. not pinpoint problem digging through hibernate code i've created sample project demonstrate (test class me.auditing.dao.auditedentityintegrationtest) has both entities predefined , generated identifiers , should audited.

the entities are:

@entity public class auditedentitywithpredefinedid extends auditableentity {      @id     private string id;      public string getid() {         return id;     }      public auditedentitywithpredefinedid setid(string id) {         this.id = id;         return this;     } } 

and:

@entity public class auditedentitywithgeneratedid extends auditableentity {      @id     @generatedvalue(generator = "uuid")     @genericgenerator(name = "uuid", strategy = "uuid")     private string id;      public string getid() {         return id;     }      public auditedentitywithgeneratedid setid(string id) {         this.id = id;         return this;     } } 

where parent class is:

@mappedsuperclass @entitylisteners(auditingentitylistener.class) public abstract class auditableentity implements serializable {      private static final long serialversionuid = -7541732975935355789l;      @manytoone(fetch = fetchtype.eager, cascade = {cascadetype.all})     @createdby     private auditordetails createdby;      @createddate     private localdatetime createddate;      @manytoone(fetch = fetchtype.eager, cascade = {cascadetype.all})     @lastmodifiedby     private auditordetails modifiedby;      @lastmodifieddate     private localdatetime modifieddate; 

and auditor getter implementation is:

@override public auditordetails getcurrentauditor() {     return new auditordetails()             .setid(null)             .setuserid("someuserid")             .setdivisionid("somedivisionid"); } 

edit 2016-08-08: seems when new entity predefined id saved, gets 2 different instances of createdby , modifiedby auditordetails, quite logical if entity wouldn't new. so, new entity generated gets both auditordetails of same instance, , 1 manually set id doesn't. tested saving auditor details in auditoraware bean before returning auditinghandler.

ok, solution find persist auditordetails before writing audited entities so:

@override @transactional public auditordetails getcurrentauditor() {     auditordetails details = new auditordetails()             .setid(null)             .setuserid("someuserid")             .setdivisionid("somedivisionid");     return auditordetailsrepository.save(details); } 

it not elegant solution, works now.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -