| Author |
problem persisting an object with collections and manytomany relationship
|
Jordan Thompson
Greenhorn
Joined: Nov 09, 2009
Posts: 20
|
|
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;
}
}
}
|
No matter where you go - there you are
-- Buckaroo Banzai
|
 |
Rishi Shehrawat
Ranch Hand
Joined: Aug 11, 2010
Posts: 218
|
|
|
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
Joined: Nov 09, 2009
Posts: 20
|
|
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
Joined: Aug 11, 2010
Posts: 218
|
|
|
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
Joined: Nov 09, 2009
Posts: 20
|
|
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
Joined: Nov 09, 2009
Posts: 20
|
|
Can anyone explain why it updates to memory, but not to the database itself?
thanks,
Jordan
|
 |
Rishi Shehrawat
Ranch Hand
Joined: Aug 11, 2010
Posts: 218
|
|
|
Is the update running in a transaction. I faced similar problem & found that update was not running in a transaction.
|
 |
Jordan Thompson
Greenhorn
Joined: Nov 09, 2009
Posts: 20
|
|
|
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.
|
 |
 |
|
|
subject: problem persisting an object with collections and manytomany relationship
|
|
|