I'm trying to populate two tables: a user table and a user visit history table.
The user table code is working fine, but the many table (uservisithistory) doesn't get populated.
I think it may be in the mapping files.
I have the PK generation set to automatic in MySQL with no nulls, etc.
Here are the mapping files:
Here is the mapping for the user visit history (many side) table:
The code to create the many side I do inside a test "main" method like this: (see: "// add new visit history record" below)
The strange thing to me is that the mapping file I generated has a "Users" element in the many side.
I think the reason it might not be populating the many side is that it needs the foreign key to insert into the user visit history table. However, since the PK in the users table gets generated by MySQL automatically, I'm not sure how this works or why the code isn't populating the many side.
Does the code, having a Set in the users table, and a Users object in the uservisithistory table, look correct?
Any advice/suggestions would be greatly appreciated.
Why the transaction.commit() there? Where is the creation of that transaction? It isn't shown in the code you posted, and we don't see what is in your dao.save() method.
Have you also looked at your console or logs to see any exceptions being thrown when you save? Because of the inverse, I am sure you have an exception being thrown when it tries to insert the child before the parent, which would through an constraint violation.
Joined: Jul 12, 2002
Thanks for your reply...sorry it's taken me so long to reply to your thoughtful posting.
I hope this additional information will help you help me figure out why the many side doesn't populate.
I have key generation in MySQL set to auto-generate PKs for each table, which shouldn't be a problem since I changed the generation method (correctly, I "think") in the hibernate config files.
Please let me know if you need additional information, OK?
My UsersDao.save() method is the following: (auto-generated by MyEclipse)
In the UservisithistoryDao class, here is the save method: (auto-generated by MyEclipse)
And, the entire example test code to get a user's data (works OK), the number of users (works OK), and to save a user (works OK) and populate the many side (*DOES NOT WORK*) with the transaction statement that you noticed was missing in my posting is:
When I run this code, I get no error messages in the console window or even detailed SQL trace (even though I have that setting enabled).
org.hibernate.PropertyValueException: not-null property references a null or transient value: TestProject.Uservisithistory.users at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70) at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494) at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:134) at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213) at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157) at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108) at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290) at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185) at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160) at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108) at org.hibernate.engine.Cascade.cascade(Cascade.java:248) at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:410) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:299) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70) at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514) at TestProject.UsersDAO.save(UsersDAO.java:54) at TestProject.GetUserData.main(GetUserData.java:83)
Joined: Jul 12, 2002
OK, finally got the many side working!
Based on the error, I added the code to make sure the user object was added to the many-side object.
// add new visit Uservisithistory uvh = new Uservisithistory();
uvh.setUsers(newUser); // ** ADDED THIS LINE **
uvh.setUvhDateTimeOfVisit(dateToStore); uvh.setUvhPageFrom("home page"); HashSet<Uservisithistory> set = new HashSet <Uservisithistory>(); set.add(uvh);