Win a copy of Terraform in Action this week in the Cloud forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Confused on the order of persisting

Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am new to hibernate and having a lot of confusions.

I created 3 classes.

I have User and Category properties in

Now when i am trying to persist only the expense object after setting the User and Category properties, i am getting TransientObjectException because i am not persisting the User and Category objects to the database.

But when I persist them before persisting the Expense object using, it works fine.

I have <many-to-one> association in Expense.hbm.xml for both User and Category, because multiple expenses can have single or many users with single or many categories.

So what is the case when we have to explicitly save these objects first and then the final object, and when is not the case.
What is the actual concept behind this? I want the User and the Category objects to automatically get persisted when i call;

I am confused. Please help me.
Posts: 1682
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the cascade attribute or annotation depeding if you are using XML or annotations. You can use CascadeType.PERSIST and CascadeType.MERGE to get the behaviour you are looking for.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic