所有项目导入对应的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. ltt.js

    var dailyBox = $('.daily-box-office'), curDate = new Date(), curYear = curDate.getFullYear(), curMon ...

  2. UI进阶 数据库 SQLite

    1.数据库管理系统 SQL:SQL是Structured Query Language(结构化查询语言)的缩写. SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言. 常见的数据库管理系 ...

  3. UIView的Touch事件UIControlEvents详解

    首先,UIControlEvents有这个几种: UIControlEventTouchDown = << , // on all touch downs UIControlEventTo ...

  4. ios中UIButton选中状态切换

    关于UIButton的事件枚举有许多,平时用的少所以很多的都不是很清楚,今天了解了下,看了以前的代码,觉得在UIButton选中时操作写了许多冗余代码,而忽略了UIButton一个很重要的属性,如下: ...

  5. android学习日记13--数据存储之SQLite

    2.SQLite 开源轻量级数据库,支持92-SQL标准,主要用于嵌入式系统,只占几百K系统资源此外,SQLite 不支持一些标准的 SQL 功能,特别是外键约束(FOREIGN KEY constr ...

  6. JavaScript(20)jQuery HTML 加入和删除元素

    jQuery - 加入元素 通过 jQuery,能够非常easy地加入新元素/内容. 加入新的 HTML 内容的四个 jQuery 方法: append() - 在被选元素的结尾插入内容 prepen ...

  7. BAPI_ACC_DOCUMENT_POST Enter rate / GBP rate type M for Error SG105

    Folks, I was wondering if I could get a bit of help here as I've been racking my brains on it for ag ...

  8. innobackupex --slave-info参数的含义和适用场景

    http://blog.itpub.net/28916011/viewspace-1969135/       我有个问题一直没弄明白,就是innobackupex里面的--slave-info这个参 ...

  9. Array.prototype.slice.call(arguments) 类数组转成真正的数组

    Array.prototype.slice.call(arguments)   我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...

  10. Lipo Error!! can't open input file

    参考文章:http://stackoverflow.com/questions/17348912/lipo-error-cant-open-input-file I got it to Work, i ...