I have an abstract class and two concrete class which extend these abstract class. I have mapped all the three in a employee.hbm.xml file. It is throwing the following error. My db is mysql
Quote:
Exception in thread "main" org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [temp.ContractEmployee#0]
Could someone kindly look into it.
Abstract Class Code:
package temp;
public abstract class Employee {
public long id;
public String employeeName;
public String location;
public String getEmployeeName()
{
return employeeName;
}
public void setEmployeeName(String employeeName)
{
this.employeeName = employeeName;
}
public String getLocation()
{
return location;
}
public void setLocation(String location)
{
this.location = location;
}
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
}
Extended Class 1 Code:
package temp;
public class RegularEmployee extends Employee {
public double salary;
public double bonus;
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
Extended Class 2Code:
package temp;
public class ContractEmployee extends Employee {
public double hourlyRate;
public double perDiem;
public double contractPeriod;
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public double getPerDiem() {
return perDiem;
}
public void setPerDiem(double perDiem) {
this.perDiem = perDiem;
}
public double getContractPeriod() {
return contractPeriod;
}
public void setContractPeriod(double contractPeriod) {
this.contractPeriod = contractPeriod;
}
}
MainClass Code:
package temp;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args)
{
SessionFactory sessionFactory;
sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
RegularEmployee regular = new RegularEmployee();
regular.setEmployeeName("Prasad");
regular.setLocation("Hyderabad");
regular.setBonus(500.00);
regular.setSalary(20000.00);
ContractEmployee cemployee = new ContractEmployee();
cemployee.setEmployeeName("Sai");
cemployee.setLocation("Hyderabad");
cemployee.setContractPeriod(12);
cemployee.setHourlyRate(50.00);
cemployee.setPerDiem(300.00);
session.clear();
session.save(regular);
session.save(cemployee);
session.getTransaction().commit();
session.flush();
session.close();
}
}
employee.hbm.xml file Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="temp.Employee" abstract="true">
<id name="id" column="EMP_ID" type="long">
<generator class="assigned"></generator>
</id>
<property name="employeeName" column="EMP_NAME" type="string"/>
<property name="location" column="LOCATION" type="string"/>
<union-subclass name="temp.RegularEmployee" table="REG_EMP">
<property name="salary" column="SALARY"/>
<property name="bonus" column="BONUS"/>
</union-subclass>
<union-subclass name="temp.ContractEmployee" table="CON_EMP">
<property name="hourlyRate" column="RATE"/>
<property name="perDiem" column="PER_DIEM"/>
<property name="contractPeriod" column="CON_PERIOD"/>
</union-subclass>
</class>
</hibernate-mapping>