File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "@ManyToMany with eclipselink not working..." Watch "@ManyToMany with eclipselink not working..." New topic

@ManyToMany with eclipselink not working...

Raul Guerrero

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,

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.


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.


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

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.

I agree. Here's the link:
subject: @ManyToMany with eclipselink not working...
It's not a secret anymore!