什么是组件属性?
比如address是students的其中一个属性,而address又有三个属性:邮编、电话和地址。address就是hibernate的组件属性。
首先建立Address类:
1 package com.imooc.hibernate; 2 3 public class Address { 4 5 private String postcode; //邮编 6 private String phone; //电话 7 private String address; //地址 8 9 public Address() {}; 10 11 public String getPostcode() { 12 return postcode; 13 } 14 public void setPostcode(String postcode) { 15 this.postcode = postcode; 16 } 17 public String getPhone() { 18 return phone; 19 } 20 public void setPhone(String phone) { 21 this.phone = phone; 22 } 23 public String getAddress() { 24 return address; 25 } 26 public void setAddress(String address) { 27 this.address = address; 28 } 29 public Address(String postcode, String phone, String address) { 30 // super(); 31 this.postcode = postcode; 32 this.phone = phone; 33 this.address = address; 34 } 35 @Override 36 public String toString() { 37 return "Address [postcode=" + postcode + ", phone=" + phone + ", address=" + address + "]"; 38 } 39 40 }
然后建立Students类:
1 package com.imooc.hibernate; 2 3 import java.sql.Blob; 4 import java.util.Date; 5 6 public class Students { 7 8 private int sid; 9 private String sname; 10 private String gender; 11 private Date birthday; 12 private Address address; 13 private Blob picture; 14 15 public Blob getPicture() { 16 return picture; 17 } 18 19 public void setPicture(Blob picture) { 20 this.picture = picture; 21 } 22 23 public Students() {} 24 25 public Students(int sid, String sname, String gender, Date birthday, Address address, Blob picture) { 26 super(); 27 this.sid = sid; 28 this.sname = sname; 29 this.gender = gender; 30 this.birthday = birthday; 31 this.address = address; 32 this.picture = picture; 33 } 34 35 @Override 36 public String toString() { 37 return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday 38 + ", address=" + address + ", picture=" + picture + "]"; 39 } 40 41 public Address getAddress() { 42 return address; 43 } 44 45 public void setAddress(Address address) { 46 this.address = address; 47 } 48 49 public int getSid() { 50 return sid; 51 } 52 53 public void setSid(int sid) { 54 this.sid = sid; 55 } 56 57 public String getSname() { 58 return sname; 59 } 60 61 public void setSname(String sname) { 62 this.sname = sname; 63 } 64 65 public String getGender() { 66 return gender; 67 } 68 69 public void setGender(String gender) { 70 this.gender = gender; 71 } 72 73 public Date getBirthday() { 74 return birthday; 75 } 76 77 public void setBirthday(Date birthday) { 78 this.birthday = birthday; 79 } 80 81 }
建立Students.hbm.xml文件,注意Address组件的写法:
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2017-5-23 0:24:09 by Hibernate Tools 3.5.0.Final --> 5 <hibernate-mapping> 6 <class name="com.imooc.hibernate.Students" table="STUDENTS"> 7 <id name="sid" type="int"> 8 <column name="SID" /> 9 <generator class="native" /> 10 </id> 11 <property name="sname" type="java.lang.String"> 12 <column name="SNAME" /> 13 </property> 14 <property name="gender" type="java.lang.String"> 15 <column name="GENDER" /> 16 </property> 17 <property name="birthday" type="java.util.Date"> 18 <column name="BIRTHDAY" /> 19 </property> 20 <property name="picture" type="java.sql.Blob"> 21 <column name="PICTURE" /> 22 </property> 23 24 <component name="address" class="com.imooc.hibernate.Address"> 25 <property name="postcode" column="POSTCODE"/> 26 <property name="phone" column="PHONE"/> 27 <property name="address" column="ADDRESS"/> 28 </component> 29 </class> 30 </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 <hibernate-configuration> 6 <session-factory> 7 <property name="connection.username">root</property> 8 <property name="connection.password">root</property> 9 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 10 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property> 11 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 12 <property name="show_sql">true</property> 13 <property name="format_sql">true</property> 14 <property name="hbm2ddl.auto">create</property> 15 16 <mapping resource="com/imooc/hibernate/Students.hbm.xml"/> 17 </session-factory> 18 </hibernate-configuration>
建立测试类:
1 package com.icoom.test; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 import java.sql.Blob; 8 import java.util.Date; 9 10 import org.hibernate.Hibernate; 11 import org.hibernate.Session; 12 import org.hibernate.SessionFactory; 13 import org.hibernate.Transaction; 14 import org.hibernate.cfg.Configuration; 15 import org.hibernate.service.ServiceRegistry; 16 import org.hibernate.service.ServiceRegistryBuilder; 17 import org.junit.After; 18 import org.junit.Before; 19 import org.junit.Test; 20 21 import com.imooc.hibernate.Address; 22 import com.imooc.hibernate.Students; 23 24 public class StudentsTest { 25 26 private SessionFactory sessionFactory; 27 private Session session; 28 private Transaction transaction; 29 30 @Before 31 public void init() { 32 // 1.创建配置对象 33 Configuration config = new Configuration().configure(); 34 // 2.创建服务注册对象 35 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 36 // 3.创建会话工厂对象 37 sessionFactory = config.buildSessionFactory(serviceRegistry); 38 // 会话对象 39 session = sessionFactory.openSession(); 40 // 开启事务 41 transaction = session.beginTransaction(); 42 } 43 @After 44 public void destory() { 45 transaction.commit(); //提交事务 46 session.close(); //关闭session 47 sessionFactory.close();//关闭会话工厂 48 } 49 @Test 50 public void testSaveStudents() { 51 Students s = new Students(); 52 s.setSid(1); 53 s.setSname("老张"); 54 s.setGender("男"); 55 s.setBirthday(new Date()); 56 Address address = new Address("250000", "1310531xxxx", "山东济南"); 57 s.setAddress(address); 58 session.save(s);//保存对象进入数据库 59 } 60 }
执行成功,查看数据库: