Hibernate维护一对多对应关系
比如学校和学生,一个学校可以有多个学生,一个学生只能属于一个学校。这就属于一对多关系
第一步创建两个实体类,用户类和学校类
学生类
package com.netxintai.domain; public class User { private Integer u_id; private String username; private String password; private String sex; private String address; /* * 一个学生只有一个学校 指向为目的 * * */ private School school; public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } }
学校类
package com.netxintai.domain; import java.util.HashSet; import java.util.Set; public class School { private Integer s_id; private String sname; /* * 一个学校可以有多个学生,hibernate规定为Set集合封装多个数据 * * */ private Set<User> setUser=new HashSet<User>(); public Integer getS_id() { return s_id; } public void setS_id(Integer s_id) { this.s_id = s_id; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Set<User> getSetUser() { return setUser; } public void setSetUser(Set<User> setUser) { this.setUser = setUser; } }
第二步配置两个类的hibernate映射文件
学生类映射配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.netxintai.domain.User" table="t_user"> <id name="u_id" column="u_id"> <generator class="native"></generator> </id> <property name="username" column="username" /> <property name="password" column="password" /> <property name="sex" column="sex" /> <property name="address" column="address" /> <!-- 表示学生所属学校 name属性:学生类对应的学校名称School类 写school class属性 School类的全路径 column属性: 外键名称 --> <many-to-one name="school" class="com.netxintai.domain.School" column="suid"></many-to-one> </class> </hibernate-mapping>
学校类映射配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.netxintai.domain.School" table="s_school"> <id name="s_id" column="s_id"> <generator class="native"></generator> </id> <property name="sname" column="sname"/> <!-- 学校映射文件中,表示所有联系人 使用Set标签表示所有联系人set标签里面有name属性,属性值写在学校实体类里面表示联系人的set集合名称 --> <set name="setUser" cascade="save-update" inverse="true"> <!-- 一对多建表,有外键 hibernate机制,双向维护未安检,在一核多那一方都要配置外键,column属性值:外键名称 --> <key column="suid"></key> <!-- 一对多配置class里面写学生实体类的全路径 --> <one-to-many class="com.netxintai.domain.User"></one-to-many> </set> </class> </hibernate-mapping>
在hibernate文件中引入两个配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置数据信息 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///mydb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123</property> <!-- 配置hibernate信息 可选 --> <!-- 输出sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 格式化sql语句 --> <property name="hibernate.format_sql">true</property> <!-- hibernate帮创建表 需要配置后 update:如果已经存在表,更新,如果没有 创建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 把映射文件放到核心配置文件中 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 引入配置文件 --> <mapping resource="com/netxintai/domain/School.hbm.xml"/> <mapping resource="com/netxintai/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
测试代码
package com.netxintai.dome; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.junit.Test; import com.netxintai.domain.School; import com.netxintai.domain.User; import com.netxintai.utils.HiberntaeUtils; public class DomeTEST { private SessionFactory sessionFactory; private Session session; private Transaction tr; @Test public void DomeTest() { try { sessionFactory = HiberntaeUtils.getsessionFactory(); session = sessionFactory.openSession(); tr = session.beginTransaction(); User user = new User(); School school = new School(); user.setAddress("长安"); user.setPassword("147"); user.setUsername("李世民"); school.setSname("长安大学"); user.setSchool(school); school.getSetUser().add(user); session.save(school); tr.commit(); } catch (Exception e) { // TODO: handle exception tr.rollback(); } finally { } } }
测试结果
备注:不懂Hibernateutils工具类的点击这里 hibernateUtils工具类