2014-09-28 172 views
0
I want to know, that if we have two class : 

1) Employee.java 

2) PermanentEmployee.java 

Now, I want to know: 

1. if there exists a record with `id = 1` in Employee table. 
2. id generator used in increment. 
3. There is no record in PermanentEmployee table. 
4. Is it possible to insert a record with id 1 in PermanentEmployee table 

ie if we save record of `permanentEmployee`, than a new record should get insert in `permannentEmployee` table and it should refer to already existing record in Employee table ? 

Any pointers will be of great help 


More Description : 

Employee.java 
---------------------------- 
package com.kar.hibernate; 

public class Employee { 

    private int eid; 
    private String ename; 
    private String ecity; 
    private String eemail; 
    private Long ephone; 
    public Employee() { 
     super(); 
    } 
    public Employee(String ename, String ecity, String eemail, Long ephone) { 
     super(); 
     this.ename = ename; 
     this.ecity = ecity; 
     this.eemail = eemail; 
     this.ephone = ephone; 
    } 
    public int getEid() { 
     return eid; 
    } 
    public void setEid(int eid) { 
     this.eid = eid; 
    } 
    public String getEname() { 
     return ename; 
    } 
    public void setEname(String ename) { 
     this.ename = ename; 
    } 
    public String getEcity() { 
     return ecity; 
    } 
    public void setEcity(String ecity) { 
     this.ecity = ecity; 
    } 
    public String getEemail() { 
     return eemail; 
    } 
    public void setEemail(String eemail) { 
     this.eemail = eemail; 
    } 
    public Long getEphone() { 
     return ephone; 
    } 
    public void setEphone(Long ephone) { 
     this.ephone = ephone; 
    } 
    @Override 
    public String toString() { 
     return "Employee [eid=" + eid + ", ename=" + ename + ", ecity=" + ecity 
       + ", eemail=" + eemail + ", ephone=" + ephone + "]"; 
    } 


} 
----------------------------------------------- 
OldEmployee.java 
----------------------------------------------- 
package com.kar.hibernate; 

public class OldEmployee extends Employee{ 

    private String oeDateOfRelease; 
    private int oeDurationInCompany; 

    public OldEmployee() { 
    } 

    public OldEmployee(String ename, String ecity, String eemail, Long ephone 
      ,String oeDateOfRelease, int oeDurationInCompany) { 
     super(ename, ecity, eemail, ephone); 
     this.oeDateOfRelease=oeDateOfRelease; 
     this.oeDurationInCompany=oeDurationInCompany; 
    } 

    public String getOeDateOfRelease() { 
     return oeDateOfRelease; 
    } 

    public void setOeDateOfRelease(String oeDateOfRelease) { 
     this.oeDateOfRelease = oeDateOfRelease; 
    } 

    public int getOeDurationInCompany() { 
     return oeDurationInCompany; 
    } 

    public void setOeDurationInCompany(int oeDurationInCompany) { 
     this.oeDurationInCompany = oeDurationInCompany; 
    } 

    @Override 
    public String toString() { 
     return super.toString()+":::::"+ "OldEmployee [oeDateOfRelease=" + oeDateOfRelease 
       + ", oeDurationInCompany=" + oeDurationInCompany + "]"; 
    } 

} 
================================================= 
Client1.java 
================================================= 
package com.kar.hibernate; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 

import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.classic.Session; 

public class Client1 { 
    public static void main(String[] args) { 
     Transaction tx=null; 
     try 
     { 
      SessionFactory sessionFactory=CHibernateUtil.getSessionFactory(); 
      Session session=sessionFactory.openSession(); 
      tx=session.beginTransaction(); 

      String ename="anuj"; 
      String ecity="blore"; 
      String eemail="[email protected]"; 
      Long ephone=1111l; 

      Employee e=new Employee(ename, ecity, eemail, ephone); 
      session.save(e); 

      tx.commit(); 
      session.close(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
      if(tx!=null) 
       tx.rollback(); 
     } 
    } 
} 

=================================================================== 
Client2.java 
========================================== 
package com.kar.hibernate; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 

import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.classic.Session; 
import org.hibernate.event.SaveOrUpdateEvent; 

public class Client2{ 
    public static void main(String[] args) { 
     Transaction tx=null; 
     try 
     { 
      SessionFactory sessionFactory=CHibernateUtil.getSessionFactory(); 
      Session session=sessionFactory.openSession(); 
      tx=session.beginTransaction(); 

      Employee e=(Employee)session.load(Employee.class, 1); 


      OldEmployee oe=new OldEmployee(e.getEname(), 
        e.getEcity(), e.getEemail(), 
e.getEphone(), "1/1/2014", 2); 

      session.saveOrUpdate(oe); 

      tx.commit(); 
      session.close(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
      if(tx!=null) 
       tx.rollback(); 
     } 
    } 
} 

------------------------------------------------------------------- 
Employee.hbm.xml 
--------------------------------------------------------------------- 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 
<hibernate-mapping package="com.kar.hibernate"> 

<class name="Employee" table="employee"> 
<id name="eid" column="eid" type="int"> 
<generator class="increment"/> 
</id> 
<property name="ename" column="ename" type="string"/> 
<property name="ecity" column="ecity" type="string"/> 
<property name="eemail" column="email"/> 
<property name="ephone"/> 

<joined-subclass name="OldEmployee" table="oldEmployee"> 
<key column="eid"></key> 
<property name="oeDateOfRelease"></property> 
<property name="oeDurationInCompany"></property> 
</joined-subclass> 

</class> 
</hibernate-mapping> 

==================================================================== 
Hibernate.cfg.xml 
=================================================================== 
<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/chm</property> 
      <property name="hibernate.connection.username">root</property> 
       <property name="hibernate.connection.password">root</property> 

      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>   
    <property name="hibernate.hbm2ddl.auto">update</property>   
    <property name="show_sql">true</property> 
    <mapping resource="com/kar/hibernate/Employee.hbm.xml"/> 

     </session-factory> 
</hibernate-configuration> 

====================================== =================================================

============ //現在我的問題是在Client2中編輯的內容,因此,當我們嘗試保存OldEmployee而不是 時,新記錄不應在Employee表中插入。但OldEmployee表的新記錄應該引用員工表的現有記錄?休眠表每個子類映射

任何指針都會有很大的幫助。每個子類

+1

您是否嘗試過查看所使用系統的文檔? – APerson 2014-09-28 19:14:46

+0

更多描述: – ankit 2014-09-28 20:05:39

回答

0

繼承表的情況下,hibernate documentation.

您的問題,像你想象的那樣,不能得到解決。首先,不能以常規方式完成java或hibernate。所以你的選擇是;

  • 刪除原Employee並插入新OldEmployee對象
  • 手動插入老員工對象。 session.createSQLQuery("insert into oldEmployee values (employeeId, ....)")

正如你所看到的這兩個解決方案的解決方法,也許這將是更明智的重新檢查你的產業,甚至商業模式。

+0

感謝您的幫助。 – ankit 2014-09-29 17:58:23