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 Mon, 20 Jul 2009 04:58:19 GMT
I've been thinking about this a bit more and it seems I should have a
@ManyToMany join since a single role can have many permissions and any
single permission can be joined to many roles. 

 

My role table has all my roles, my permission table has all my permissions.
All I need the merge/persist of the role entity to do is update the join
table. What annotation should I use for this? 

 

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