hibernate之初学一对多和多对一配置及使用
2021-07-05 21:05
标签:uil java .hbm.xml import entity 使用 oct connect throws 按查询及存取速率来说的一对多用的相对多对一少一些,这里只写主要配置文件的代码 首先是hibernate的配置文件 一对多的配置文件代码,很明显使用到了set集合 对应的实体类: 多对一的配置文件代码,很明显是一个对象类型 对应的实体类 测试这两个存储方式 hibernate之初学一对多和多对一配置及使用 标签:uil java .hbm.xml import entity 使用 oct connect throws 原文地址:http://www.cnblogs.com/ShaoXin/p/7105837.htmlDOCTYPE 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.Driverproperty>
property name="hibernate.connection.url">jdbc:mysql:///webprojectproperty>
property name="hibernate.connection.username">rootproperty>
property name="hibernate.connection.password">123456property>
property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectproperty>
property name="hibernate.show_sql">trueproperty>
property name="hibernate.hbm2ddl.auto">createproperty>
mapping resource="com/shaoxin/entity/Dept.hbm.xml"/>
mapping resource="com/shaoxin/entity/Employee.hbm.xml"/>
session-factory>
hibernate-configuration>
xml version="1.0"?>
DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
hibernate-mapping package="com.shaoxin.entity">
class name="Dept" table="dept">
id name="deptid" column="deptid">
generator class="native">generator>
id>
property name="deptName" column="deptname">property>set name="setEmployees" table="employee">
key column="dept_id">key>
one-to-many class="Employee"/>
set>
class>
hibernate-mapping>
package com.shaoxin.entity;
import java.util.HashSet;
import java.util.Set;
public class Dept {
private int deptid;
private String deptName;
private Set
xml version="1.0"?>
DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
hibernate-mapping package="com.shaoxin.entity">
class name="Employee" table="employee">
id name="employeeid" column="employeeid">
generator class="native">generator>
id>
property name="employeeName" column="employeename"/>many-to-one name="dept" column="dept_id" class="Dept">many-to-one>
class>
hibernate-mapping>
package com.shaoxin.entity;
public class Employee {
private int employeeid;
private String employeeName;
private Dept dept;
public int getEmployeeid() {
return employeeid;
}
public void setEmployeeid(int employeeid) {
this.employeeid = employeeid;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}
package com.shaoxin.entity;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;
public class TestMany2Many {
static SessionFactory sf;
static {
sf = new Configuration().configure().buildSessionFactory();
}
@Test
public void testOne2Many() throws Exception {
Session openSession = sf.openSession();
Transaction beginTransaction = openSession.beginTransaction();
Employee empzs = new Employee();
Employee empls = new Employee();
empls.setEmployeeName("李四");
empzs.setEmployeeName("张三");
Dept dept = new Dept();
dept.getSetEmployees().add(empls);
dept.getSetEmployees().add(empzs);
dept.setDeptName("应用开发");
openSession.save(empls);
openSession.save(empzs);
openSession.save(dept);
beginTransaction.commit();
openSession.close();
}
@Test
public void testMany2One() throws Exception {
Session openSession = sf.openSession();
Transaction beginTransaction = openSession.beginTransaction();
Employee empzs = new Employee();
Employee empls = new Employee();
Dept dept = new Dept();
dept.setDeptName("应用开发");
empls.setEmployeeName("李四");
empzs.setEmployeeName("张三");
empls.setDept(dept);
empzs.setDept(dept);
openSession.save(dept);
openSession.save(empls);
openSession.save(empzs);
beginTransaction.commit();
openSession.close();
}
}
下一篇:php防止post重复提交