-->
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.  [ 5 posts ] 
Author Message
 Post subject: mang-to-one problems?
PostPosted: Fri Jul 09, 2004 4:08 am 
Newbie

Joined: Fri Jul 09, 2004 3:32 am
Posts: 3
hi,guys

I have two class that have mang-to-one relationship.
I have an Object Department(1) Linked to another Object Employee(2),

Source Department:
<class name="test.Department" table="department">
<id name="id" type="string" unsaved-value="any">
<column name="id" sql-type="varchar(32)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="name" type="string" update="true" insert="true">
<column name="name" sql-type="varchar(150)" not-null="false"/>
</property>
<property name="fatherId" type="string" update="true" insert="true">
<column name="father_id" sql-type="varchar(32)" not-null="false"/>
</property>
<set name="employee" lazy="true" inverse="true" cascade="all">
<key column="id"/>
<one-to-many class="test.Employee"/>
</set>

Source Employee:
<class name="test.Employee" table="employee">
<id name="id" type="string" unsaved-value="any">
<column name="id" sql-type="varchar(32)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="name" type="string" update="true" insert="true">
<column name="name" sql-type="varchar(80)" not-null="false"/>
</property>
<property name="password" type="string" update="true" insert="true">
<column name="password" sql-type="varchar(30)" not-null="false"/>
</property>
<many-to-one name="department" class="test.Department" >
<column name="id" sql-type="varchar(32)" not-null="true"/>
</many-to-one>
</class>


....
Employee employee=new Employee();
employee.setId("0001");
employee.setName("test1");
employee.setPassword("111");

Department dp=new Department();
dp.setId("dp1");
dp.setName("software design");
Set employeeSet=new HashSet();
employeeSet.add(employee);
dp.setEmployee(employeeSet);
....
session.save(dp);
....
sucess!

but when i...

Department department=new Department();
department.setId("dp1");

Employee employee=new Employee();
employee.setId("0002");
employee.setName("test2");
employee.setPassword("111");
employee.setDepartment(department);

....
session.save(employee);
....


it reports:
net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: test.Employee.department


please tell me how to do this


thanks
Feeler
[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 7:58 am 
Beginner
Beginner

Joined: Wed Nov 19, 2003 6:46 pm
Posts: 41
Location: Auckland, New Zealand
You haven't saved the department, so Hibernate considers it a transient object. Also, you probably want to add the employee to the department's set of employees.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 9:25 pm 
Newbie

Joined: Fri Jul 09, 2004 3:32 am
Posts: 3
thanks
but i don't want to save a new department,
i only want to save a new employee to the department's set of employees.
i really don't know how to do this
can you tell me?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 9:54 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2003 6:46 pm
Posts: 41
Location: Auckland, New Zealand
I would suggest you add the employee to the department's collection of employees, then update() the department. You shouldn't need to save() the employee as your cascade would cover saving the employee.

HTH

Craig


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 11:14 pm 
Newbie

Joined: Fri Jul 09, 2004 3:32 am
Posts: 3
thank a lot!!!!!!!!!!
i've been beeting this problem for two days you save me!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.