Hibernate学习6—Hibernate 映射类型
第一节:基本类型映射
com.cy.model.Book.java:
package com.cy.model; import java.sql.Blob; import java.util.Date; public class Book { private int id; private String bookName; // 图书名称 private float price; //图书价格 private boolean specialPrice; //是否是特价 private Date publishDate; // 发布日期 private String author; // 作者 private String introduction; // 简介 private Blob bookPic; // 图书图片 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public boolean isSpecialPrice() { return specialPrice; } public void setSpecialPrice(boolean specialPrice) { this.specialPrice = specialPrice; } public Date getPublishDate() { return publishDate; } public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getIntroduction() { return introduction; } public void setIntroduction(String introduction) { this.introduction = introduction; } public Blob getBookPic() { return bookPic; } public void setBookPic(Blob bookPic) { this.bookPic = bookPic; } }
Book.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.cy.model"> <class name="Book" table="t_book"> <id name="id" column="bookId"> <generator class="native"></generator> </id> <!-- type是对应的hibernate映射类型 length=“40”表示字符串长度为40 --> <property name="bookName" column="bookName" length="40"></property> <property name="price" column="price" type="float"></property> <property name="specialPrice" column="specialPrice" type="boolean"></property> <property name="publishDate" column="publishDate" type="date"></property> <property name="author" column="author" length="20"></property> <property name="introduction" column="introduction" type="text"></property> <property name="bookPic" column="bookPic" type="blob"></property> </class> </hibernate-mapping>
BookTest.java测试代码:
public class BookTest { public static void main(String[] args) throws ParseException, IOException { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); Book book = new Book(); book.setBookName("java编程思想"); book.setPrice(100); book.setSpecialPrice(true); book.setAuthor("埃克尔"); book.setPublishDate(new SimpleDateFormat("yyyy-MM-dd").parse("2017-1-1")); book.setIntroduction("简介..."); LobHelper lobHelper = session.getLobHelper(); InputStream in = new FileInputStream("c://java编程思想.jpg"); Blob bookPic = lobHelper.createBlob(in, in.available()); book.setBookPic(bookPic); session.save(book); session.getTransaction().commit(); session.close(); } }
结果:
t_book:
存数据后:
第二节:集合类型映射
这里讲的集合和前面一对多多对一的集合不一样;
这里是一种组合;单纯的数据映射,不带session缓存的,也不包括OID
1.Set集合映射:
Student.java:
package com.cy.model; import java.util.Set; public class Student { private long id; private String name; private Set<String> images; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<String> getImages() { return images; } public void setImages(Set<String> images) { this.images = images; } }
Student.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Student" table="t_student"> <id name="id" column="stuId"> <generator class="native"></generator> </id> <property name="name" column="stuName"></property> <!-- t_image表的外键studentId关联Student表的主键 element元素是集合的元素 --> <set name="images" table="t_image"> <key column="studentId"></key> <element column="imageName" type="string"></element> </set> </class> </hibernate-mapping>
测试:
@Test public void testSetSave(){ Set<String> imageSet=new HashSet<String>(); imageSet.add("image1.png"); imageSet.add("image2.png"); imageSet.add("image3.png"); imageSet.add("image3.png"); Student s1=new Student(); s1.setImages(imageSet); session.save(s1); } @Test public void testSetFetch(){ Student student=(Student)session.get(Student.class, Long.valueOf(1)); Iterator it = student.getImages().iterator(); while(it.hasNext()){ System.out.println(it.next()); } }
执行testSetSave后:
t_student
t_image:外键studentId 参考t_student的主键列
2.List集合映射:
List:正是因为有了索引列imageIndex,所以他才是有序的;
Student2.java:
package com.cy.model; import java.util.List; public class Student2 { private long id; private String name; private List<String> images; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getImages() { return images; } public void setImages(List<String> images) { this.images = images; } }
Student2.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Student2" table="t_student"> <id name="id" column="stuId"> <generator class="native"></generator> </id> <property name="name" column="stuName"></property> <list name="images" table="t_image2"> <key column="studentId"></key> <list-index column="imageIndex"></list-index> <element column="imageName" type="string"></element> </list> </class> </hibernate-mapping>
测试代码:
@Test public void testListSave(){ List<String> imageList=new ArrayList<String>(); imageList.add("image1.png"); imageList.add("image2.png"); imageList.add("image3.png"); imageList.add("image3.png"); Student2 s2=new Student2(); s2.setImages(imageList); session.save(s2); } @Test public void testListFetch(){ Student2 student2=(Student2)session.get(Student2.class, Long.valueOf(2)); Iterator it = student2.getImages().iterator(); while(it.hasNext()){ System.out.println(it.next()); } }
t_student:
t_image2:
3.Bag集合映射:
用List来模拟的;大多数情况下是有序的;但是数据量很大时,确实是无序。
student3.java:
package com.cy.model; import java.util.List; public class Student3 { private long id; private String name; private List<String> images; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getImages() { return images; } public void setImages(List<String> images) { this.images = images; } }
Student3.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Student3" table="t_student"> <id name="id" column="stuId"> <generator class="native"></generator> </id> <property name="name" column="stuName"></property> <idbag name="images" table="t_image3"> <collection-id type="long" column="imageId"> <generator class="increment"></generator> </collection-id> <key column="studentId"></key> <element column="imageName" type="string"></element> </idbag> </class> </hibernate-mapping>
测试代码:
@Test public void testBagSave(){ List<String> imageList=new ArrayList<String>(); imageList.add("image1.png"); imageList.add("image2.png"); imageList.add("image3.png"); imageList.add("image3.png"); Student3 s3=new Student3(); s3.setImages(imageList); session.save(s3); } @Test public void testBagFetch(){ Student3 student3 =(Student3)session.get(Student3.class, Long.valueOf(3)); Iterator it = student3.getImages().iterator(); while(it.hasNext()){ System.out.println(it.next()); } }
4.Map集合映射
Student4.java:
package com.cy.model; import java.util.Map; public class Student4 { private long id; private String name; private Map<String, String> images; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<String, String> getImages() { return images; } public void setImages(Map<String, String> images) { this.images = images; } }
Student4.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Student4" table="t_student"> <id name="id" column="stuId"> <generator class="native"></generator> </id> <property name="name" column="stuName"></property> <map name="images" table="t_image4"> <key column="studentId"></key> <map-key column="imageKey" type="string"></map-key> <element column="imageName" type="string"></element> </map> </class> </hibernate-mapping>
测试代码:
@Test public void testMapSave(){ Map<String, String> imageMap = new HashMap<String, String>(); imageMap.put("i1", "image1.png"); imageMap.put("i2", "image2.png"); imageMap.put("i3", "image3.png"); imageMap.put("i4", "image4.png"); Student4 s4=new Student4(); s4.setImages(imageMap); session.save(s4); } @Test public void testMapFetch(){ Student4 student4 =(Student4)session.get(Student4.class, Long.valueOf(4)); /** * 遍历map,方法一 */ Iterator<Entry<String, String>> it = student4.getImages().entrySet().iterator(); while(it.hasNext()){ Entry<String, String> entry = it.next(); System.out.println(entry.getKey() + "----" + entry.getValue()); } System.out.println("------------------------------------------------"); /** * 方法二 */ Map<String, String> imageMap = student4.getImages(); Set<String> imageKey = imageMap.keySet(); Iterator<String> its = imageKey.iterator(); while(its.hasNext()){ String key = its.next(); System.out.println(key + ":" + imageMap.get(key)); } }
执行testMapSave:
t_image4表结构:
执行testMapFetch: