aspose file tools*
The moose likes Object Relational Mapping and the fly likes @ManyToMany with eclipselink not working... 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 with eclipselink not working..." Watch "@ManyToMany with eclipselink not working..." New topic
Author

@ManyToMany with eclipselink not working...

Raul Guerrero
Greenhorn

Joined: Apr 13, 2009
Posts: 7
Ok, I'm trying to do a ManyToMany relationship between a Role entity and a Task entity, the MySQL schema and dummy data is:



Which leaves me with a bi-directional relationship like this: role <- roletask -> task

Now, on my java entities, the Role is the owner, as I'm gonna be adding tasks to a certain Role, so, the definition of Role is:



And for the Task entity, I'm defining this:



Now, when I get a Role from a session bean using the method: Role role = em.find(Role.class, new BigDecimal(1)); and then, when using the getter in Role for taskList, the taskList only has one item instead of the five defined in the database!

I'm using a code like this somewhere else and it works as it should, but here it's not working, I'm using eclipselink with glassfish 3.1.2 and, on the persistence.xml, I enabled the generated SQL log, and this is what eclipselink is executing when fetching the taskList:



So it looks like it's running two queries for fetching the objects, and the first one, when I run it in MySQL it fetches all tasks correctly, but the second one only fetches the first one, now, I don't know why is running two queries for this, but I know that the one that I actually get on my entity objects is the second one which is wrong, instead of the first one which is correct.

Is there something wrong with my code? or what am I doing wrong to fetch my taskList and get the data I'm supposed to get?

Hope someone can help me,

Thanks!
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
The issue is most likely that you are not correct maintaining your relationship. Most likely you are only adding to one side of the relationship.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side

If you call refresh() with the object, you should see all 5 tasks.

You could also disable the shared cache, but fixing your code would be best, as your object will still be corrupt with your persistence context.

See,
http://wiki.eclipse.org/EclipseLink/FAQ/How_to_disable_the_shared_cache%3F


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Raul Guerrero
Greenhorn

Joined: Apr 13, 2009
Posts: 7
Hi James, thanks for the reply.

Actually, on an act of despair, I updated my JDK to jdk 7 update 11, and suddenly it started working again, I was using update 5, and I dunno what was going on, but now that I updated, everything's working as it should.

It would be good to have more time to research on what happened and why the issue, but for now it works and I can get the job done.

The only thing is that I was getting an exception on the glassfish log stating that it wasn't able to load the DataSourceSpec class on startup, and I didn't review the glassfish log until after the update, so I didn't know that glassfish was failing before. Is just one of those really weird bugs.

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @ManyToMany with eclipselink not working...
 
Similar Threads
How toPersist Value in the Tables Using Entity Bean
mapping One-to-many relationships using NetBeans and Not Hibernate
hibernat creating the tables without cascade
Edit primary key, if it's a foreign key
org.hibernate.PropertyValueException: not-null property references a null or transient value: