JPA--三种关系的处理

一对多关系:男人经常幻想自己有这个,一个男对应上N个女人,可惜在数据库里面这个关系的维护jpa规范却交给了多的一方来维护,男的是被维护端....

代码附加:

mappedBy来声明为被维护端,指定的值是关系维护端由哪个属性去维护这个关系,类似hibernate里面<setname="items"inverse="true"/>

多对一这边也需要声明manytoone

optional=false

不可选代表必须要存在,不能为null

多对以要设置外键名称,@JoinColumn

hibernate<manytonoecolumn="">

这里面叫order和关键字冲突.通过table来修改

事物问题

你对这个数据进行处理,当处理完却被别人修改了.如果你再查询,会从缓存把你那个依旧拿出来,如果你要调用最新只能refresh(person)

如果后面是many默认就延迟加载,其他是立即加载.

附上此代码:

package com.itcast.domail;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;

import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="t_order")
public class Order {

	private String orderid;
	private Float  amount;
	private Set<OrderItem> items=new HashSet<OrderItem>();
	@OneToMany(cascade={CascadeType.REMOVE,CascadeType.MERGE,CascadeType.PERSIST },
			fetch=FetchType.LAZY,mappedBy="order")
	
	public Set<OrderItem> getItems() {
		return items;
	}
	public void setItems(Set<OrderItem> items) {
		this.items = items;
	}
	@Id
	@Column(length=12)
	public String getOrderid() {
		return orderid;
	}
	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}
	@Column(nullable=false)
	public Float getAmount() {
		return amount;
	}
	public void setAmount(Float amount) {
		this.amount = amount;
	}
	//下面这个方法说明了两者在数据库里的关系
	public void addOrderItem(OrderItem orderItem){
		orderItem.setOrder(this);
		this.items.add(orderItem);
	}
	
}

一对一,和谐的夫妻制度,有人说着是性能力差的人想出来的,不知道说这话的人性能力咋样.....

一对一怎么决定维护端,jpa没规定,自己决定

附上代码

package com.itcast.domail;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class IDCard {
	
	private Integer id;
	private	String cardId;
	private Student student;
	//关系的被维护端,由业务需要决定级联
	@OneToOne(mappedBy="idCard_id",cascade={CascadeType.PERSIST,CascadeType.MERGE,
			},optional=false)
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}@Column(length=18,nullable=false)
	public String getCardId() {
		return cardId;
	}
	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

}
package com.itcast.domail;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Student {
	private Integer id;
	private String name;
	private IDCard idCard_id;
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(length=10,nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne(cascade={CascadeType.REMOVE,CascadeType.PERSIST,CascadeType.MERGE})
	//关系维护端定义外键
	@JoinColumn(name="card_id",nullable=false)
	public IDCard getIdCard_id() {
		return idCard_id;
	}
	public void setIdCard_id(IDCard idCard_id) {
		this.idCard_id = idCard_id;
	}
	public void addCard(IDCard card){
		card.setStudent(this);
		this.setIdCard_id(card);
	}

}

开放的社会,现在玩的都是你出轨,我也出轨,男女关系很复杂......

代码附加:

双向多对多关系

数据库里面没有这个,通过中间表来搞这个.

删除时:先解除关系再删除

package com.itcast.domail;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Girl {
	private String name;
	private Integer id;
	private Set<Boy> boyFriends=new HashSet<Boy>();
	@ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.EAGER
			,mappedBy="girlFriends")
	public Set<Boy> getBoyFriends() {
		return boyFriends;
	}
	public void setBoyFriends(Set<Boy> boyFriends) {
		this.boyFriends = boyFriends;
	}
	@Column(nullable=false,length=10)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Girl other = (Girl) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	

}
package com.itcast.domail;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Boy {
	
	private String name;
	private Integer id;
	private Set<Girl> girlFriends=new HashSet<Girl>();
	@ManyToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST})
	@JoinTable(name="boy_girl",joinColumns=@JoinColumn(name="boy_id"),
			inverseJoinColumns=@JoinColumn(name="girl_id"))
	public Set<Girl> getGirlFriends() {
		return girlFriends;
	}
	public void setGirlFriends(Set<Girl> girlFriends) {
		this.girlFriends = girlFriends;
	}
	@Column(nullable=false,length=10)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void addGirl(Girl girl){
		this.girlFriends.add(girl);
	}
	//需要重写Gril的hashcode与equls方法
	public void removeGirl(Girl girl){
		if(this.girlFriends.contains(girl)){
			this.girlFriends.remove(girl);
		}
	}
	
}

另外附上jpa对联合主键的处理

jpa规定

联合主键:

无参构造

必须要实现序列化接口

必须要重写hashcodeequls方法

package com.itcast.domail;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class AirLinePK implements Serializable{

	private String startCity;
	private String endCity;
	@Column(length=3)
	public String getStartCity() {
		return startCity;
	}
	public void setStartCity(String startCity) {
		this.startCity = startCity;
	}	@Column(length=3)
	public String getEndCity() {
		return endCity;
	}
	public void setEndCity(String endCity) {
		this.endCity = endCity;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((endCity == null) ? 0 : endCity.hashCode());
		result = prime * result
				+ ((startCity == null) ? 0 : startCity.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final AirLinePK other = (AirLinePK) obj;
		if (endCity == null) {
			if (other.endCity != null)
				return false;
		} else if (!endCity.equals(other.endCity))
			return false;
		if (startCity == null) {
			if (other.startCity != null)
				return false;
		} else if (!startCity.equals(other.startCity))
			return false;
		return true;
	}
	
}

相关推荐