每个子类映射的Hibernate表 我想知道,如果我们有两个类: 1) Employee.java 2) PermanentEmployee.java 现在,我想知道: 1.如果在Employee表中存在“id=1”的记录。 2.增量中使用的id生成器。
每个子类映射的Hibernate表每个子类映射的Hibernate表 我想知道,如果我们有两个类: 1) Employee.java 2) PermanentEmployee.java 现在,我想知道: 1.如果在Employee表中存在“id=1”的记录。 2.增量中使用的id生成器。 ,hibernate,Hibernate,每个子类映射的Hibernate表 我想知道,如果我们有两个类: 1) Employee.java 2) PermanentEmployee.java 现在,我想知道: 1.如果在Employee表中存在“id=1”的记录。 2.增量中使用的id生成器。 3.PermanentEmployee表中没有记录。 4.是否可以在PermanentEmployee表中插入id为1的记录 也就是说,如果我们保存“permanentEmployee”的记录,那么新记录应该在“permanentEmploye
我想知道,如果我们有两个类:
1) Employee.java
2) PermanentEmployee.java
现在,我想知道:
1.如果在Employee表中存在“id=1”的记录。
2.增量中使用的id生成器。
3.PermanentEmployee表中没有记录。
4.是否可以在PermanentEmployee表中插入id为1的记录
也就是说,如果我们保存“permanentEmployee”的记录,那么新记录应该在“permanentEmployee”表中插入,并且它应该引用Employee表中已经存在的记录?
任何指点都会大有帮助
更多说明:
Employee.java
----------------------------
包com.kar.hibernate;
公营雇员{
私有内部eid;
私有字符串名称;
私密性;
私人字符串电子邮件;
私人长耳机;
公职人员(){
超级();
}
公共雇员(字符串ename、字符串ecity、字符串eemail、长ephone){
超级();
this.ename=ename;
this.ecity=ecity;
this.eemail=eemail;
this.ephone=ephone;
}
公共int getEid(){
返回eid;
}
公共void setEid(int-eid){
this.eid=eid;
}
公共字符串getEname(){
返回ename;
}
public void setEname(字符串ename){
this.ename=ename;
}
公共字符串getEcity(){
返回精度;
}
公共无效集合属性(字符串属性){
this.ecity=ecity;
}
公共字符串getEemail(){
回复电子邮件;
}
公共void setEemail(字符串eemail){
this.eemail=eemail;
}
公共长途电话(){
回程耳机;
}
公共电话(长电话){
this.ephone=ephone;
}
@凌驾
公共字符串toString(){
return“Employee[eid=“+eid+”,ename=“+ename+”,ecity=“+ecity
+“,eemail=“+eemail+”,ephone=“+ephone+”]”;
}
}
-----------------------------------------------
OldEmployee.java
-----------------------------------------------
包com.kar.hibernate;
公共类OldEmployee扩展了Employee{
私人租赁;
私营国际公司;
公共雇员(){
}
public OldEmployee(字符串ename、字符串ecity、字符串eemail、长ephone)
,字符串oeDateOfRelease,内部oeDurationInCompany){
super(ename、ecity、eemail、ephone);
this.oeDateOfRelease=oeDateOfRelease;
此.oeDurationInCompany=oeDurationInCompany;
}
公共字符串getOeDateOfRelease(){
归还或归还日期;
}
public void setOeDateOfRelease(字符串oeDateOfRelease){
this.oeDateOfRelease=oeDateOfRelease;
}
public int getOeDurationInCompany(){
返回oeDurationInCompany;
}
公共无效setOeDurationInCompany(国际oeDurationInCompany){
this.oeDurationInCompany=oeDurationInCompany;
}
@凌驾
公共字符串toString(){
return super.toString()+”:::“+”OldEmployee[oEdateOfree=“+oEdateOfree
+“,oeDurationInCompany=“+oeDurationInCompany+”]”;
}
}
=================================================
Client1.java
=================================================
包com.kar.hibernate;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
导入org.hibernate.SessionFactory;
导入org.hibernate.Transaction;
导入org.hibernate.classic.Session;
公共类客户端1{
公共静态void main(字符串[]args){
事务tx=null;
尝试
{
SessionFactory SessionFactory=CHibernateUtil.getSessionFactory();
Session Session=sessionFactory.openSession();
tx=session.beginTransaction();
字符串ename=“anuj”;
字符串ecity=“blore”;
字符串eemail=”a@a";
长ephone=1111l;
员工e=新员工(ename、ecity、eemail、ephone);
会议.保存(e);
tx.commit();
session.close();
}
捕获(例外e)
{
e、 printStackTrace();
如果(tx!=null)
tx.回滚();
}
}
}
===================================================================
Client2.java
==========================================
包com.kar.hibernate;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
导入org.hibernate.SessionFactory;
导入org.hibernate.Transaction;
导入org.hibernate.classic.Session;
导入org.hibernate.event.SaveOrUpdateEvent;
公共类客户端2{
公共静态void main(字符串[]args){
事务tx=null;
尝试
{
SessionFactory SessionFactory=CHibernateUtil.getSessionFactory();
Session Session=sessionFactory.openSession();
tx=session.beginTransaction();
Employee e=(Employee)session.load(Employee.class,1);
OldEmployee oe=新的OldEmployee(如getEname(),
e、 getEcity(),e.getEemail(),
e、 getEphone(),“1/1/2014”,2);
会话.保存或更新(oe);
tx.commit();
session.close();
}
捕获(例外e)
{
e、 printStackTrace();
如果(tx!=null)
tx.回滚();
}
}
}
-------------------------------------------------------------------
Employee.hbm.xml
---------------------------------------------------------------------
====================================================================
Hibernate.cfg.xml
===================================================================
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/chm
根
根
org.hibernate.dialogue.mysqldialogue
更新
真的
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>