org.hibernate.transientobjectexception

In this section, you will learn org.hibernate.transientobjectexception it's cause and solution.

Ads

Tutorials   
How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

org.hibernate.transientobjectexception

In this section, you will learn org.hibernate.transientobjectexception it's cause and solution.

But before discussing it further, you need to know about transient object :

A new persistence class instance, which is not related to a Session and has not contained any value representing row of a database table and also have not contained any identifier value, is considered as transient by Hibernate.

Student student=new Student();
student.setName("Rajesh"); // student is in transient state

You can convert  transient instance into persistence by associating it with a Session as follows :

Long id = (Long) session.save(student);
// person is now in a persistent state

transientobjectexception

The transientobjectexception is thrown when a transient instance is passes to a Session method that expects a persistent instance.

Let us take a simple example :

X is referring to Y using a primary key column of yId of Y.

The mapping of X is something like this :

<class name="net.roseindia.X" table="X" >
<id name="xId" type="java.lang.Long">
<property name="xId" column="x_id" />
</id>
.................
.................
<many-to-one name="yId" class="net.roseindia.Y" fetch="select">
<column name="y_id" not-null="true" />
</many-to-one>
................
................

Now, we want to persist the value of foreign key in X. You have to make sure that instance Y is persistent not transient.

But if you try something like this :

X x = new X();
Y y = new Y();
x.setY(y);
..........
..........
session.save(x);

This will throw org.hibernate.transientobjectexception :

solution

Since Y is in transient state which is the root cause of the problem. To make instance Y persistent, you need to bind y to the session. You can do it like this :

X x = new X();
Y y = session.get(Y.class, new Long(1));
x.setY(y);
.........................
.........................
session.save(x);

The above code will run smoothly without throwing org.hibernate.transientobjectexception .

Advertisements

Ads
Share on Google+Share on Google+

org.hibernate.transientobjectexception

Posted on: April 14, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: org.hibernate.transientobjectexception  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads