Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Object Relational Mapping and the fly likes Confused on the order of persisting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Confused on the order of persisting" Watch "Confused on the order of persisting" New topic

Confused on the order of persisting

Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

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.
Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

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.

[How To Ask Questions][Read before you PM me]
I agree. Here's the link:
subject: Confused on the order of persisting
It's not a secret anymore!