Hi Guys,
I have a fairly simple requirement, I think.
I have a master detail relationship between Department and Employee. I also need to have a self join on employee between id and manager.
The tables look like this:
CREATE TABLE `department` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`label` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
CREATE TABLE `employee` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`logon` varchar(45) NOT NULL,
`deptId` int(10) unsigned NOT NULL,
`managerId` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_user_dept` (`deptId`),
CONSTRAINT `FK_user_dept` FOREIGN KEY (`deptId`) REFERENCES `department` (`id`)
I want to add a department and then add some employees and save the department (which will save the employees).
My mapping file looks like this:
<hibernate-mapping package="testing.hibernate">
<!--
*** Department ***
-->
<class name="Department" table="department">
<id name="id" column="id" unsaved-value="0">
<generator class="identity"></generator>
</id>
<property name="label" column="label" type="string"></property>
<!--
Many employees can belong a single department
-->
<many-to-one name="employees" column="deparmentId" class="Employee"
not-null="true" lazy="false"></many-to-one>
</class>
<!--
*** Employee ***
-->
<class name="Employee" table="employee">
<id name="id" column="id" unsaved-value="0">
<generator class="identity"></generator>
</id>
<property name="logon" column="logon" type="string"></property>
<!--
Many user can belong a single department
-->
<many-to-one name="department" column="deparmentId" class="Employee"
not-null="true" lazy="false"></many-to-one>
</class>
</hibernate-mapping>
Even just saving a department gives an error. Can anyone tell me what the mapping file should look like?
For reference the code is as follows:
Code:
public void testHibernate() {
DateFormat dateFormat = new SimpleDateFormat("dd MMM hh:mm:ssss");
ArrayList<Employee> employees = new ArrayList<Employee>();
Department department = new Department();
//This gives an error
saveDepartment(department);
for(int i = 0; i < 3; i++ ){
Employee employee = new Employee();
employee.setLogon("Test Employee " + dateFormat.format(new Date()));
employees.add(employee);
}
department.setEmployees(employees);
saveDepartment(department);
}
public boolean saveDepartment(Department department){
Transaction tx = null;
boolean retVal = false;
try {
session = factory.openSession();
tx = session.beginTransaction();
session.saveOrUpdate(department);
tx.commit();
retVal = true;
}
catch(HibernateException e) {
if(tx != null) {
tx.rollback();
}
System.out.println("Hibernate Exception :" + e.getMessage());
throw e;
}
catch(Exception e) {
System.out.println("Exception :" + e.getMessage());
}
finally {
session.close();
}
return retVal;
}
public class Department {
private int id;
private String label;
private ArrayList<Employee> employees;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String logon) {
this.label = logon;
}
public ArrayList<Employee> getEmployees() {
return employees;
}
public void setEmployees(ArrayList<Employee> employees) {
this.employees = employees;
}
public String ToString() {
return "Department: \n" +
"ID: " + id + "\n" +
"Label: " + label;
}
public class Employee {
private int id;
private String logon;
private Employee manager;
private Department department;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLogon() {
return logon;
}
public void setLogon(String logon) {
this.logon = logon;
}
public Employee getManager() {
return manager;
}
public void setManager(Employee manager) {
this.manager = manager;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String ToString() {
return "Employee: \n" +
"ID: " + id + "\n" +
"Logon: " + logon + "\n" +
"Department: " + department.ToString() + "\n + " +
"Manager: " + manager.ToString();
}
}