Spring - hibernate: detached entity passed to persist -


i have following object:

public class constraint { @id @generatedvalue(strategy=generationtype.identity) int id;  string name; string description; string path; int level;  @manytoone @joincolumn(name="parent_id") constraint parent;  @onetomany @joincolumn(name="parent_id") set<constraint> children;  @manytoone @joincolumn(name="type_id") constrainttype type; } 

as see table represents tree structure. having functions:

public list<constraint> getalldescendantsofconstraint(integer id) {     stringbuilder sb = new stringbuilder();      sb.append("with recursive tree ");      sb.append("( ");      sb.append(" select * constraints id = :id ");      sb.append(" union ");      sb.append(" select a.* constraints a, tree b a.parent_id = b.id ");      sb.append(") ");      sb.append("select * tree id <> :id ");      query q = entitymanager.createnativequery(sb.tostring(), constraint.class);     q.setparameter("id", id);     return (list<constraint>) q.getresultlist(); } @transactional public void setuppaths() {     query q = entitymanager.createquery("from constraint", constraint.class);     @suppresswarnings("unchecked")     list<constraint> constraints = (list<constraint>) q.getresultlist();      for(constraint c : constraints) {         list<constraint> descendants = getalldescendantsofconstraint(c.getid());         for(constraint d : descendants) {             string tpath;             if((tpath = d.getpath()) == null || d.getpath().equals(""))                 tpath = string.valueof(c.getid());             else                 tpath = d.getpath() + "." + string.valueof(c.getid());             d.setpath(tpath);             entitymanager.persist(d);         }     } } 

if call setuppaths "detached entity passed persist exception". if instead of entitymanager.persist(d); :

d = entitymanager.merge(d);             d.setpath(tpath); 

nothing happens (the data not saved in db). if call flush, detached entity passed persist exception well. suspect has me not eagerly loading parent or children?

edit: after further testing since above seems fishy, seems every entity entitymanager, no matter how (either em.find(myobject.class,id or kind of queries) detached right after fetching! see calling

em.contains(myobject) 

which returns false. problem use exact same setup on other spring projects same database server , it's wroking fine.

after (finally) turning debug info hibernate on saw entitymanager closing session right after fetch wrong since using @transactional annotation. seems problem sts (springsource toolsuite) , spring configuration, particularly:

<tx:annotation-driven mode="aspectj" transaction-manager="transactionmanager"/> 

this supposedly witht correct maven setup

<plugin>             <groupid>org.codehaus.mojo</groupid>             <artifactid>aspectj-maven-plugin</artifactid>             <version>1.3.1</version>             <dependencies>                 <dependency>                     <groupid>org.aspectj</groupid>                     <artifactid>aspectjrt</artifactid>                     <version>${org.aspectj-version}</version>                 </dependency>                 <dependency>                     <groupid>org.aspectj</groupid>                     <artifactid>aspectjtools</artifactid>                     <version>${org.aspectj-version}</version>                 </dependency>             </dependencies>             <executions>                 <execution>                     <id>compile</id>                     <configuration>                         <source>${java-version}</source>                         <target>${java-version}</target>                         <verbose>false</verbose>                         <outxml>true</outxml>                         <aspectlibraries>                             <aspectlibrary>                                 <groupid>org.springframework</groupid>                                 <artifactid>spring-aspects</artifactid>                             </aspectlibrary>                         </aspectlibraries>                     </configuration>                     <goals>                         <goal>compile</goal>                     </goals>                     </execution>             </executions>             <configuration>                 <outxml>true</outxml>                 <source>${java-version}</source>                 <target>${java-version}</target>             </configuration>         </plugin> 

should take care of compile-time weaving went wrong. using plugin 1.2, when changed 1.3.1 magically worked.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -