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="a@a";
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表的新记录应该引用现有的Employee Table记录?
任何指针都会有很大的帮助。
答案 0 :(得分:0)
每个子类的继承表就是这种情况,hibernate documentation.
你的问题,无法像你想象的那样解决。首先,不能以常规方式完成java或hibernate。所以你的选择是;
Employee
并插入新的OldEmployee
对象session.createSQLQuery("insert into oldEmployee values (employeeId, ....)")
正如您所看到的,这两种解决方案都是解决方法,也许重新检查您的继承甚至业务模型会更聪明。