所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java

第一节:每个具体类对应一个表

Image.java

 package com.wishwzp.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;
}
}

WorkImage.java

 package com.wishwzp.model;

 public class WorkImage extends Image{

 }

LifeImage.java

 package com.wishwzp.model;

 public class LifeImage extends Image{

 }

Student.java

 package com.wishwzp.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;
}
}

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/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/Student.hbm.xml"/>
<mapping resource="com/wishwzp/model/LifeImage.hbm.xml"/>
<mapping resource="com/wishwzp/model/WorkImage.hbm.xml"/> </session-factory> </hibernate-configuration>

StudentTest.java

 package com.wishwzp.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.wishwzp.model.Image;
import com.wishwzp.model.Image2;
import com.wishwzp.model.Image3;
import com.wishwzp.model.Student2;
import com.wishwzp.model.Student3;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testGetAllImages(){ }
}

WorkImage.java和LifeImage.java继承了 Image.java,我们在数据库里面可以看出来的。

这里我们在向数据库里面插入一些数据。。。

向t_student表中插入stuName为张三:

向t_lifeimage表中插入imageName:

向t_workimage表中插入imageName:

然后我们查询这些数据:

StudentTest.java

 package com.wishwzp.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.wishwzp.model.Image;
import com.wishwzp.model.Image2;
import com.wishwzp.model.Image3;
import com.wishwzp.model.Student2;
import com.wishwzp.model.Student3;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭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());
}
}
}

控制台显示:

第二节:根类对应一个表

image2.java

 package com.wishwzp.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;
}
}

LifeImage2.java

 package com.wishwzp.model;

 public class LifeImage2 extends Image2{

 }

WorkImage2.java

 package com.wishwzp.model;

 public class WorkImage2 extends Image2{

 }

Student2.java

 package com.wishwzp.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;
} }

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.wishwzp.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.wishwzp.model.Student2"></many-to-one> <subclass name="com.wishwzp.model.LifeImage2" discriminator-value="li"></subclass>
<subclass name="com.wishwzp.model.WorkImage2" discriminator-value="wi"></subclass>
</class> </hibernate-mapping>

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.wishwzp.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.wishwzp.model.Image2"/>
</set>
</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://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/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/Student2.hbm.xml"/>
<mapping resource="com/wishwzp/model/Image2.hbm.xml"/> </session-factory> </hibernate-configuration>

StudentTest.java

 package com.wishwzp.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.wishwzp.model.Image;
import com.wishwzp.model.Image2;
import com.wishwzp.model.Image3;
import com.wishwzp.model.Student2;
import com.wishwzp.model.Student3;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testGetAllImages2(){ }
}

插入数据:

然后查询这些数据:

StudentTest.java

 package com.wishwzp.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.wishwzp.model.Image;
import com.wishwzp.model.Image2;
import com.wishwzp.model.Image3;
import com.wishwzp.model.Student2;
import com.wishwzp.model.Student3;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭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());
}
}
}

控制台显示:

第三节:每个类对应一个表

Student3.java

 package com.wishwzp.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;
} }

Image3.java

 package com.wishwzp.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;
} }

LifeImage3.java

 package com.wishwzp.model;

 public class LifeImage3 extends Image3{

 }

WorkImage3.java

 package com.wishwzp.model;

 public class WorkImage3 extends Image3{

 }

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.wishwzp.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.wishwzp.model.Image3"/>
</set>
</class> </hibernate-mapping>

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.wishwzp.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.wishwzp.model.Student3"></many-to-one> <joined-subclass name="com.wishwzp.model.LifeImage3" table="t_lifeImage3">
<key column="lifeImageId"></key>
</joined-subclass> <joined-subclass name="com.wishwzp.model.WorkImage3" table="t_workImage3">
<key column="workImageId"></key>
</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://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/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/Student3.hbm.xml"/>
<mapping resource="com/wishwzp/model/Image3.hbm.xml"/> </session-factory> </hibernate-configuration>

StudentTest.java

 package com.wishwzp.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.wishwzp.model.Image;
import com.wishwzp.model.Image2;
import com.wishwzp.model.Image3;
import com.wishwzp.model.Student2;
import com.wishwzp.model.Student3;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testGetAllImages3(){ }
}

插入一些数据:

然后查询这些数据:

StudentTest.java

 package com.wishwzp.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.wishwzp.model.Image;
import com.wishwzp.model.Image2;
import com.wishwzp.model.Image3;
import com.wishwzp.model.Student2;
import com.wishwzp.model.Student3;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭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 映射继承的更多相关文章

  1. hibernate映射-继承映射

    对于面向对象的程序设计语言而言,继承和多态是两个最基本的概念.Hibernate的继承映射可以理解成持久化类之间的继承关系.例如:人和学生之间的关系.学生继承人,可以认为学生是一个特殊的人,如果对人进 ...

  2. 【SSH系列】Hibernate映射 -- 继承映射

    开篇前言 在前面的博文中,小编介绍了hibernate中的映射,一对一,一对多,多对多,单向,双向等,今天这篇博文,小编主要来介绍一下hibernate中的继承映射,小伙伴都知道在C#中,如果想要实现 ...

  3. Hibernate学习7—Hibernate 映射继承

    需求:学生有很多照片,分为生活照和工作照: 第一节:每个具体类对应一个表 Student.java: package com.cy.model; import java.util.Set; publi ...

  4. hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系

    hibernate环境配置:导包.... 单向n-1:单向 n-1 关联只需从 n 的一端可以访问 1 的一端 <many-to-one> 元素来映射组成关系: name: 设定待映射的持 ...

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

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

  6. Hibernate映射解析——七种映射关系

    首先我们了解一个名词ORM,全称是(Object Relational Mapping),即对象关系映射.ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现,这样开发人员就可以把对 ...

  7. Hibernate学习之路-- -映射 继承关系(subclass , joined-subclass,union-subclass )

    1.继承映射 举例:对于面向对象的程序设计语言而言,继承和多态是两个最基本的概念.Hibernate 的继承映射可以理解持久化类之间的继承关系.例如:人和学生之间的关系.学生继承了人,可以认为学生是一 ...

  8. SSH深度历险(一)深入浅出Hibernate架构(一)-------映射解析——七种映射关系

            ORM,全称是(Object Relational Mapping),即对象关系映射.ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现,这样开发人员就可以把对数据 ...

  9. Hibernate的七种映射关系之七种关联映射(二)

    继续上篇博客 七.Hibernate双向一对多关联映射:让多的一端来维护关系. 主要是解决一对多单向关联的缺陷,而不是需求驱动的. 1.在Student.java实体类里添加Classes引用.pri ...

随机推荐

  1. UITableView性能优化

    关于UITableView的性能优化,网络上也有一些总结.在这里就介绍下我们项目中遇到的问题以及对应的解决方法.相信我们遇到的问题也有一定的普适性,能够作为其他问题的优化方案. Instruments ...

  2. Notepad++的一些常用的快捷键

    ctrl+/  一个一个单词的往后跳Ctrl+H 替换Ctrl+F  查找Ctrl+D 复制当前行到下一行Ctrl+L 删除当前行Ctrl+Shift+F 在文件中找F5 打开run对话框F11 全屏 ...

  3. openstack系列阅读目录

    一 keystone系列 keystone基础 网关协议cgi,fastcgi,wsgi,uwsgi keystone源码分析 keystone部署及使用 keystone客户端工作介绍 二 glan ...

  4. java Swing图形化界面

    学过java的人应该对java的图形化界面很是反感,特别是接触java不久的人.如果想和其他语言那样用鼠标拖拽,可以使用wondosbulider插件.但是用起来也不是那么方便.当然对于不乐意写代码的 ...

  5. C#通过OLEDB导出大数据到Excel

    C#导出数据到Excel,基本有两种方法,一种方法是通过Microsoft.Office.Interop.Excel.Application,一行一列的写入Excel中:另一种方法是通过OLEDB,利 ...

  6. hiberante中get和load方法的区别

    1.从返回结果上对比: load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常 get方法检索不到的话会返回null 2.从检索执行机制上对比: ...

  7. USB移动硬盘WinPE启动盘的制作方法

    USB移动硬盘WinPE启动盘的制作方法 软件:老九WinPE 老毛桃终于撒手无论版 发行时间:2007年9月11日 制作发行:老毛桃 作用:当系统坏了,无法进入时,用来做系统维护,备份文件.轻巧稳定 ...

  8. Linux开发环境搭建与使用——ubuntu更新设置

    ubuntu操作系统公布时,为了减小操作系统的体积,只配备了主要的系统软件.应用软件.我们开发中须要用到的大部分软件都须要在使用中从网上自行更新. 假设ubuntu没有网络,能够说寸步难行. 以下教大 ...

  9. [置顶] Java编程笔试题之一 ----文件操作

    题目:给定一个文件和一个字符串,判断文件是否包含该字符串,如果包含,请打印出包含该字符串的行号以及该行的全部内容. 思路: ①使用缓冲流(BufferedReader)读取文件,定义初始行号为0.   ...

  10. 04---XML编程整理

    一.XML概述       XML(eXtensible Markup Language),可扩展标记语言,       被设计的宗旨是传输数据,而非显示数据       W3C发布的,目前遵循1.0 ...