These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Table per concreteclass throws NonUniqueObjectException
PostPosted: Wed Dec 07, 2011 6:28 am 
Newbie

Joined: Sat Nov 26, 2011 11:14 am
Posts: 3
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 2
Code:
    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>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.