Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

many side of one to many not being populated  RSS feed

 
Ranch Hand
Posts: 1574
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Users Table


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.

Thanks.

Mike
 
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First suggestions is to move your

inverse="true"

To the other side of the relationship. So instead of in the Set of user, into the child mapping file.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I am assuming quite a bit when I read

dao.save(newUser);
transaction.commit();

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.

Mark
 
Mike London
Ranch Hand
Posts: 1574
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

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?

Thanks.

- Mike

--------------------------

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).
 
Mark Spritzler
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry I didn't look the first time, but where is your cascade option for the collection.

Mark
 
Mike London
Ranch Hand
Posts: 1574
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

Pardon the "newbie" reply, but where should I put the cascade option?

Thanks.
 
Mike London
Ranch Hand
Posts: 1574
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yikes! This is complicated...

If I enter cascade="all",

like this:

<set name="uservisithistories" inverse="true" cascade="all" >
<key>
<column name="usr_pk" >
<comment>foreign key back to users table.</comment>
</column>
</key>
<one-to-many class="TestProject.Uservisithistory" />
</set>


I get this:

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)
 
Mike London
Ranch Hand
Posts: 1574
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

Works fine now!

Thanks to all who replied.

mike
 
Mark Spritzler
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fantastic. Good work. You got the last part before I could even come back and reply.

Mark
 
Mike London
Ranch Hand
Posts: 1574
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark.

Because of great people like you, JavaRanch ROCKS!!!

Thank you for all your help and great replies.



M
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!