hibernate中继承映射

继承映射:

1)每个类一张表。

package model;

public class Person {
	private String id;
	
	private String name;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}


package model;

public class Student extends Person{
	
	private String idCard;

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	
	
}

package model;

public class Teacher  extends Person{
	private int salary;
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
}

<?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>
	<class name = "model.Student" table = "student" lazy="true">
		<id name="id" column="id" type="string">
			<generator class="uuid"></generator>
		</id>
		<property name="name" column = "name" type = "string"></property>
		<property name="idCard" column = "idCard" type = "string"></property>
	</class>
</hibernate-mapping>

<?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>
	<class name = "model.Teacher" table = "teacher" lazy="true">
		<id name="id" column="id" type="string">
			<generator class="uuid"></generator>
		</id>
		<property name="name" column = "name" type = "string"></property>
		<!-- 
		虽然Person中的id属性和name属性是private,当是它们的存储方法被Teacher类继承下来了,hibernate只要根据get和set方法来存取即可。
		-->
		<property name="salary" column = "salary" type = "integer"></property>
	</class>
</hibernate-mapping>

package model;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/*
 * 
 * create table student (id varchar(255) not null, name varchar(255), idCard varchar(255), primary key (id))
create table teacher (id varchar(255) not null, name varchar(255), salary integer, primary key (id))
 */
public class Create {
	public static void main(String[] args) {
		SchemaExport export = new SchemaExport(new Configuration().configure());
		export.create(true, true);
	}
}

package model;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Test {
	public static void main(String[] args) {
		//save();
		fetch();
	}
	
	/**
	 * 
Hibernate: insert into student (name, idCard, id) values (?, ?, ?)
Hibernate: insert into teacher (name, salary, id) values (?, ?, ?)
	 */
	static void save () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
				
			Student student = new Student ();
			student.setIdCard("12334");
			student.setName("zhanglei");
			
			Teacher teacher = new Teacher ();
			teacher.setSalary(100);
			
			teacher.setName("lisi");
			
			session.save(student);
			
			session.save(teacher);
			
			
			tx.commit();
		}catch (Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
	
	static void fetch () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from model.Person");//多态查询
			Iterator it = query.list().iterator() ;
			while (it.hasNext()) {
				Person person = (Person)it.next();
				System.out.println(person.getName());
			}
			tx.commit();
		}catch (Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
}

Hibernate: select teacher0_.id as id1_, teacher0_.name as name1_, teacher0_.salary as salary1_ from teacher teacher0_
Hibernate: select student0_.id as id0_, student0_.name as name0_, student0_.idCard as idCard0_ from student student0_
lisi
zhanglei



2)一张表存储继承体系中所有类的信息(数据库表实际上是继承体系中所有类的属性并集所构成的字段)

需要在HBM文件中增加如下一行。

<discriminator column = "personType" type = "string" length="10"></discriminator>

Student类,Person类,Teacher类同上:


<?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>
	<class name = "model.Person" table = "Person" lazy="true">
		<id name="id" column="id" type="string" length="50">
			<generator class="uuid"></generator>
		</id>
		<discriminator column = "personType" type = "string" length="10"></discriminator>
		<property name="name" column = "name" type = "string"></property>
		<subclass name = "model.Student" discriminator-value="student">
			<property name="idCard" type = "string"></property>
		</subclass>
		<subclass name="model.Teacher" discriminator-value="teacher">
			<property name="salary" type = "int"></property>
		</subclass>
	</class>
</hibernate-mapping>

package model;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/*
 * 
 * create table Person (id varchar(255) not null, personType varchar(255) not null, name varchar(255), idCard varchar(255), salary integer, primary key (id))
 */
public class Create {
	public static void main(String[] args) {
		SchemaExport export = new SchemaExport(new Configuration().configure());
		export.create(true, true);
	}
}

package model;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Test {
	public static void main(String[] args) {
		//save();
		//fetch();
		fetch3();
	}
	
	/**
	 * 
Hibernate: insert into Person (name, idCard, personType, id) values (?, ?, 'student', ?)
Hibernate: insert into Person (name, salary, personType, id) values (?, ?, 'teacher', ?)
	 */
	static void save () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
				
			Student student = new Student ();
			student.setIdCard("12334");
			student.setName("zhanglei");
			
			Teacher teacher = new Teacher ();
			teacher.setSalary(100);
			
			teacher.setName("lisi");
			
			session.save(student);
			
			session.save(teacher);
			
			
			tx.commit();
		}catch (Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
	/*
	 * 
	 * Hibernate: select person0_.id as col_0_0_ from Person person0_
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.idCard as idCard0_0_, person0_.salary as salary0_0_, person0_.personType as personType0_0_ from Person person0_ where person0_.id=?
zhanglei
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.idCard as idCard0_0_, person0_.salary as salary0_0_, person0_.personType as personType0_0_ from Person person0_ where person0_.id=?
lisi
	 */
	static void fetch () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from model.Person");//多态查询
			Iterator it = query.iterate() ;
			while (it.hasNext()) {
				Person person = (Person)it.next();
				System.out.println(person.getName());
			}
			tx.commit();
		}catch (Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
	/**
	 * Hibernate: select person0_.id as col_0_0_ from Person person0_
model.Person_$$_javassist_0
model.Person_$$_javassist_0
	 */
	static void fetch2 () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from Person");//多态查询
			Iterator it = query.iterate() ;
			while (it.hasNext()) {
				Object object = (Person)it.next();
				System.out.println(object.getClass().getName());
				/*
				 原因在于System.out.println(object.getClass().getName());model.Person_$$_javassist_0
				if (object instanceof Student) {
					System.out.println(((Student)object).getIdCard());
				}
				if (object.getClass()==  Student.class) {
					System.out.println(((Student)object).getIdCard());
				}*/
				
			}
			tx.commit();
		}catch (Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
	/*
	 * Hibernate: select person0_.id as id0_, person0_.name as name0_, person0_.idCard as idCard0_, person0_.salary as salary0_, person0_.personType as personType0_ from Person person0_
	   12334
	 */
	static void fetch3 () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from Person");//多态查询
			Iterator it = query.list().iterator();
			while (it.hasNext()) {
				Object object = (Person)it.next();
				if (object instanceof Student) {
					Student student = (Student)object;
					System.out.println(student.getIdCard());
				}
			}
			tx.commit();
		}catch (Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
}
3:公共信息放在父类表中,独有信息放在子类表中,每个子类对应一张表。



<?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>
	<class name = "model.Person" table = "person" lazy="true">
		<id name="id" column="id" type="string">
			<generator class="uuid"></generator>
		</id>
		<property name="name" type = "string"></property>
		<joined-subclass name="model.Student" table="student">
			<key column = "id"></key>
			<property name="idCard" type = "string"></property>
		</joined-subclass>
		
		<joined-subclass name="model.Teacher" table="teacher">
			<key column = "id"></key>
			<property name="salary" type = "int"></property>
		</joined-subclass>
	</class>
</hibernate-mapping>

package model;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/*
create table person (id varchar(255) not null, name varchar(255), primary key (id))
create table student (id varchar(255) not null, idCard varchar(255), primary key (id))
create table teacher (id varchar(255) not null, salary integer, primary key (id))
alter table student add index FK8FFE823BA4230EF5 (id), add constraint FK8FFE823BA4230EF5 foreign key (id) references person (id)
alter table teacher add index FKAA31CBE2A4230EF5 (id), add constraint FKAA31CBE2A4230EF5 foreign key (id) references person (id)

 */
public class Create {
	public static void main(String[] args) {
		SchemaExport export = new SchemaExport(new Configuration().configure());
		export.create(true, true);
	}
}
package model;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Test {
	public static void main(String[] args) {
		//save();
		fetch();
	}
	/*	 
Hibernate: insert into person (name, id) values (?, ?)
Hibernate: insert into student (idCard, id) values (?, ?)
Hibernate: insert into person (name, id) values (?, ?)
Hibernate: insert into teacher (salary, id) values (?, ?)
	 */
	static void save () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
		
			
			Student student = new Student ();
			student.setIdCard("12334");
			student.setName("zhanglei");
			
			Teacher teacher = new Teacher ();
			teacher.setSalary(100);
			
			teacher.setName("lisi");
			
			session.save(student);
			
			session.save(teacher);
			
			tx.commit();
		}catch (Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
	/**
Hibernate: select person0_.id as col_0_0_ from person person0_
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_1_.idCard as idCard1_0_, person0_2_.salary as salary2_0_, case when person0_1_.id is not null then 1 when person0_2_.id is not null then 2 when person0_.id is not null then 0 end as clazz_0_ from person person0_ left outer join student person0_1_ on person0_.id=person0_1_.id left outer join teacher person0_2_ on person0_.id=person0_2_.id where person0_.id=?
zhanglei
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_1_.idCard as idCard1_0_, person0_2_.salary as salary2_0_, case when person0_1_.id is not null then 1 when person0_2_.id is not null then 2 when person0_.id is not null then 0 end as clazz_0_ from person person0_ left outer join student person0_1_ on person0_.id=person0_1_.id left outer join teacher person0_2_ on person0_.id=person0_2_.id where person0_.id=?
lisi
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_1_.idCard as idCard1_0_, person0_2_.salary as salary2_0_, case when person0_1_.id is not null then 1 when person0_2_.id is not null then 2 when person0_.id is not null then 0 end as clazz_0_ from person person0_ left outer join student person0_1_ on person0_.id=person0_1_.id left outer join teacher person0_2_ on person0_.id=person0_2_.id where person0_.id=?
zhanglei
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_1_.idCard as idCard1_0_, person0_2_.salary as salary2_0_, case when person0_1_.id is not null then 1 when person0_2_.id is not null then 2 when person0_.id is not null then 0 end as clazz_0_ from person person0_ left outer join student person0_1_ on person0_.id=person0_1_.id left outer join teacher person0_2_ on person0_.id=person0_2_.id where person0_.id=?
lisi
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_1_.idCard as idCard1_0_, person0_2_.salary as salary2_0_, case when person0_1_.id is not null then 1 when person0_2_.id is not null then 2 when person0_.id is not null then 0 end as clazz_0_ from person person0_ left outer join student person0_1_ on person0_.id=person0_1_.id left outer join teacher person0_2_ on person0_.id=person0_2_.id where person0_.id=?
baother
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_1_.idCard as idCard1_0_, person0_2_.salary as salary2_0_, case when person0_1_.id is not null then 1 when person0_2_.id is not null then 2 when person0_.id is not null then 0 end as clazz_0_ from person person0_ left outer join student person0_1_ on person0_.id=person0_1_.id left outer join teacher person0_2_ on person0_.id=person0_2_.id where person0_.id=?
wokao
	 * 
	 */
	static void fetch () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from model.Person");//多态查询
			Iterator it = query.iterate();
			while (it.hasNext()) {
				Person person = (Person)it.next();
				System.out.println(person.getName());
			}
			tx.commit();
		}catch (Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值