hibernate_exercise-many- to-one(1)
多对一关系
1.创建t_user表、t_group表
2.在eclipse中创建对应的实体类
1 package com.eneity; 2 3 public class User { 4 5 private int id; 6 private String name; 7 private Group group;//注意这里不是gid 而是group对象 8 9 public User() { 10 super(); 11 // TODO Auto-generated constructor stub 12 } 13 public int getId() { 14 return id; 15 } 16 public void setId(int id) { 17 this.id = id; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public Group getGroup() { 26 return group; 27 } 28 public void setGroup(Group group) { 29 this.group = group; 30 } 31 32 33 }
User.java
1 package com.eneity; 2 3 public class Group { 4 5 private int id ; 6 private String name; 7 8 public Group() { 9 super(); 10 // TODO Auto-generated constructor stub 11 } 12 public int getId() { 13 return id; 14 } 15 public void setId(int id) { 16 this.id = id; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 }
Group.java
3.写对应的xml映射文件
注意:写好对应的xml文件后要在主配置文件中添加映射
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 2018-4-8 16:35:46 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="com.eneity.User" table="t_user"> 7 <id name="id" type="int"> 8 <column name="ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="name" type="java.lang.String"> 12 <column name="NAME" /> 13 </property> 14 <many-to-one name="group" class="com.eneity.Group" fetch="join"> 15 <column name="gid" /> 16 </many-to-one> 17 </class> 18 </hibernate-mapping>
com/eneity/User.hbm.xml
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 2018-4-8 16:35:46 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="com.eneity.Group" table="t_group"> 7 <id name="id" type="int"> 8 <column name="ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="name" type="java.lang.String"> 12 <column name="NAME" /> 13 </property> 14 </class> 15 </hibernate-mapping>
com/eneity/Group.hbm.xml
主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 8 <property name="hibernate.connection.password">xzt521</property> 9 <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/myweb</property> 10 <property name="hibernate.connection.username">xzt</property> 11 <!-- 配置显示sql语句 --> 12 <property name="show_sql">true</property> 13 <!-- 以一定的格式输出语句 --> 14 <property name="format_sql">true</property> 15 <mapping resource="com/eneity/Group.hbm.xml"/> 16 <mapping resource="com/eneity/User.hbm.xml"/> 17 </session-factory> 18 </hibernate-configuration>
hibernate.cfg.xml
4.写一个测试类
注意:如果只是要查询数据,只要开启session即可
要增删改的话,要开启事务管理
最后要提交事务(这样数据库中才会存进去数据)
关闭session
1 package com.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 import org.junit.After; 8 import org.junit.Before; 9 import org.junit.Test; 10 11 import com.eneity.Customers; 12 import com.eneity.Group; 13 import com.eneity.Order; 14 import com.eneity.User; 15 16 17 public class Test1 { 18 private SessionFactory buildSessionFactory; 19 private Session Session; 20 private Transaction Transaction; 21 22 @Before 23 public void before() { 24 Configuration cfg = new Configuration().configure(); 25 buildSessionFactory = cfg.buildSessionFactory(); 26 Session = buildSessionFactory.openSession(); 27 Transaction = Session.beginTransaction(); 28 System.out.println(Session); 29 30 31 } 32 33 @Test 34 public void test1() { 35 // TODO Auto-generated method stub 36 37 Group group = new Group(); 38 group.setName("duhao"); 39 User user = new User(); 40 user.setName("jiunan"); 41 user.setGroup(group); 42 Session.save(group); 43 Session.save(user); 44 45 46 } 47 48 @After 49 public void after() { 50 Transaction.commit(); 51 Session.close(); 52 53 } 54 }
Test1.java