所有项目导入对应的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() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getImageName() { 16 return imageName; 17 } 18 public void setImageName(String imageName) { 19 this.imageName = imageName; 20 } 21 public Student getStudent() { 22 return student; 23 } 24 public void setStudent(Student student) { 25 this.student = student; 26 } 27 }
WorkImage.java
1 package com.wishwzp.model; 2 3 public class WorkImage extends Image{ 4 5 }
LifeImage.java
1 package com.wishwzp.model; 2 3 public class LifeImage extends Image{ 4 5 }
Student.java
1 package com.wishwzp.model; 2 3 import java.util.Set; 4 5 public class Student { 6 7 private int id; 8 private String name; 9 private Set<Image> images; 10 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public Set<Image> getImages() { 24 return images; 25 } 26 public void setImages(Set<Image> images) { 27 this.images = images; 28 } 29 }
hibernate.cfg.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 8 <session-factory> 9 10 <!--数据库连接设置 --> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">123456</property> 15 16 17 <!-- 方言 --> 18 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 19 20 <!-- 控制台显示SQL --> 21 <property name="show_sql">true</property> 22 23 <!-- 自动更新表结构 --> 24 <property name="hbm2ddl.auto">update</property> 25 26 <mapping resource="com/wishwzp/model/Student.hbm.xml"/> 27 <mapping resource="com/wishwzp/model/LifeImage.hbm.xml"/> 28 <mapping resource="com/wishwzp/model/WorkImage.hbm.xml"/> 29 30 </session-factory> 31 32 </hibernate-configuration>
StudentTest.java
1 package com.wishwzp.service; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Set; 7 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.junit.After; 11 import org.junit.Before; 12 import org.junit.Test; 13 14 import com.wishwzp.model.Image; 15 import com.wishwzp.model.Image2; 16 import com.wishwzp.model.Image3; 17 import com.wishwzp.model.Student2; 18 import com.wishwzp.model.Student3; 19 import com.wishwzp.util.HibernateUtil; 20 21 public class StudentTest { 22 23 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 24 private Session session; 25 26 @Before 27 public void setUp() throws Exception { 28 session=sessionFactory.openSession(); // 生成一个session 29 session.beginTransaction(); // 开启事务 30 } 31 32 @After 33 public void tearDown() throws Exception { 34 session.getTransaction().commit(); // 提交事务 35 session.close(); // 关闭session 36 } 37 38 @Test 39 public void testGetAllImages(){ 40 41 } 42 }
WorkImage.java和LifeImage.java继承了 Image.java,我们在数据库里面可以看出来的。
这里我们在向数据库里面插入一些数据。。。
向t_student表中插入stuName为张三:
向t_lifeimage表中插入imageName:
向t_workimage表中插入imageName:
然后我们查询这些数据:
StudentTest.java
1 package com.wishwzp.service; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Set; 7 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.junit.After; 11 import org.junit.Before; 12 import org.junit.Test; 13 14 import com.wishwzp.model.Image; 15 import com.wishwzp.model.Image2; 16 import com.wishwzp.model.Image3; 17 import com.wishwzp.model.Student2; 18 import com.wishwzp.model.Student3; 19 import com.wishwzp.util.HibernateUtil; 20 21 public class StudentTest { 22 23 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 24 private Session session; 25 26 @Before 27 public void setUp() throws Exception { 28 session=sessionFactory.openSession(); // 生成一个session 29 session.beginTransaction(); // 开启事务 30 } 31 32 @After 33 public void tearDown() throws Exception { 34 session.getTransaction().commit(); // 提交事务 35 session.close(); // 关闭session 36 } 37 38 @Test 39 public void testGetAllImages(){ 40 List<Image> imageList=new ArrayList<Image>(); 41 int stuId=1; 42 List<Image> lifeImageList=(List<Image>)session.createQuery("from LifeImage l where l.student.id="+stuId).list(); 43 imageList.addAll(lifeImageList); 44 List<Image> workImageList=(List<Image>)session.createQuery("from WorkImage w where w.student.id="+stuId).list(); 45 imageList.addAll(workImageList); 46 Iterator it=imageList.iterator(); 47 while(it.hasNext()){ 48 Image image=(Image)it.next(); 49 System.out.println(image.getImageName()); 50 } 51 } 52 }
控制台显示:
第二节:根类对应一个表
image2.java
1 package com.wishwzp.model; 2 3 public class Image2 { 4 5 private int id; 6 private String imageName; 7 private String imageType; 8 private Student2 student; 9 10 public int getId() { 11 return id; 12 } 13 public void setId(int id) { 14 this.id = id; 15 } 16 public String getImageName() { 17 return imageName; 18 } 19 public void setImageName(String imageName) { 20 this.imageName = imageName; 21 } 22 public Student2 getStudent() { 23 return student; 24 } 25 public void setStudent(Student2 student) { 26 this.student = student; 27 } 28 public String getImageType() { 29 return imageType; 30 } 31 public void setImageType(String imageType) { 32 this.imageType = imageType; 33 } 34 }
LifeImage2.java
1 package com.wishwzp.model; 2 3 public class LifeImage2 extends Image2{ 4 5 }
WorkImage2.java
1 package com.wishwzp.model; 2 3 public class WorkImage2 extends Image2{ 4 5 }
Student2.java
1 package com.wishwzp.model; 2 3 import java.util.Set; 4 5 public class Student2 { 6 7 private int id; 8 private String name; 9 private Set<Image2> images; 10 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public Set<Image2> getImages() { 24 return images; 25 } 26 public void setImages(Set<Image2> images) { 27 this.images = images; 28 } 29 30 31 32 }
image2.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.wishwzp.model"> 7 8 <class name="Image2" table="t_image2"> 9 <id name="id" column="imageId"> 10 <generator class="native"></generator> 11 </id> 12 13 <discriminator column="imageType" type="string"></discriminator> 14 <property name="imageName" column="imageName"></property> 15 16 <many-to-one name="student" column="stuId" class="com.wishwzp.model.Student2"></many-to-one> 17 18 <subclass name="com.wishwzp.model.LifeImage2" discriminator-value="li"></subclass> 19 <subclass name="com.wishwzp.model.WorkImage2" discriminator-value="wi"></subclass> 20 </class> 21 22 </hibernate-mapping>
Student2.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.wishwzp.model"> 7 8 <class name="Student2" table="t_student2"> 9 <id name="id" column="stuId"> 10 <generator class="native"></generator> 11 </id> 12 13 <property name="name" column="stuName"></property> 14 15 <set name="images"> 16 <key column="stuId"></key> 17 <one-to-many class="com.wishwzp.model.Image2"/> 18 </set> 19 </class> 20 21 </hibernate-mapping>
hibernate.cfg.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 8 <session-factory> 9 10 <!--数据库连接设置 --> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">123456</property> 15 16 17 <!-- 方言 --> 18 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 19 20 <!-- 控制台显示SQL --> 21 <property name="show_sql">true</property> 22 23 <!-- 自动更新表结构 --> 24 <property name="hbm2ddl.auto">update</property> 25 26 <mapping resource="com/wishwzp/model/Student2.hbm.xml"/> 27 <mapping resource="com/wishwzp/model/Image2.hbm.xml"/> 28 29 30 </session-factory> 31 32 </hibernate-configuration>
StudentTest.java
1 package com.wishwzp.service; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Set; 7 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.junit.After; 11 import org.junit.Before; 12 import org.junit.Test; 13 14 import com.wishwzp.model.Image; 15 import com.wishwzp.model.Image2; 16 import com.wishwzp.model.Image3; 17 import com.wishwzp.model.Student2; 18 import com.wishwzp.model.Student3; 19 import com.wishwzp.util.HibernateUtil; 20 21 public class StudentTest { 22 23 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 24 private Session session; 25 26 @Before 27 public void setUp() throws Exception { 28 session=sessionFactory.openSession(); // 生成一个session 29 session.beginTransaction(); // 开启事务 30 } 31 32 @After 33 public void tearDown() throws Exception { 34 session.getTransaction().commit(); // 提交事务 35 session.close(); // 关闭session 36 } 37 38 39 @Test 40 public void testGetAllImages2(){ 41 42 } 43 }
插入数据:
然后查询这些数据:
StudentTest.java
1 package com.wishwzp.service; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Set; 7 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.junit.After; 11 import org.junit.Before; 12 import org.junit.Test; 13 14 import com.wishwzp.model.Image; 15 import com.wishwzp.model.Image2; 16 import com.wishwzp.model.Image3; 17 import com.wishwzp.model.Student2; 18 import com.wishwzp.model.Student3; 19 import com.wishwzp.util.HibernateUtil; 20 21 public class StudentTest { 22 23 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 24 private Session session; 25 26 @Before 27 public void setUp() throws Exception { 28 session=sessionFactory.openSession(); // 生成一个session 29 session.beginTransaction(); // 开启事务 30 } 31 32 @After 33 public void tearDown() throws Exception { 34 session.getTransaction().commit(); // 提交事务 35 session.close(); // 关闭session 36 } 37 38 @Test 39 public void testGetAllImages2(){ 40 Student2 student2=(Student2)session.get(Student2.class, 1); 41 Set<Image2> images=student2.getImages(); 42 Iterator it=images.iterator(); 43 while(it.hasNext()){ 44 Image2 image=(Image2)it.next(); 45 System.out.println(image.getImageName()); 46 } 47 } 48 }
控制台显示:
第三节:每个类对应一个表
Student3.java
1 package com.wishwzp.model; 2 3 import java.util.Set; 4 5 public class Student3 { 6 7 private int id; 8 private String name; 9 private Set<Image3> images; 10 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public Set<Image3> getImages() { 24 return images; 25 } 26 public void setImages(Set<Image3> images) { 27 this.images = images; 28 } 29 30 }
Image3.java
1 package com.wishwzp.model; 2 3 public class Image3 { 4 5 private int id; 6 private String imageName; 7 private Student3 student; 8 9 public int getId() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getImageName() { 16 return imageName; 17 } 18 public void setImageName(String imageName) { 19 this.imageName = imageName; 20 } 21 public Student3 getStudent() { 22 return student; 23 } 24 public void setStudent(Student3 student) { 25 this.student = student; 26 } 27 28 }
LifeImage3.java
1 package com.wishwzp.model; 2 3 public class LifeImage3 extends Image3{ 4 5 }
WorkImage3.java
1 package com.wishwzp.model; 2 3 public class WorkImage3 extends Image3{ 4 5 }
Student3.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.wishwzp.model"> 7 8 <class name="Student3" table="t_student3"> 9 <id name="id" column="stuId"> 10 <generator class="native"></generator> 11 </id> 12 13 <property name="name" column="stuName"></property> 14 15 <set name="images"> 16 <key column="stuId"></key> 17 <one-to-many class="com.wishwzp.model.Image3"/> 18 </set> 19 </class> 20 21 </hibernate-mapping>
Image3.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.wishwzp.model"> 7 8 <class name="Image3" table="t_image3"> 9 <id name="id" column="imageId"> 10 <generator class="native"></generator> 11 </id> 12 13 <property name="imageName" column="imageName"></property> 14 15 <many-to-one name="student" column="stuId" class="com.wishwzp.model.Student3"></many-to-one> 16 17 <joined-subclass name="com.wishwzp.model.LifeImage3" table="t_lifeImage3"> 18 <key column="lifeImageId"></key> 19 </joined-subclass> 20 21 <joined-subclass name="com.wishwzp.model.WorkImage3" table="t_workImage3"> 22 <key column="workImageId"></key> 23 </joined-subclass> 24 </class> 25 26 </hibernate-mapping>
hibernate.cfg.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 8 <session-factory> 9 10 <!--数据库连接设置 --> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">123456</property> 15 16 17 <!-- 方言 --> 18 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 19 20 <!-- 控制台显示SQL --> 21 <property name="show_sql">true</property> 22 23 <!-- 自动更新表结构 --> 24 <property name="hbm2ddl.auto">update</property> 25 26 <mapping resource="com/wishwzp/model/Student3.hbm.xml"/> 27 <mapping resource="com/wishwzp/model/Image3.hbm.xml"/> 28 29 </session-factory> 30 31 </hibernate-configuration>
StudentTest.java
1 package com.wishwzp.service; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Set; 7 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.junit.After; 11 import org.junit.Before; 12 import org.junit.Test; 13 14 import com.wishwzp.model.Image; 15 import com.wishwzp.model.Image2; 16 import com.wishwzp.model.Image3; 17 import com.wishwzp.model.Student2; 18 import com.wishwzp.model.Student3; 19 import com.wishwzp.util.HibernateUtil; 20 21 public class StudentTest { 22 23 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 24 private Session session; 25 26 @Before 27 public void setUp() throws Exception { 28 session=sessionFactory.openSession(); // 生成一个session 29 session.beginTransaction(); // 开启事务 30 } 31 32 @After 33 public void tearDown() throws Exception { 34 session.getTransaction().commit(); // 提交事务 35 session.close(); // 关闭session 36 } 37 38 @Test 39 public void testGetAllImages3(){ 40 41 } 42 }
插入一些数据:
然后查询这些数据:
StudentTest.java
1 package com.wishwzp.service; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Set; 7 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.junit.After; 11 import org.junit.Before; 12 import org.junit.Test; 13 14 import com.wishwzp.model.Image; 15 import com.wishwzp.model.Image2; 16 import com.wishwzp.model.Image3; 17 import com.wishwzp.model.Student2; 18 import com.wishwzp.model.Student3; 19 import com.wishwzp.util.HibernateUtil; 20 21 public class StudentTest { 22 23 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 24 private Session session; 25 26 @Before 27 public void setUp() throws Exception { 28 session=sessionFactory.openSession(); // 生成一个session 29 session.beginTransaction(); // 开启事务 30 } 31 32 @After 33 public void tearDown() throws Exception { 34 session.getTransaction().commit(); // 提交事务 35 session.close(); // 关闭session 36 } 37 38 @Test 39 public void testGetAllImages3(){ 40 Student3 student3=(Student3)session.get(Student3.class, 1); 41 Set<Image3> images=student3.getImages(); 42 Iterator it=images.iterator(); 43 while(it.hasNext()){ 44 Image3 image=(Image3)it.next(); 45 System.out.println(image.getImageName()); 46 } 47 } 48 }
控制台显示: