首页 > 代码库 > 映射继承
映射继承
每个具体类对应一个表
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="http://www.mamicode.com/li"></subclass> <subclass name="com.zhiqi.model.WorkImage" discriminator-value="http://www.mamicode.com/wi"></subclass> </class> </hibernate-mapping>
当子类非常多的时候,我们可以选择映射根。
映射继承
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。