Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

@ManyToMany with eclipselink not working...

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Raul Guerrero
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Do you want ants? Because that's how you get ants. And a tiny ads:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic