Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Issue while update the parent recored and removed some child records

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having two tables,Employe and Department .Employee is the parent table an deprtment is the child table.

create table employee (emp_id int primary key not null identity,first_name varchar(20),lastname varchar(20),dob varchar(20));
create table deparment (dept_id int primary key not null identity,dept_name varchar(20),dept_desc varchar(30),emp_id integer references employee(emp_id) not null);

I have create a record in employee table and attached 2 records to Department,I am able to save and delete the record perfectely. While updating i want to remove on department. Ideally what i am expecting is, when i remove the department from collection while updating.It should delete department de-selected record. Please find the configurtion files.

Employee.hbm.xml

<hibernate-mapping>
<class name="com.hcl.model.Employee" table="employee" lazy="false">
<id name="empId" type="java.lang.Integer" column="emp_id">
<generator class="native"/>
</id>
<property name="firstName" type="java.lang.String" column="first_name" length="20"/>
<property name="lastname" type="java.lang.String" column="lastname" length="20"/>
<property name="dob" type="java.lang.String" column="dob" length="20"/>
<set name="deparments" lazy="false" inverse="true" cascade="all">
<key on-delete="cascade">
<column name="emp_id"/>
</key>
<one-to-many class="com.hcl.model.Deparment"/>
</set>
</class>
</hibernate-mapping>

Department.hbm.xml


<hibernate-mapping>
<class name="com.hcl.model.Deparment" table="deparment" lazy="false">

<id name="deptId" type="java.lang.Integer" column="dept_id">
<generator class="native" />
</id>

<property name="deptName" type="java.lang.String" column="dept_name" length="20"/>
<property name="deptDesc" type="java.lang.String" column="dept_desc" length="30" />

<many-to-one name="employee" class="com.hcl.model.Employee" insert="false" update="false" not-null="true">
<column name="emp_id"/>
</many-to-one>
</class>
</hibernate-mapping>


Test class

Employee employee = (Employee) demo.load(new Employee(),4);
Set<Deparment> dept = employee.getDeparments();
Iterator<Deparment> iterator = dept.iterator();
Set<Deparment> dept1=new HashSet<Deparment>();
while (iterator.hasNext()) {
Deparment deparment = iterator.next();
if(deparment.getDeptName().equals("HR")){
dept1.add(deparment);
}

}
employee.setDeparments(dept1);
demo.update(employee);

Thanks
Giri

 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic