• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Cascade update JPA ?

 
Chris Ericsson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I go stuck trying to make a ManyToMany cascading event to work. I've bean browsing through similar posts, but I can't see where my code is doing wrong.

I have two tables Organisation and Project, Organisation is the owner of the relationship and there is a relation table between the two.
I am trying to persist a new project entity, that can have one or more organisation associated to it. I use a selectManyListbox(JSF 2) to let the user choose more than one organisation.

All this works fine until I try to cascade the relationship. Either only the project is getting persisted or the persistent manager creates new copies of the same project, for every organisation that is associated to it. I.e. different orgID and projID (instead of different orgID refering to the same projID)

Why does JPA makes new instance of the same Project (identical properties, except the ID) when I try to persist the relation ?

I am trying to learn JPA/JSF and any help is very welcomed. Greetings Chris.

PS. In the Project bean controller I got the right instances of Organisations that the user selects.

Entity objects


Project bean controller


Finally the Organiasation facade edit function
 
Chris Ericsson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there no one that has any idea, or am I asking the wrong question? If I miss something in the code or explanation, let me know.

Thankful for any hint, Chris
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you persist the project?

If it has references to existing (detached) Organizations then you need to resolve those references when persisting, or call merge() instead of persist(). To resolve a reference you need to ensure that it references the managed object from the current persistence context (find() it in the current EntityManager).

Ensure you do not have two copies of the same Project.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic