Hibernate的关联映射(2)-单向1-1关联

单向1-1关联

对于单向的1-1关联关系,需要在持久化类里增加代表关联实体的成员变量,并为该成员变量增加setter和getter方法,从持久化类的代码看,单向1-1和单向N-1没有丝毫区别,因为N的一端或者1的一端都是直接访问关联实体,只需要增加代表关联实体的属性即可;对于1-1关联,无论是单向还是双向,都需要使用@OneToOne修饰代表关联实体的属性,可以使用的属性如下:

cascade:(非必须属性)指定Hibernate对关联实体采用怎样的级联策略,该级联策略支持如下五个属性:

1.CascadeType.ALL:指定Hibernate将多有的持久化操作都级联到关联实体;

2.CascadeType.MERGE:指定Hibernate将merge操作级联到关联实体;

3.CascadeType.PERSIST:指定Hibernate将persist操作级联到关联实体;

4.CascadeType.REFRESH:指定Hibernate将refresh操作级联到关联实体;

5.CascadeType.REMOVE:指定Hibernate将remove操作级联到关联实体;

fetch:(非必须属性)指定抓取关联实体时的抓取策略,该属性支持如下两个属性值:

1.FetchType.EAGER:抓取实体时,立即抓取关联实体,这是默认值;

2.FetchType.LAZY:抓取实体时,延迟抓取关联实体,等到正真用到关联实体时采取抓取;

optional:(非必须属性)该属性指定关联关系是否可选

targetEntity:(非必须属性)该属性指定关联实体的类名,在默认情况下,Hibernate将通过反射来判断关联实体的类名;

mappedBy:(非必须属性)该属性合法的属性值为关联实体的属性名,该属性指定关联实体中哪个属性可引用到当前实体;

orphanRemoval:(非必须属性)该属性设置是否删除"孤儿"实体,如果某个实体所关联的父实体不存在,(即该实体对应记录的外键为null),该实体就是所谓的"孤儿"实体;


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1>基于外键的单向1-1关联

对于基于外键的1-1关联,只要使用@OneToOne注解修饰代表关联实体的属性,再使用@JoinColumn映射外键列即可,由于是1-1关联,因此应该为@JoinColumn增加unique=true;

Person实体类

<span style="font-size:18px;">package com.anlw.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name="person_inf")
public class Person{
	//标识属性
	@Id
	@Column(name="person_id1")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	private String name;
	private int age;
	//定义该Person实体关联的Address实体
	@OneToOne(targetEntity=Address.class)
	//映射名为address_id的外键列,参照关联实体对应表的addrsss_id主键列
	@JoinColumn(name="address_id2",referencedColumnName="address_id3",unique=true)
	@Cascade(CascadeType.ALL)
	private Address address;
	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 int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	
}
</span>
对Person实体的说明:

在添加级联操作注解有两种方式:

1,@Cascade(CascadeType.ALL)直接添加一个单独的注解,关键是使用的包为

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

2,@OneToOne(cascade={CascadeType.ALL},targetEntity=Address.class)添加到这个注解里面,关键是使用的包为

import javax.persistence.CascadeType;

import javax.persistence.OneToOne;

这句注解@JoinColumn(name="address_id4",referencedColumnName="address_id11d",unique=true)可以改为@JoinColumn(name="address_id4"),hibernate会自动反射识别;

Address实体类

<span style="font-size:18px;">package com.anlw.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="address_inf")
public class Address {
	//标识属性
	@Id
	@Column(name="address_id3")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer addressId;
	//定义地址详细的成员变量
	private String addressDetail;
	//无参构造器
	public Address(){}
	//初始化全部成员变量的构造器
	public Address(String addressDetail) {
		this.addressDetail = addressDetail;
	}
	public Integer getAddressId() {
		return addressId;
	}
	public void setAddressId(Integer addressId) {
		this.addressId = addressId;
	}
	public String getAddressDetail() {
		return addressDetail;
	}
	public void setAddressDetail(String addressDetail) {
		this.addressDetail = addressDetail;
	}
	
}
</span>
核心测试代码
<span style="font-size:18px;">package com.anlw.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.anlw.entity.Address;
import com.anlw.entity.Person;

public class Test {
	public static void main(String[] args) {
		Configuration conf = new Configuration().configure();
		ServiceRegistry st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
		SessionFactory sf = conf.buildSessionFactory(st);
		Session sess = sf.openSession();
		Transaction tx = sess.beginTransaction();
		Person p = new Person();
		Address a = new Address("guangzhou");
		p.setName("crazyit.org");
		p.setAge(11);
		p.setAddress(a);
		sess.persist(p);
		Address a2 = new Address("shanghai");
		p.setAddress(a2);
		tx.commit();
		sess.close();
		sf.close();

	}
}
</span>

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2>有连接表的单向1-1关联

有连接表的1-1关联同样需要显示使用@JoinTable映射连接表,除此之外,由于此处的@JoinTable映射的连接表维护的是1-1关联,因此程序需要为@JoinTable中的joinColumn属性映射的外键增加unique=true,也为inverseJoinColumns属性映射的外键列增加unique-true;

实体类Address和核心代码和上面一样,只有Person如下:

<span style="font-size:18px;">package com.anlw.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;



@Entity
@Table(name="person_inf")
public class Person{
	//标识属性
	@Id
	@Column(name="person_id1")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	private String name;
	private int age;
	//定义Pseson实体关联的Address实体
	@OneToOne(targetEntity=Address.class)
	@JoinTable(name="p_a",
	joinColumns=@JoinColumn(name="p_id",
	referencedColumnName="person_id1",unique=true),
	inverseJoinColumns=@JoinColumn(name="a_id",referencedColumnName="address_id11d",unique=true))
	@Cascade(CascadeType.ALL)
	private Address address;
	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 int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	
}</span>
上面连接表中两个外键列都增加了unique=true,这就保证了一个address实体最多只能关联一个person实体;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值