Hibernate 映射关系
Posted kika
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate 映射关系相关的知识,希望对你有一定的参考价值。
所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java
第一节:Hibernate 一对一映射关系实现
1,按照主键映射;
2,按照外键映射;
1,按照主键映射:
User.java
1 package com.wishwzp.model; 2 3 public class User { 4 5 private int id; 6 private String name; 7 private Address address; 8 9 public int getId() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public Address getAddress() { 22 return address; 23 } 24 public void setAddress(Address address) { 25 this.address = address; 26 } 27 28 29 }
Address.java
1 package com.wishwzp.model; 2 3 public class Address { 4 5 private int id; 6 private String address; 7 private String zipcode; 8 private User user; 9 10 public int getId() { 11 return id; 12 } 13 public void setId(int id) { 14 this.id = id; 15 } 16 public String getAddress() { 17 return address; 18 } 19 public void setAddress(String address) { 20 this.address = address; 21 } 22 public String getZipcode() { 23 return zipcode; 24 } 25 public void setZipcode(String zipcode) { 26 this.zipcode = zipcode; 27 } 28 public User getUser() { 29 return user; 30 } 31 public void setUser(User user) { 32 this.user = user; 33 } 34 35 36 }
User.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="User" table="t_user"> 9 <id name="id" column="userId"> 10 <generator class="native"></generator> 11 </id> 12 13 <property name="name" column="userName"></property> 14 15 <one-to-one name="address" class="com.wishwzp.model.Address" cascade="all"></one-to-one> 16 </class> 17 18 </hibernate-mapping>
Address.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="Address" table="t_address"> 9 <id name="id" column="addressId"> 10 <generator class="foreign"> 11 <param name="property">user</param> 12 </generator> 13 </id> 14 15 <property name="address" column="address"></property> 16 <property name="zipcode" column="zipcode"></property> 17 18 <one-to-one name="user" class="com.wishwzp.model.User" constrained="true"></one-to-one> 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/User.hbm.xml"/> 27 <mapping resource="com/wishwzp/model/Address.hbm.xml"/> 28 29 </session-factory> 30 31 </hibernate-configuration>
UserTest.java
1 package com.wishwzp.service; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Test; 8 9 import com.wishwzp.model.Address; 10 import com.wishwzp.model.Address2; 11 import com.wishwzp.model.User; 12 import com.wishwzp.model.User2; 13 import com.wishwzp.util.HibernateUtil; 14 15 public class UserTest { 16 17 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 18 private Session session; 19 20 @Before 21 public void setUp() throws Exception { 22 session=sessionFactory.openSession(); // 生成一个session 23 session.beginTransaction(); // 开启事务 24 } 25 26 @After 27 public void tearDown() throws Exception { 28 session.getTransaction().commit(); // 提交事务 29 session.close(); // 关闭session 30 } 31 32 @Test 33 public void testSave1(){ 34 35 } 36 37 }
UserTest.java
1 package com.wishwzp.service; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Test; 8 9 import com.wishwzp.model.Address; 10 import com.wishwzp.model.Address2; 11 import com.wishwzp.model.User; 12 import com.wishwzp.model.User2; 13 import com.wishwzp.util.HibernateUtil; 14 15 public class UserTest { 16 17 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 18 private Session session; 19 20 @Before 21 public void setUp() throws Exception { 22 session=sessionFactory.openSession(); // 生成一个session 23 session.beginTransaction(); // 开启事务 24 } 25 26 @After 27 public void tearDown() throws Exception { 28 session.getTransaction().commit(); // 提交事务 29 session.close(); // 关闭session 30 } 31 32 @Test 33 public void testSave1(){ 34 User user=new User(); 35 user.setName("张三"); 36 37 Address address=new Address(); 38 address.setAddress("某地方"); 39 address.setZipcode("43242"); 40 address.setUser(user); 41 42 user.setAddress(address); 43 session.save(user); 44 } 45 46 }
我们发现主键一对一的。。。。
2,按照外键映射:
User2.java
1 package com.wishwzp.model; 2 3 public class User2 { 4 5 private int id; 6 private String name; 7 private Address2 address; 8 9 public int getId() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public Address2 getAddress() { 22 return address; 23 } 24 public void setAddress(Address2 address) { 25 this.address = address; 26 } 27 28 29 }
Address2.java
1 package com.wishwzp.model; 2 3 public class Address2 { 4 5 private int id; 6 private String address; 7 private String zipcode; 8 private User2 user; 9 10 public int getId() { 11 return id; 12 } 13 public void setId(int id) { 14 this.id = id; 15 } 16 public String getAddress() { 17 return address; 18 } 19 public void setAddress(String address) { 20 this.address = address; 21 } 22 public String getZipcode() { 23 return zipcode; 24 } 25 public void setZipcode(String zipcode) { 26 this.zipcode = zipcode; 27 } 28 public User2 getUser() { 29 return user; 30 } 31 public void setUser(User2 user) { 32 this.user = user; 33 } 34 35 36 37 }
User2.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="User2" table="t_user2"> 9 <id name="id" column="userId"> 10 <generator class="native"></generator> 11 </id> 12 13 <property name="name" column="userName"></property> 14 15 <many-to-one name="address" class="com.wishwzp.model.Address2" column="addressId" cascade="all" unique="true"></many-to-one> 16 </class> 17 18 </hibernate-mapping>
Address2.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="Address2" table="t_address2"> 9 <id name="id" column="addressId"> 10 <generator class="native"> 11 </generator> 12 </id> 13 14 <property name="address" column="address"></property> 15 <property name="zipcode" column="zipcode"></property> 16 17 <one-to-one name="user" class="com.wishwzp.model.User2" property-ref="address"></one-to-one> 18 </class> 19 20 </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 Hibernate 映射一对一关联关系关联映射级联操作关系维护 ---- Hibernate之一对多|多对一关系