映射继承

每个具体类对应一个表

package com.zhiqi.model;

import java.util.Set;

public class Employee {
	private int id;
	private String name;
	private Set<Image> imageSet;

	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Employee(String name) {
		super();
		this.name = name;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Image> getImageSet() {
		return imageSet;
	}
	public void setImageSet(Set<Image> imageSet) {
		this.imageSet = imageSet;
	}

}

  

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.zhiqi.model">
	<class name="Employee" table="t_employee">
		<id name="id" column="employee_id">
			<generator class="native"></generator>
		</id>

		<property name="name" column="emp_name"></property>

	</class>

</hibernate-mapping>

  Image类设为抽象的

package com.zhiqi.model;

public abstract class Image {
	private int id;
	private String imageName;
	private Employee emp;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getImageName() {
		return imageName;
	}
	public void setImageName(String imageName) {
		this.imageName = imageName;
	}
	public Employee getEmp() {
		return emp;
	}
	public void setEmp(Employee emp) {
		this.emp = emp;
	}
}

  

public class LifeImage extends Image {

}

  

public class WorkImage extends Image {

}

  分别配置xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.zhiqi.model">
	<class name="WorkImage" table="t_work_image">
		<id name="id" column="work_image_id">
			<generator class="native"></generator>
		</id>

		<property name="imageName" column="image_name"></property>
		<many-to-one name="emp" column="emp_id" class="com.zhiqi.model.Employee"></many-to-one>

	</class>

</hibernate-mapping>

  设置数据

@Test
	public void testSetDate(){
		Employee emp=new Employee("马化腾");

		LifeImage lifeImage1=new LifeImage();
		lifeImage1.setImageName("li001.jpg");
		LifeImage lifeImage2=new LifeImage();
		lifeImage2.setImageName("li002.jpg");

		WorkImage workImage1=new WorkImage();
		workImage1.setImageName("wi1.jpg");
		WorkImage workImage2=new WorkImage();
		workImage2.setImageName("wi2.jpg");

		lifeImage1.setEmp(emp);lifeImage2.setEmp(emp);
		workImage1.setEmp(emp);workImage2.setEmp(emp);
		session.save(lifeImage1);session.save(lifeImage2);
		session.save(workImage1);session.save(workImage2);
		session.save(emp);
	}

  

  获取数据

@Test
	public void testGetDate(){
		Employee emp=(Employee)session.get(Employee.class, Integer.valueOf(1));

		//第一种普通方法

//		Query q1=session.createQuery("from LifeImage l where l.emp.id="+emp.getId());
//		List<LifeImage> list1=q1.list();
//		Query q2=session.createQuery("from WorkImage w where w.emp.id="+emp.getId());
//		List<WorkImage> list2=q2.list();
//		for(LifeImage li:list1){
//			System.out.println(li.getImageName());
//		}
//		for(WorkImage wi:list2){
//			System.out.println(wi.getImageName());
//		}

		//第二种经验方法(集合就迭代器遍历)

		List<Image> imageList=new ArrayList<Image>();

		List<LifeImage> lifeImagelist=session.createQuery("from LifeImage l where l.emp.id="+emp.getId()).list();
		imageList.addAll(lifeImagelist);
		List<WorkImage> WorkImagelist=session.createQuery("from WorkImage w where w.emp.id="+emp.getId()).list();
		imageList.addAll(WorkImagelist);

		Iterator it=imageList.iterator();
		while(it.hasNext()){
			Image image=(Image)it.next();
			System.out.println(image.getImageName());
		}
	}

  根类对应一个表

<mapping resource="com/zhiqi/model/Employee.hbm.xml"/>
<mapping resource="com/zhiqi/model/Image.hbm.xml"/>

<hibernate-mapping package="com.zhiqi.model">
	<class name="Employee" table="t_employee">
		<id name="id" column="employee_id">
			<generator class="native"></generator>
		</id>

		<property name="name" column="emp_name"></property>

		<set name="imageSet">
			<key column="emp_id"></key>
			<one-to-many class="com.zhiqi.model.Image"></one-to-many>
		</set>
	</class>

</hibernate-mapping>

  

<hibernate-mapping package="com.zhiqi.model">
	<class name="Image" table="t_image">
		<id name="id" column="image_id">
			<generator class="native"></generator>
		</id>

		<discriminator column="image_type"></discriminator>

		<property name="imageName" column="image_name"></property>

		<many-to-one name="emp" column="emp_id"></many-to-one>
		<subclass name="com.zhiqi.model.LifeImage" discriminator-value="li"></subclass>
		<subclass name="com.zhiqi.model.WorkImage" discriminator-value="wi"></subclass>
	</class>

</hibernate-mapping>

  

当子类非常多的时候,我们可以选择映射根。

时间: 2024-12-06 12:50:00

映射继承的相关文章

Entity Framework Code First 映射继承关系

转载 http://www.th7.cn/Program/net/201301/122153.shtml Code First如何处理类之间的继承关系.Entity Framework Code First有三种处理类之间继承关系的方法,我们将逐一介绍这三种处理方法. 1.Table Per Hierarchy(TPH): 只建立一个表,把基类和子类中的所有属性都映射为表中的列. 2.Table Per Type(TPT): 为基类和每个子类建立一个表,每个与子类对应的表中只包含子类特有的属性对

[原创]java WEB学习笔记87:Hibernate学习之路-- -映射 继承关系(subclass , joined-subclass,union-subclass )

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------

框架 day33 Hibernate,组件映射,继承映射,抓取(检索)策略-优化,检索方式总结

组件(组成)映射 例1: public class Person { private Integer pid; //OID 值 private String name; //第一种方案 private String homeAddr; private String homeTel; private String companyAddr; private String companyTel; ↑一般项目都都采用此方案() *通过面向对象角度,使用设计模式(组件|组合),将数据都抽取到一个对象中.将

【译】第7节---映射继承策略

原文:http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx 我们在之前的部分看到,EF为每个具体的领域类创建数据库表.然而,你可以使用继承来设计域类.面向对象技术包括"has a"和"is a"关系,而基于SQL的关系模型在表之间只有一个"has a"关系. SQL数据库管理系统不支持类型继承.那么,将如何使用关系数据库映

(七)Hibernate 映射继承

所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:每个具体类对应一个表 Image.java 1 package com.wishwzp.model; 2 3 public abstract class Image { 4 5 private int id; 6 private String imageName; 7 private Student student; 8 9 public int getId()

hibernate注解映射继承关系

数据库表 用户表(User) id username password 客户表(Customer),客户表id引用用户表id id phone 员工表(Employee),员工表id引用用户表id id qq 飞机表(Plane),飞机表包含客机和战斗机,客机有厕所,战斗机有武器 id type speed wc_position weapon_position 实体映射 //子类共享属性id,version @MappedSuperclass public abstract class Ide

hibernate 映射继承关系

实现方式一般有三种: 1. 继承关系树每个具体类对应一张表(不介绍) 2. 继承关系树的根类对应一张表 3. 继承关系树的每个类对应一张表 先介绍关系: DayEmployee和MonthEmploy是Employee的子类,并且Company和Employee是一对多关系: 具体代码如下: Company.java import java.util.HashSet; import java.util.Set; public class Company { private Integer id;

Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)

1. 2. 1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 7 &l

Hibernate学习7—Hibernate 映射继承

需求:学生有很多照片,分为生活照和工作照: 第一节:每个具体类对应一个表 Student.java: package com.cy.model; import java.util.Set; public class Student { private int id; private String name; private Set<Image> images; public int getId() { return id; } public void setId(int id) { this.