一对多 1:*
Employee Dept 一个员工对应一个部门,一个部门对应多个员工
* 1
关系属性 Dept Set<Employee> emps=new HashSet<Employee>();
为什么要使用Set:
1.Set可以去重复
2.如果关系属性是一个集合,要使用set接口类型,而不能使用Hashset,也Hibernate 延迟加载有关系
对象之间的单向和双向关系由业务需求而定
建表
t_employee
普通的列 + 外键列 d_id
t_dept
普通的列
1.建表
create table g_dept(
t_id integer primary key,
t_dname varchar2(20),
t_dno varchar2(20)
)
create table g_employee(
t_id integer primary key,
t_name varchar2(25),
t_birthday date,
t_salary number(7,2),
t_email varchar2(30),
d_id integer references g_dept(t_id)
)
2.在com.jsu.hb.pojo包中提供2个实体类Employee.java和Dept.java
在Employee.java中
package com.jsu.hb.pojo;
import java.util.Date;
public class Employee {
private Integer id;
private String name;
private Date birthday;
private String email;
private double salary;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
在Dept.java中
package com.jsu.hb.pojo;
import java.util.HashSet;
import java.util.Set;
public class Dept {
private Integer id;
private String dname;
private String dno;
private Set<Employee> emps = new HashSet<Employee>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getDno() {
return dno;
}
public void setDno(String dno) {
this.dno = dno;
}
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
}
3.提供映射配置文件(单向)在o2m.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">
<hibernate-mapping package="com.jsu.hb.pojo">
<class name="Dept" table="g_dept">
<id name="id" column="t_id">
<generator class="increment"></generator>
</id>
<property name="dname" column="t_dname"></property>
<property name="dno" column="t_dno"></property>
<!-- 关系属性 -->
<set name="emps" cascade="all" inverse="true">
<key column="d_id"></key>
<one-to-many class="Employee"></one-to-many>
</set>
</class>
<class name="Employee" table="g_employee">
<id name="id" column="t_id">
<generator class="increment"></generator>
</id>
<property name="name" column="t_name"></property>
<property name="birthday" column="t_birthday" type="java.util.Date"></property>
<property name="email" column="t_email"></property>
<property name="salary" column="t_salary"></property>
</class>
<hibernate-mapping>
4.在hibernate.cfg.xml中对映射文件进行注册
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- show_sql:是否显示hibernate执行的SQL语句,默认为false --> <property name="show_sql">true</property> <!-- show_sql:是否显示hibernate执行格式化输出的SQL语句,默认为false --> <property name="format_sql">true</property> <!-- 配置与数据库连接的参数 --> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:oracle</property> <property name="connection.username">scott</property> <property name="connection.password">tiger</property> <!-- 2.自身属性相关的配置 dialect:方言 hibernate根据dialect的配置进行特定数据性能方面的调优 --> <property name="dialect">org.hibernate.dialect.Oracle9iDialect</property> <mapping resource="com/jsu/hb/pojo/o2m.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
5.提供HibernateUtil.java配置文件
package com.jsu.hb.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sf;
private static ThreadLocal<Session> tl= new ThreadLocal<Session>();
static{
try{
Configuration cfg = new Configuration();
cfg.configure();
sf=cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
public static Session openSession(){
return sf.openSession();
}
public static Session getCurrentSession(){
Session session = tl.get();//先从储存的线程中查找
if(session==null){
session=openSession();
tl.set(session);
return session;
}
return session;
}
}
6.在测试类中TestO2M.java中
package com.jsu.hb.test;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.jsu.hb.pojo.Dept;
import com.jsu.hb.pojo.Employee;
import com.jsu.hb.util.HibernateUtil;
public class TestO2M {
@Test
public void save(){
Dept d = new Dept();
d.setDname("技术部");
d.setDno("1001");
Employee e1 = new Employee();
e1.setName("zhangsan");
e1.setBirthday(new Date());
e1.setEmail("zhangsan@qq.com");
e1.setSalary(1234.0);
Employee e2 = new Employee();
e2.setName("tom");
e2.setBirthday(new Date());
e2.setEmail("tom@qq.com");
e2.setSalary(2345.5);
Employee e3 = new Employee();
e2.setName("leon");
e2.setBirthday(new Date());
e2.setEmail("leon@qq.com");
e2.setSalary(2351.6);
Set<Employee> emps = new HashSet<Employee>();
emps.add(e1);
emps.add(e2);
emps.add(e3);
d.setEmps(emps);//设置在内存中的关系
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.getTransaction();
tx.begin();
session.save(d);
tx.commit();
}
}
本文详细阐述了一对一关系模型在数据库中的实现方式,并通过使用Hibernate框架进行配置,展示了如何在Java应用中轻松地管理和操作这种关系。文章涵盖了建表、实体类设计、映射配置、测试验证等关键步骤,旨在帮助开发者理解和实践一对一关系的高效实现。

185

被折叠的 条评论
为什么被折叠?



