Hi,
Is there any way to insert a row in the parent table alone using hibernate.
My case is like this
A department table is related to an employee table in one-to many fashion. But while trying to insert a row in department, i am force to add a set of employees also.The ddl used to create the schema are as follows
create table department (dept_id varchar(15) primary key,
dept_name varchar(50),
dept_location varchar(50));
create table employee (emp_id number(10) primary key,
emp_name varchar(50),
dept_id varchar(15),
foreign key (dept_id) references department(dept_id));
I used middlegen to create the mapping files they are as follows
Department.hbl.xml-----------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="com.roche.dal.Department"
table="DEPARTMENT"
>
<id
name="deptId"
type="java.lang.String"
column="DEPT_ID"
>
<generator class="assigned" />
</id>
<property
name="deptName"
type="java.lang.String"
column="DEPT_NAME"
length="50"
/>
<property
name="deptLocation"
type="java.lang.String"
column="DEPT_LOCATION"
length="50"
/>
<!-- associations -->
<!-- bi-directional one-to-many association to Employee -->
<set
name="employees"
lazy="true"
inverse="false"
cascade="delete"
>
<key>
<column name="DEPT_ID" />
</key>
<one-to-many
class="com.roche.dal.Employee"
/>
</set>
</class>
</hibernate-mapping>
employee.hbm.xml--------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="com.roche.dal.Employee"
table="EMPLOYEE"
>
<id
name="empId"
type="java.lang.String"
column="EMP_ID"
>
<generator class="assigned" />
</id>
<property
name="empName"
type="java.lang.String"
column="EMP_NAME"
length="50"
/>
<!-- associations -->
<!-- bi-directional many-to-one association to Department -->
<many-to-one
name="department"
class="com.roche.dal.Department"
not-null="true"
>
<column name="DEPT_ID" />
</many-to-one>
<!-- bi-directional one-to-one association to EmployeeSecurityData -->
<one-to-one
name="employeeSecurityData"
class="com.roche.dal.EmployeeSecurityData"
outer-join="auto"
/>
<!-- bi-directional one-to-many association to DocumentReviewData -->
<set
name="documentReviewDatas"
lazy="true"
inverse="true"
>
<key>
<column name="EMP_ID" />
</key>
<one-to-many
class="com.roche.dal.DocumentReviewData"
/>
</set>
<!-- bi-directional many-to-many association to DocumentData -->
<set
name="documentDatas"
lazy="true"
table="DOCUMENT_REVIEW_DATA"
>
<key>
<column name="EMP_ID" />
</key>
<many-to-many
class="com.roche.dal.DocumentData"
>
<column name="DOC_ID" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
and my generated Department class is
import java.io.Serializable;
import java.util.Set;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Department implements Serializable {
/** identifier field */
private String deptId;
/** nullable persistent field */
private String deptName;
/** nullable persistent field */
private String deptLocation;
/** persistent field */
private Set employees;
/** full constructor */
public Department(String deptId, String deptName, String deptLocation, Set employees) {
this.deptId = deptId;
this.deptName = deptName;
this.deptLocation = deptLocation;
this.employees = employees;
}
/** default constructor */
public Department() {
}
/** minimal constructor */
public Department(String deptId, Set employees) {
this.deptId = deptId;
this.employees = employees;
}
public String getDeptId() {
return this.deptId;
}
public void setDeptId(String deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return this.deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptLocation() {
return this.deptLocation;
}
public void setDeptLocation(String deptLocation) {
this.deptLocation = deptLocation;
}
public Set getEmployees() {
return this.employees;
}
public void setEmployees(Set employees) {
this.employees = employees;
}
public String toString() {
return new ToStringBuilder(this)
.append("deptId", getDeptId())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof Department) ) return false;
Department castOther = (Department) other;
return new EqualsBuilder()
.append(this.getDeptId(), castOther.getDeptId())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getDeptId())
.toHashCode();
}
}
Since I want to have other tables related to employee, I can add an employee when ever a department is added.
Is there any configuration setting or any parameter in the mapping file so that i can insert to the parent table alone?
Thanks in advance
Deepak