- 浏览: 189788 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (321)
- eclipse (4)
- idea (2)
- Html (8)
- Css (14)
- Javascript (8)
- Jquery (6)
- Ajax Json (4)
- Bootstrap (0)
- EasyUI (0)
- Layui (0)
- 数据结构 (0)
- Java (46)
- DesPattern (24)
- Algorithm (2)
- Jdbc (8)
- Jsp servlet (13)
- Struts2 (17)
- Hibernate (11)
- Spring (5)
- S2SH (1)
- SpringMVC (4)
- SpringBoot (11)
- WebService CXF (4)
- Poi (2)
- JFreeChart (0)
- Shiro (6)
- Lucene (5)
- ElasticSearch (0)
- JMS ActiveMQ (3)
- HttpClient (5)
- Activiti (0)
- SpringCloud (11)
- Dubbo (6)
- Docker (0)
- MySQL (27)
- Oracle (18)
- Redis (5)
- Mybatis (11)
- SSM (1)
- CentOS (10)
- Ant (2)
- Maven (4)
- Log4j (7)
- XML (5)
最新评论
1. Hibernate映射继承
1.1) 每个具体类对应一个表
1.2) 根类对应一个表
1.3) 每个类对应一个表
新建项目Hibernate07
hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--数据库连接设置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate4</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL --> <property name="show_sql">true</property> <!-- 自动更新表结构 --> <property name="hbm2ddl.auto">update</property> <!-- Mapper --> <mapping resource="com/andrew/model/Student.hbm.xml"/> <mapping resource="com/andrew/model/LifeImage.hbm.xml"/> <mapping resource="com/andrew/model/WorkImage.hbm.xml"/> <mapping resource="com/andrew/model/Student2.hbm.xml"/> <mapping resource="com/andrew/model/Image2.hbm.xml"/> <mapping resource="com/andrew/model/Student3.hbm.xml"/> <mapping resource="com/andrew/model/Image3.hbm.xml"/> </session-factory> </hibernate-configuration> HibernateUtil.java package com.andrew.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { // 1. 实例化配置文件 Configuration configuration = new Configuration().configure(); // 2. 实例化服务登记 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); // 3. 获取Session工厂 return configuration.buildSessionFactory(serviceRegistry); } public static SessionFactory getSessionFactory() { return sessionFactory; } }
1.1) 每个具体类对应一个表
Image.java package com.andrew.model; public abstract class Image { private int id; private String imageName; private Student student; 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 Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } } LifeImage.java package com.andrew.model; public class LifeImage extends Image{ } LifeImage.hbm.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.andrew.model"> <class name="LifeImage" table="t_lifeImage"> <id name="id" column="lifeImageId"> <generator class="native"></generator> </id> <property name="imageName" column="imageName"></property> <many-to-one name="student" column="stuId" class="com.andrew.model.Student"></many-to-one> </class> </hibernate-mapping> WorkImage.java package com.andrew.model; public class WorkImage extends Image{ } WorkImage.hbm.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.andrew.model"> <class name="WorkImage" table="t_workImage"> <id name="id" column="workImageId"> <generator class="native"></generator> </id> <property name="imageName" column="imageName"></property> <many-to-one name="student" column="stuId" class="com.andrew.model.Student"></many-to-one> </class> </hibernate-mapping> Student.java package com.andrew.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.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Image> getImages() { return images; } public void setImages(Set<Image> images) { this.images = images; } } insert into t_student values(1, '张三'); insert into t_lifeimage values(1, 'image1.jpg', 1); insert into t_workimage values(1, 'image2.jpg', 1); StudentTest.java package com.andrew.service; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.andrew.model.Image; import com.andrew.model.Image2; import com.andrew.model.Image3; import com.andrew.model.Student2; import com.andrew.model.Student3; import com.andrew.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session=sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 } @After public void tearDown() throws Exception { session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } @Test public void testGetAllImages() { List<Image> imageList = new ArrayList<Image>(); int stuId = 1; List<Image> lifeImageList = (List<Image>)session.createQuery("from LifeImage l where l.student.id=" + stuId).list(); imageList.addAll(lifeImageList); List<Image> workImageList = (List<Image>)session.createQuery("from WorkImage w where w.student.id=" + stuId).list(); imageList.addAll(workImageList); Iterator it = imageList.iterator(); while (it.hasNext()) { Image image = (Image)it.next(); System.out.println(image.getImageName()); } } } 运行结果: Hibernate: select lifeimage0_.lifeImageId as lifeImag1_2_, lifeimage0_.imageName as imageNam2_2_, lifeimage0_.stuId as stuId3_2_ from t_lifeImage lifeimage0_ where lifeimage0_.stuId=1 Hibernate: select workimage0_.workImageId as workImag1_7_, workimage0_.imageName as imageNam2_7_, workimage0_.stuId as stuId3_7_ from t_workImage workimage0_ where workimage0_.stuId=1 image1.jpg image2.jpg
1.2) 根类对应一个表
Image2.java package com.andrew.model; public class Image2 { private int id; private String imageName; private String imageType; private Student2 student; 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 Student2 getStudent() { return student; } public void setStudent(Student2 student) { this.student = student; } public String getImageType() { return imageType; } public void setImageType(String imageType) { this.imageType = imageType; } } Image2.hbm.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.andrew.model"> <class name="Image2" table="t_image2"> <id name="id" column="imageId"> <generator class="native"></generator> </id> <discriminator column="imageType" type="string"></discriminator> <property name="imageName" column="imageName"></property> <many-to-one name="student" column="stuId" class="com.andrew.model.Student2"></many-to-one> <subclass name="com.andrew.model.LifeImage2" discriminator-value="li"></subclass> <subclass name="com.andrew.model.WorkImage2" discriminator-value="wi"></subclass> </class> </hibernate-mapping> LifeImage2.java package com.andrew.model; public class LifeImage2 extends Image2 { } WorkImage2.java package com.andrew.model; public class WorkImage2 extends Image2{ } Student2.java package com.andrew.model; import java.util.Set; public class Student2 { private int id; private String name; private Set<Image2> images; 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<Image2> getImages() { return images; } public void setImages(Set<Image2> images) { this.images = images; } } Student2.hbm.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.andrew.model"> <class name="Student2" table="t_student2"> <id name="id" column="stuId"> <generator class="native"></generator> </id> <property name="name" column="stuName"></property> <set name="images"> <key column="stuId"></key> <one-to-many class="com.andrew.model.Image2"/> </set> </class> </hibernate-mapping> insert into t_student2 values(1, '张三'); insert into t_image2 values(1, 'li', 'image1.jpg', 1); insert into t_image2 values(2, 'wi', 'image2.jpg', 1); StudentTest.java package com.andrew.service; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.andrew.model.Image; import com.andrew.model.Image2; import com.andrew.model.Image3; import com.andrew.model.Student2; import com.andrew.model.Student3; import com.andrew.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session=sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 } @After public void tearDown() throws Exception { session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } @Test public void testGetAllImages2() { Student2 student2 = (Student2)session.get(Student2.class, 1); Set<Image2> images = student2.getImages(); Iterator it = images.iterator(); while (it.hasNext()) { Image2 image = (Image2)it.next(); System.out.println(image.getImageName()); } } } 运行结果: Hibernate: select student2x0_.stuId as stuId1_5_0_, student2x0_.stuName as stuName2_5_0_ from t_student2 student2x0_ where student2x0_.stuId=? Hibernate: select images0_.stuId as stuId4_5_0_, images0_.imageId as imageId1_0_0_, images0_.imageId as imageId1_0_1_, images0_.imageName as imageNam3_0_1_, images0_.stuId as stuId4_0_1_, images0_.imageType as imageTyp2_0_1_ from t_image2 images0_ where images0_.stuId=? image2.jpg image1.jpg
1.3) 每个类对应一个表
Image3.java package com.andrew.model; public class Image3 { private int id; private String imageName; private Student3 student; 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 Student3 getStudent() { return student; } public void setStudent(Student3 student) { this.student = student; } } Image3.hbm.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.andrew.model"> <class name="Image3" table="t_image3"> <id name="id" column="imageId"> <generator class="native"></generator> </id> <property name="imageName" column="imageName"></property> <many-to-one name="student" column="stuId" class="com.andrew.model.Student3"></many-to-one> <joined-subclass name="com.andrew.model.LifeImage3" table="t_lifeImage3"> <key column="lifeImageId"></key> </joined-subclass> <joined-subclass name="com.andrew.model.WorkImage3" table="t_workImage3"> <key column="workImageId"></key> </joined-subclass> </class> </hibernate-mapping> LifeImage2.java package com.andrew.model; public class LifeImage3 extends Image3{ } WorkImage3.java package com.andrew.model; public class WorkImage3 extends Image3{ } Student3.java package com.andrew.model; import java.util.Set; public class Student3 { private int id; private String name; private Set<Image3> images; 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<Image3> getImages() { return images; } public void setImages(Set<Image3> images) { this.images = images; } } Student3.hbm.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.andrew.model"> <class name="Student3" table="t_student3"> <id name="id" column="stuId"> <generator class="native"></generator> </id> <property name="name" column="stuName"></property> <set name="images"> <key column="stuId"></key> <one-to-many class="com.andrew.model.Image3"/> </set> </class> </hibernate-mapping> insert into t_student3 values(1, '张三'); insert into t_image3 values(1, 'image11.jpg', 1); insert into t_image3 values(2, 'image22.jpg', 1); insert into t_lifeimage3 values(1); insert into t_workimage3 values(2); StudentTest.java package com.andrew.service; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.andrew.model.Image; import com.andrew.model.Image2; import com.andrew.model.Image3; import com.andrew.model.Student2; import com.andrew.model.Student3; import com.andrew.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session=sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 } @After public void tearDown() throws Exception { session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } @Test public void testGetAllImages3() { Student3 student3 = (Student3)session.get(Student3.class, 1); Set<Image3> images = student3.getImages(); Iterator it = images.iterator(); while (it.hasNext()) { Image3 image = (Image3)it.next(); System.out.println(image.getImageName()); } } } 运行结果: Hibernate: select student3x0_.stuId as stuId1_6_0_, student3x0_.stuName as stuName2_6_0_ from t_student3 student3x0_ where student3x0_.stuId=? Hibernate: select images0_.stuId as stuId3_6_0_, images0_.imageId as imageId1_1_0_, images0_.imageId as imageId1_1_1_, images0_.imageName as imageNam2_1_1_, images0_.stuId as stuId3_1_1_, case when images0_1_.lifeImageId is not null then 1 when images0_2_.workImageId is not null then 2 when images0_.imageId is not null then 0 end as clazz_1_ from t_image3 images0_ left outer join t_lifeImage3 images0_1_ on images0_.imageId=images0_1_.lifeImageId left outer join t_workImage3 images0_2_ on images0_.imageId=images0_2_.workImageId where images0_.stuId=? image22.jpg image11.jpg
发表评论
-
Hibernate高级配置
2019-01-16 08:52 3531. 配置数据库连接池 反问数据库,需要不断的创建和释放 ... -
Hibernate查询方式
2019-01-16 08:52 3961) Hibernate查询方式简介 新建项目Hiber ... -
Hibernate检索策略
2019-01-15 14:16 3721. Hibernate检索策略 新建项目Hiberna ... -
Hibernate映射关系
2019-01-15 14:16 4361. Hibernate映射关系 新建项目Hiberna ... -
Hibernate映射类型
2019-01-15 08:54 3841. 基本类型映射 Hibernat ... -
Hibernate操作对象
2019-01-15 08:54 3281. Hibernate中四种对象状态 临时状态(tra ... -
关联关系一对多映射
2019-01-14 09:01 3991. Junit4方法详解 setUpBeforeCla ... -
映射对象标识符(OID)
2019-01-14 09:01 4491. Hibernate用对象标识符(OID)来区分对象 ... -
Hibernate4的CRUD
2019-01-14 09:01 4151. Hibernate4的CRUD 新建项目Hiber ... -
Hibernate简介
2019-01-14 08:55 3611. Hibernate简介 Hibernate官网:htt ...
相关推荐
### 用Hibernate映射继承关系 #### 14.1 继承关系树的每个具体类对应一个表 在Hibernate中映射继承关系时,一种常见的策略是将继承关系树的每个具体类映射到单独的数据库表中。这种方法称为**表/类映射**(Table/...
《Hibernate映射继承实战解析》 在Java世界中,Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了数据库操作。它允许开发者用面向对象的方式处理数据,而无需关心底层SQL语句的编写。在大型项目中,...
在Java的ORM框架Hibernate中,映射继承关系是常见的需求,尤其在处理具有层次结构的实体类时。本文将详细探讨Hibernate中处理继承关系的三种映射策略:subclass、joined-subclass以及union-subclass。 首先,让我们...
Hibernate,作为Java中广泛使用的对象关系映射(ORM)框架,提供了多种方式来处理继承关系的映射,其中一种就是"每个类都对应一张表"的策略。本文将深入探讨这种映射方式,以及如何在Hibernate中实现它。 首先,...
Hibernate继承映射是将Java类的继承关系映射到数据库表的一种策略,使得对象模型的复杂性能够平滑地转化为关系数据库模型。本篇将详细介绍Hibernate继承映射的第一种策略——每棵类继承树对应一张表,即单一表继承...
在大型项目中,由于业务需求复杂,我们常常会使用到类的继承来组织代码结构,而Hibernate提供了对这些继承关系的映射支持。本主题将深入探讨"Hibernate继承映射+C3P0代码"的相关知识点。 首先,让我们理解Hibernate...
【压缩包子文件的文件名称列表】:“hibernate映射文件自动生成.doc”很可能是这篇博文的文档形式,其中详细介绍了具体的操作步骤、使用工具或者自定义脚本,以帮助开发者理解并应用自动化的映射文件生成过程。...
### 常用Hibernate映射配置详解 #### 1. hibernate-mapping节点解析 `hibernate-mapping`节点是Hibernate映射文件中的顶级节点,用于定义一系列配置选项,控制整个映射文件的行为和映射规则。这些配置包括数据库...
Hibernate的继承映射 --每个继承层次一张表 -- 每个具体类一张表 -- 每个类一张表
《Hibernate继承映射详解》 Hibernate,作为Java领域中的一款著名对象关系映射(ORM)框架,极大地简化了数据库操作。在实际项目中,我们常常会遇到类的继承关系,而Hibernate提供了强大的支持来处理这种继承关系的...
针对hibernate的继承映射部分做了几个测试的例子,更加直观的阐述了继承映射的几种方式以及对应的表结构方式,文件被分割成3部分 http://download.csdn.net/source/259075 http://download.csdn.net/source/259072 ...
### Hibernate的继承多态映射关系详解 在软件开发领域,特别是使用面向对象编程语言如Java进行开发时,我们经常需要处理复杂的类继承关系。在ORM(Object-Relational Mapping)框架如Hibernate中,如何优雅地将这些...
针对hibernate的继承映射部分做了几个测试的例子,更加直观的阐述了继承映射的几种方式以及对应的表结构方式,文件被分割成3部分 http://download.csdn.net/source/259075 http://download.csdn.net/source/259072 ...
《Hibernate继承映射详解》 在Java开发中,对象关系映射(ORM)框架如Hibernate大大简化了数据库操作。Hibernate不仅提供了对基本数据类型的映射,还支持复杂的数据结构,如继承关系的映射。本篇文章将深入探讨...
本文将深入探讨“hibernate关联映射实例”中的关键知识点,包括一对多、多对多和继承映射,这些都是Hibernate中至关重要的概念。 1. **一对多关联映射**: 在现实世界中,一个实体可能会与多个其他实体相关联,...
【标题】:“Hibernate继承映射(annotation)” 在Java对象关系映射(ORM)框架中,Hibernate是一个非常重要的工具,它允许开发人员将Java类映射到数据库表,从而简化了数据库操作。本主题主要探讨的是Hibernate中的...