vellingiri dharmalingam

Greenhorn
+ Follow
since Jan 18, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by vellingiri dharmalingam

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