This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem persisting an object with collections and manytomany relationship

 
Jordan Thompson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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;
}
}
}
 
Rishi Shehrawat
Ranch Hand
Posts: 218
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic