• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

problem persisting an object with collections and manytomany relationship

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my User object:

I get a User(s) from the database:


I update it/them:


And then I save it:


And there is my problem. Everything looks fine until, When I debug the Server, the User (u) looks like it works, but when I refresh it, it reverts back to whatever is in the database before the attempted changes. I did try using merge(u) instead of persist(u), but it made no difference! I thought that if the entity already existed, you NEEDED to call merge().
Note that the user, permissions, dataloggers, columnheadings, etc all exist. All I am doing is trying to make the relationship between them.
Any suggestions?

OK - I'm an idiot. I only updated one way of the many-to-many relationships. You need to update both ends :

u.getDataloggerCollection().clear();
for (String userLogger : user.loggers) {
for (Datalogger availLogger :loggerList) {
if (userLogger.equals(availLogger.getName())) {
u.getDataloggerCollection().add(availLogger);
/** NEED TO ADD THIS: **/
availLogger.getUsers.add(u);
break;
}
}
}
 
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This could be due to use of inverse in the mapping file. In the user mapping file you might have specified inverse="true" for the dataLoggerCollection. This will result in Hibernate ignoring changes to data logger collection in user object.
 
Jordan Thompson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
R_Shehrawat - I'm not sure what you mean by the mapping file...

Anyway, that didn't work either. I have now also tried this new method that updates both the User as well as the collections and persists both sides:



I am not getting any errors and when I run this code and when I look at the data from the client app, it looks like the code worked... Until I restart the server... the data is only retained in memory on the server and never inserted into the database.
I was mistaken when I said I had it working - the data is temporarily kept in the server until the ejb is re-installed or glassfish is restarted.
 
Rishi Shehrawat
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My mistake.. i should have looked at the code carefully.. so seems like you are using JPA.. by mapping file i meant hibernate HBM file, which has hibernate specific option inverse="true" which results in the type of behavior you talked about .. (i:e only one side of updates to a relationship are persisted, the other side are ignored for optimization purpose)
 
Jordan Thompson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
R_Shehrawat - thanks for your help. I have searched for any file containing "inverse" in my server project and cannot find one. I also searched for a file called "hibernate" or one called "*.hbm" and could not find any.
For what its worth, here is the ejb handler for a user:


 
Jordan Thompson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain why it updates to memory, but not to the database itself?

thanks,
Jordan
 
Rishi Shehrawat
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the update running in a transaction. I faced similar problem & found that update was not running in a transaction.
 
Jordan Thompson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, its not. I tried using a transaction, but got an exception when I tried to create the transaction. From what I found out, it turns out that you cannot mix transactions and @PersistenceContext.
 
No, tomorrow we rule the world! With this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic