关键代码:
<!-- 单向多对一映射 name:多这一端属性的名字 class:一那一端对应的类名 column:一那一端在多的一端数据表中外键的名字 --> <many-to-one name="user" class="User" column="USER_ID"> </many-to-one>
详细:
User.java:
package com.shuoeasy.test; public class User { private int id; private String userName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + "]"; } }
Orders.java:
package com.shuoeasy.test; public class Orders { private int id; private User user; private String title; public int getId() { return id; } public void setId(int id) { this.id = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return "Order [id=" + id + ", user=" + user + ", title=" + title + "]"; } }
User.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.shuoeasy.test.User" table="USER"> <id name="id" type="int"> <column name="ID" /> <!-- 指定主键的生成方式,native:使用数据库本地方式 --> <generator class="native" /> </id> <property name="userName" type="java.lang.String"> <column name="USERNAME" /> </property> </class> </hibernate-mapping>
Orders.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping package="com.shuoeasy.test"> <class name="Orders" table="Orders"> <id name="id" type="int"> <column name="ID" /> <!-- 指定主键的生成方式,native:使用数据库本地方式 --> <generator class="native" /> </id> <property name="title" type="java.lang.String"> <column name="TITLE" /> </property> <!-- 单向多对一映射 name:多这一端属性的名字 class:一那一端对应的类名 column:一那一端在多的一端数据表中外键的名字 --> <many-to-one name="user" class="User" column="USER_ID"> </many-to-one> </class> </hibernate-mapping>
AppTest.java:
package com.shuoeasy.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { Session session; SessionFactory sf; @Before public void init() { Configuration conf = new Configuration().configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); sf = conf.buildSessionFactory((org.hibernate.service.ServiceRegistry) sr); session = sf.openSession(); session.beginTransaction(); System.out.println("init"); } @After public void destory() { session.getTransaction().commit(); session.close(); sf.close(); System.out.println("dectory"); } /** * 插入数据 */ @Test public void testInsert(){ User user = new User(); user.setUserName("张三"); Orders order1 = new Orders(); order1.setTitle("张三的订单1"); order1.setUser(user); Orders order2 = new Orders(); order2.setTitle("张三的订单2"); order2.setUser(user); // 按照父子数据的顺序插入,生成三条insert session.save(user); session.save(order1); session.save(order2); /* * 打乱顺序插入,生成三条insert,两条update。 * 所以不建议打乱顺序 session.save(order1); session.save(order2); session.save(user);*/ } /** * 读取数据 */ @Test public void testGet() { Orders order = (Orders) session.get(Orders.class, 1); System.out.println(order); // 使用的对象的时候才会懒加载user对象 } /** * 修改 */ @Test public void testUpdate(){ Orders order = (Orders) session.get(Orders.class, 1); order.getUser().setUserName("李四"); session.save(order); } /** * 删除 */ @Test public void testDelete(){ // 删除会失败,默认不能删除级联对象 User user = (User) session.get(User.class, 1); session.delete(user); } }
生产的数据库表结构
user表:
orders表: