This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes ManyToMany merge problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "ManyToMany merge problem" Watch "ManyToMany merge problem" New topic
Author

ManyToMany merge problem

Maria Michael
Greenhorn

Joined: Sep 07, 2011
Posts: 18

Hello,

I have the following relationship:

An interview has many participants
A participant can be in many interviews

I resolved the relationship with an intermediate table in the database INTERVIEW_PARTICIPANT.

The following is the annotations in the Interview.java





The following is the annotations in the Participant.java



When I select the participants in the Interview, on saving (em.merge()) it prompts an error.

Summary of the error:



My JSF code:




Any ideas cause I really don't understand the problem from the stacktrace?

Thanks.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

You need to initialize the List.
JPA doesnt know which list implementation to choose.
Try:

if you want an ArrayList.

Edit: By the way this applies for one to many relations as well.
Edit2: I forgot to mention. You dont need to initialize it inside the class. It just has to be initialized before you do anything with it in jpa context.


Please correct my English.
Maria Michael
Greenhorn

Joined: Sep 07, 2011
Posts: 18

Manuel thank you for your reply.

I initialised the list in both the entity classes as you said.

Still the problem exists. I manually added data in the relationship via the SQL SErver database.
When I deploy the application the list shows correctly the selections based on the keys I entered in the database. If I just save without changing anything the error occurs.

BUT If I remove all selections from my jsf page the Save button works! and in the database what I previously entered is correctly removed.

My thinking is that at least it recognised the manytomany relationship.

Perhaps the entitymanager does not handle the lists correctly? I also tried Collection but still the same problem exists.

btw I do not have a class for the intermediate table-relationship as i read jpa handles it via the lists. Do I need a class?

Also before calling the entitymanager.merge() I print the list of participants and it shows the correct values.


Thank you.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Can you include the full stack trace, code where you call the merge, and what version you are using (did you try the latest EclipseLink release?).

Do you have a null in the collection, or a instance of a class that is not mapped?

Do other operations and class work? What is special about this case?

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Maria Michael
Greenhorn

Joined: Sep 07, 2011
Posts: 18

Sure thank you,

The stacktrace:



My code in the controller bean



My code in the AbstractFacade.java (that has all operations done by the entity manager - yes I am using the code from the CRUD application Netbeans generated for me. It was a good starting point for me as a beginner




EclipseLink version (No I haven't tried to upgrade anything so that I don't mess up anything that I managed to make it work so far :S sad i know)



I think no I don't have a null in the collection I would know when I was printing it right?

Does it matter that I don;t have an entityclass for the intermediate table? I am sure the many to many works because it removes the entries from the database when I clear the list. but to insert new, or make changes it crushes.

It is the first time I ever tried @Manytomany relationship, so far onetoone and onetomany relationships in my application work just fine.

Thank you for your time.
Maria Michael
Greenhorn

Joined: Sep 07, 2011
Posts: 18

Solved the problem!!!

As I couldn't find many similar posts in google as my problem here, and my java code looked correct I thought the problem might be on the underlying database design.

My design was as following:

Interview table - Interview-Participants table and the Participant table. I figured there might be a problem that the many is on a table part of an aggregate relationship (Person - Participant).

Therefore I made the many to many relationship between the Interview table and the Person table and made all the necessary changes in my code.

This solved the problem even though I don't really understand why.

Thank you for your help.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Odd, what was the code that did not work vs the code that did?

You may want to log a bug and include both versions of your code, a null-pointer should never be occurring in merge, although it sounds like you may have mapped something wrong.
Maria Michael
Greenhorn

Joined: Sep 07, 2011
Posts: 18

Sorry for not replying earlier.

What I modified was the mapping of objects to the tables in the database as I described earlier (my post on June 7).

No code was modified other than the code to reflect the new way of mapping.

Hope this helps someone with similar problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ManyToMany merge problem
 
Similar Threads
JPA: Count object relations in n:m relationship - do I need a native query?
error while persisting many to many relationship object : Attempt to assign
EJB 3.0 and ManyToMany relationship
2 Hibernate Qs: Collections, Annotation Mapping
ManyToMany: ternary Relationship