openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "C N Davies" <...@cndavies.com>
Subject RE: Merge updating the wrong table
Date Tue, 21 Jul 2009 09:12:32 GMT
I finally worked it out. Took me a few days but I have to set cascade type
on the Role entity to "Refresh" then it will only update the join table and
not attempt to update the entity tables. 

 

Finally!



 

Chris

 

From: C N Davies [mailto:cnd@cndavies.com] 
Sent: Sunday, 19 July 2009 5:14 AM
To: 'users@openjpa.apache.org'
Subject: Merge updating the wrong table

 

I have a parent entity and a child entity.  It uses a join table to hold the
relationship. When I do a get on the parent it retrieves the children
correctly but when I call merge on the parent after adding some children, it
tries to persist the children in the child table not into the join table. A
is a basic outline of my code is below.  I don't understand why the get is
working but the mere is working this way. Am I missing something obvious?

 

Parent:

 

@Entity

 

public class Role extends CoreObject{

      @OneToMany(cascade={CascadeType.ALL})

      private List <Permission> permissions;

 

      /**

       * @param permissions the permissions to set

       */

      public void setPermissions(List <Permission> permissions) {

            this.permissions = permissions;

      }

 

      /**

       * @return the permissions

       */

      public List <Permission> getPermissions() {

            return permissions;

      }

}

 

Child:

 

@Entity

 

public class Permission extends CoreObject{

 

}

 

 

I call merge like so :

 

public void merge(CoreObject co){

            EntityManagerFactory emf =
Persistence.createEntityManagerFactory(ConfigUtils.getParameter(Constants.Pa
ram_PersistenceUnit));             

            threadLocal = new ThreadLocal<EntityManager>();

            em = threadLocal.get();

            em = emf.createEntityManager();

            threadLocal.set(em);

            em.getTransaction().begin();

em.merge(co);

            em.getTransaction().commit();

em.close();

 

      }

 


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message