-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ManyToOne - Save - org.hibernate.TransientObjectException
PostPosted: Mon Dec 14, 2009 9:53 am 
Newbie

Joined: Wed Feb 08, 2006 10:53 am
Posts: 3
While I try to save top level entity (using JPA - hibernate), do I need to get the ManyToOne mapped entity freshly from database and set it or cannot I just set Id (of manyToOne mapped entity and save top level entity? When do not get fresh entity it throws: org.hibernate.TransientObjectException:

Table structures we are using:

DEPARTMENT(DEPARTMENT_ID BIGINT, NAME VARCHAR(128))

EMPLOYEE(EMPLOYEE_ID BIGINT, NAME VARCHAR(128), DEPARTMENT_ID BIGINT)

Entities:
class Department
{
@Id
Long departmentId;
String name;
@Version
Long versionNumber;

}

class Employee
{
@Id
Long employeeId;
String name;
@ManyToOne
Department department;
@Version
Long versionNumber

}

(both classes have setter and getter methods for all fields and default constructor, constructor which takes primary key as argument) Now if I want to save Employee with departmentId (say 100), do I need to get the Department record first and then set it in employee?

Cannot I create instance of Department directly (by setting primary key(departmentId)) and set Department instance in Employee and save Employee? When I do this it is throwing org.hibernate.TransientObjectException.

Any suggestions on best practice to be followed for this?

Thank you in advance


Top
 Profile  
 
 Post subject: Re: ManyToOne - Save - org.hibernate.TransientObjectException
PostPosted: Tue Dec 15, 2009 2:45 am 
Newbie

Joined: Wed Feb 08, 2006 10:53 am
Posts: 3
One of the solution could be:
We will update Employee POJO as below

Code:
ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "DEPARTMENT_ID", referencedColumnName = "DEPARTMENT_ID", **insertable=false, updatable=false**)
    private Department department;
       
    @Column(name = "department_id")
    private Long departmentId;


(both department and departmentId will have setter and getter methods)

and Now here (please see that both department and departmentId mapped to same column (DEPARTMENT_ID)
we use department only for fetching Department details
and departmentId to insert or update Employee

But I am worried if it is a better approach.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.