Hibernate学习(二)关系映射----基于外键的单向一对一

事实上,单向1-1与N-1的实质是相同的,1-1是N-1的特例,单向1-1与N-1的映射配置也非常相似。只需要将原来的many-to-one元素增加unique="true"属性,用于表示N的一端也必须是唯一的,在N的一端增加了唯一的约束,即成为单向1-1。基于外键的单向1-1的配置将与无连接表N-1关联的many-to-one增加unique="true"属性即可。

一、模型介绍

一个人(account)对应一个地址(address)。

二、实体

account类

package entity;

import java.io.Serializable;
public class account implements Serializable{
public account(){ }
// 建议hibernate中不用使用id,name这种关键字 可能会报错
private int account_id;
private String account_name;
private String password;
//需要添加被控端的引用 此处添加的是对应关系 即一个人对应一个地址的地址对象
private address address; public int getAccount_id() {
return account_id;
}
public void setAccount_id(int account_id) {
this.account_id = account_id;
}
public String getAccount_name() {
return account_name;
}
public void setAccount_name(String account_name) {
this.account_name = account_name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public address getAddress() {
return address;
}
public void setAddress(address address) {
this.address = address;
}
}

address实体类

package entity;

import java.io.Serializable;

public class address implements Serializable{
public address(){
}
private int address_id;
private String address_name; public int getAddress_id() {
return address_id;
}
public void setAddress_id(int address_id) {
this.address_id = address_id;
}
public String getAddress_name() {
return address_name;
}
public void setAddress_name(String address_name) {
this.address_name = address_name;
}
}

映射关系配置
account.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "entity.account" table ="account">
<id column = "account_id" name = "account_id">
<generator class="native"/> <!--主键生成方式 所以在创建表的时候要主键自增
--> </id>
<property name="account_name" type = "java.lang.String" column = "account_name"/>
<property name="password" type="java.lang.String" column = "password"/>
<!-- 通过many-to-one 标签添加唯一属性约束,建立一对一关联关系
此处映射的意思是 account的adress属性对应着外表的address_id列 且填加了唯一约束 说明address_id是唯一的 -->
<many-to-one name="address" column="address_id" unique="true"/>
<!-- 必须通过映射关系生成的表才会生成唯一约束,否则不会生成 -->
</class>
</hibernate-mapping>

address.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "entity.address" table = "address">
<id name="address_id" column="address_id">
<generator class="native"/>
</id>
<!-- generated= never 标明此属性值不是从数据库中生成。generated默认值是never。
generated= insert 标明此属性值在insert的时候生成,但是不会在随后的update时重新生成。
generated= always 标明此属性值在insert和update时都会被生成
-->
<property name="address_name" type = "java.lang.String" column = "address_name" generated="never" lazy="false"/>
</class>
</hibernate-mapping>

这里讲通用逻辑封装成了一个方法了

/**
* @author Administrator
*
*/
package HibernateUtil; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtil {
private static SessionFactory factory;
static{
Configuration cnn = new Configuration().configure();
factory = cnn.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return factory;
}
public static Session getOneSession(){
return factory.openSession();
}
public static void ClossSession(Session session){
if(session != null){
session.close();
}
}
}

建表就不说了但是注意不要忘记创建自增和索引名字必须是Hibernate.sequence .
hibernate.hbm.xml同Hibernate学习一中的配置只是添加了映射文件 <mapping resource="account.hbm.xml"/><mapping resource="address.hbm.xml"/>

测试

package text;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import entity.account;
import entity.address; import HibernateUtil.HibernateUtil; public class textManytoOne { @Test
public void add(){
Transaction tx = null;
Session session = HibernateUtil.getOneSession();
tx=session.beginTransaction(); address address = new address();
address.setAddress_name("天津");
session.save(address); account account = new account();
account.setAccount_name("大伟哥666");
account.setPassword("123456");
account.setAddress(address); // 重复添加 account account1 = new account();
account1.setAccount_name("小老虎666");
account1.setPassword("123456");
account1.setAddress(address); try{
session.save(account);
session.save(account1);
tx.commit();
}catch(HibernateException e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtil.ClossSession(session);
}
}
}

总结注意
1.在多表实体类中记得添加被控端的对象的引用 且属性名字最好不用和关键字冲突

2.要在多表的映射文件中写上<many-to-one name="被控端引用对象" column="外键 也就是另一张表的主键" unique="是否对外键加唯一">

3.创建表的时候主键索引名字是Hibernate.sequence 不知道为什么  但是我的名字不是这个就报错

4.Hibernate的配置文件中记得添加实体类的映射文件 <mapping resource="xxx.hbm.xml">

上一篇:《JAVA程序设计与实例》记录与归纳--继承与多态


下一篇:Linux 文件系统错误的修复方法 ddrescue替代dd的恢复软件 备用超级块