项目的文件结构:
HibernateSessionFactory.java
package addHibFile;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.hibernate.HibernateException;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml";
private HibernateSessionFactory() {
}
private static final ThreadLocal threadLocal=new ThreadLocal();
private static final Configuration cfg=new Configuration();
private static SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException{
Session session=(Session)threadLocal.get();
if(session==null) {
if(sessionFactory==null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory=cfg.buildSessionFactory();
}
catch (Exception e) {
// TODO: handle exception
System.out.println("生成SessionFactory失败!!!");
e.printStackTrace();
}
}
session=sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException{
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if(session!=null) {
session.close();
}
}
}
OneOneDAO.java
package DAO;
import addHibFile.HibernateSessionFactory;
import org.hibernate.*;
import PO.*;
public class OneOneDAO {
//添加会员的方法
public void addCompany(Company company) {
Session session=HibernateSessionFactory.currentSession();
Transaction ts=null;
try {
ts=session.beginTransaction();
session.save(company);
ts.commit();
}catch (Exception e) {
// TODO: handle exception
ts.rollback();
System.out.println("系统错误:在OneOneDAO的addCompany方法中出错:");
e.printStackTrace();
}finally {
HibernateSessionFactory.closeSession();
}
}
//获取会员信息
public Company loadCompany(Integer id) {
Session session=HibernateSessionFactory.currentSession();
Transaction ts=null;
Company company=null;
try {
ts=session.beginTransaction();
company=(Company)session.get(Company.class, id);
ts.commit();
}catch (Exception e) {
// TODO: handle exception
ts.rollback();
System.out.println("系统错误:在OneOneDAO中的loadCompany方法出错:");
e.printStackTrace();
}finally {
HibernateSessionFactory.closeSession();
}
return company;
}
//添加客户信息
public void addClient(Client client) {
Session session=HibernateSessionFactory.currentSession();
Transaction ts=null;
try {
ts=session.beginTransaction();
session.save(client);
ts.commit();
}catch (Exception e) {
// TODO: handle exception
ts.rollback();
System.out.println("系统错误:在OneOneDAO中的addClient方法中出错:");
e.printStackTrace();
}finally {
HibernateSessionFactory.closeSession();
}
}
//获取客户信息
public Client loadClient(Integer id) {
Session session=HibernateSessionFactory.currentSession();
Transaction ts=null;
Client client=null;
try {
ts=session.beginTransaction();
client=(Client)session.get(Client.class, id);
ts.commit();
}catch (Exception e) {
// TODO: handle exception
ts.rollback();
System.out.println("系统错误:在OneOneDAO中loadClient方法中出错:");
e.printStackTrace();
}finally {
HibernateSessionFactory.closeSession();
}
return client;
}
}
Address.java
package PO;
import java.io.Serializable;
public class Address implements Serializable{
private Integer id;
private String province; //省份
private String city; //城市
private String street; //街道
private String zipcode; //邮编
private Client client; //关联另外一个类
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
}
Address.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 解析文件的DTD -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 映射文件的根目录 -->
<hibernate-mapping package="PO">
<class name="Address" table="address">
<id column="ID" name="id" type="integer">
<generator class="identity"></generator>
</id>
<property name="province" column="PROVINCE" type="string"></property>
<property name="city" column="CITY" type="string"></property>
<property name="street" column="STREET" type="string"></property>
<property name="zipcode" column="ZIPCODE" type="string"></property>
<!-- 映射Client与Address的一对一外键关联 -->
<one-to-one name="Client" class="PO.Client" property-ref="address"></one-to-one>
</class>
</hibernate-mapping>
Client.java
package PO;
import java.io.Serializable;
public class Client implements Serializable{
private Integer id;
private String clientname; //客户名称
private String phone; //客户电话
private String email; //客户邮箱
private Address address; //关联另外一个PO
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getClientname() {
return clientname;
}
public void setClientname(String clientname) {
this.clientname = clientname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
cliemt.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 解析文件的DTD -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 映射文件的根目录 -->
<hibernate-mapping package="PO">
<class name="Client" table="client">
<id column="ID" name="id" type="integer">
<generator class="identity"></generator>
</id>
<property column="CLIENTNAME" name="clientname" type="string"></property>
<property column="PHONE" name="phone" type="string"></property>
<property column="EMAIL" name="email" type="string"></property>
<!-- 映射Client到Address的一对一外键关联,唯一的多对一,实际上变成了一对一关系 -->
<many-to-one name="address" class="PO.Address" column="address" cascade="all"
lazy="false" unique="true"></many-to-one>
</class>
</hibernate-mapping>
Company.java
package PO;
import java.io.Serializable;
public class Company implements Serializable{
private Integer id;
private String companyname; //单位名称
private String linkman; //单位联系人
private String telephone; //联系电话
private String email; //邮箱
private Login login; //关联另外一个类,即Login类
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCompanyname() {
return companyname;
}
public void setCompanyname(String companyname) {
this.companyname = companyname;
}
public String getLinkman() {
return linkman;
}
public void setLinkman(String linkman) {
this.linkman = linkman;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Login getLogin() {
return login;
}
public void setLogin(Login login) {
this.login = login;
}
}
Company.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 解析文件的DTD -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 映射文件的根目录 -->
<hibernate-mapping package="PO">
<class name="Company" table="company">
<id column="ID" name="id" type="integer">
<!-- 自增 -->
<generator class="identity"></generator>
</id>
<property name="companyname" column="COMPANYNAME" type="string"></property>
<property name="linkman" column="LINKMAN" type="string"></property>
<property name="telephone" column="TELEPHONE" type="string"></property>
<property name="email" column="EMAIL" type="string"></property>
<!-- 映射Company与Login的一对一主键关联 -->
<one-to-one name="login" cascade="all" class="PO.Login" lazy="false" fetch="join"
outer-join="true"></one-to-one>
</class>
</hibernate-mapping>
Login.java
package PO;
import java.io.Serializable;
public class Login implements Serializable{
private Integer id;
private String loginname; //登陆账号
private String loginpwd; //登陆密码
private Company company; //关联另外一个类Company
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getLoginpwd() {
return loginpwd;
}
public void setLoginpwd(String loginpwd) {
this.loginpwd = loginpwd;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
}
Login.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 解析文件的DTD -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 映射文件的根目录 -->
<hibernate-mapping package="PO">
<class name="Login" table="login">
<id column="ID" name="id" type="integer">
<generator class="foreign">
<param name="property">company</param>
</generator>
</id>
<property name="loginname" column="LOGINNAME" type="string"></property>
<property name="loginpwd" column="LOGINPWD" type="string"></property>
<!-- 映射Company与Login的一对一关联 -->
<one-to-one name="company" class="PO.Company" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
TextBean.java
package text;
import PO.*;
import DAO.OneOneDAO;
public class TextBean {
OneOneDAO dao=new OneOneDAO();
//添加会员信息
public void addCompany() {
Company company=new Company();
Login login=new Login();
login.setLoginname("QQ");
login.setLoginpwd("123");
company.setCompanyname("清华大学出版设");
company.setLinkman("某某某");
company.setTelephone("123456754");
company.setEmail("1234543qq.com");
//持久化对象之间相互设置关联关系
login.setCompany(company);
company.setLogin(login);
dao.addCompany(company);
}
//获取会员信息
public Company loadCompany(Integer id) {
return dao.loadCompany(id);
}
//添加客户信息
public void addClient() {
Client client=new Client();
Address address=new Address();
address.setCity("杭州");
address.setProvince("杭州市");
address.setStreet("红枫街道");
address.setZipcode("100324");
client.setClientname("hjw");
client.setEmail("1235543qq.com");
client.setPhone("23123");
//PO对象之间相互设置关联关系
address.setClient(client);
client.setAddress(address);
dao.addClient(client);
}
//获取客户信息
public Client loadClient(Integer id) {
return dao.loadClient(id);
}
}
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="text.TextBean" %>
<%@page import="PO.*" %>
<html>
<head>
<title>Hibernate5的一对一关系映射</title>
</head>
<body>
<h2>Hibernate5的一对一关联映射</h2>
<hr>
<!-- 调用TextBean测试 -->
<jsp:useBean id="test" class="text.TextBean"></jsp:useBean>
<%
test.addCompany();
out.println("添加一个公司<br>");
test.addClient();
out.println("添加一个客户<br>");
Integer id=new Integer(1);
Company company=test.loadCompany(id);
out.println("加载id为1的客户<br>");
Login login=company.getLogin();
out.println("获取公司的登陆账号<br>");
Client client=test.loadClient(id);
out.println("获取id为1的客户<br>");
Address address=client.getAddress();
out.println("获取该客户地址");
out.println("<br>company.getCompanyname()="+company.getCompanyname());
out.println("<br>company.getEmail()="+company.getEmail());
out.println("<br>company.getLinkman()="+company.getLinkman());
out.println("<br>company.getTelephone()="+company.getTelephone());
out.println("<br>login.getLoginname()="+login.getLoginname());
out.println("<br>login.getLoginpwd()="+login.getLoginpwd());
out.println("<br>");
out.println("<br>client.getClientname()="+client.getClientname());
out.println("<br>client.getPhone()="+client.getPhone());
out.println("<br>client.getEmail()="+client.getEmail());
out.println("<br>address.getProvince()="+address.getProvince());
out.println("<br>address.getCity()="+address.getCity());
out.println("<br>address.getStreet()="+address.getStreet());
out.println("<br>address.getZipcode()="+address.getZipcode());
%>
</body>
</html>
运行结果: