Hibernate注解---抓取策略

 

package com.gc.sis.core;

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

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name="illu")
@XmlRootElement
public class Illu {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	
	private int singlePrem;
	
	//级联保存,删除等
	//FetchType.EAGER 不延迟,立即加载关联对象
	@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	private Set<Client> clients;
	
	//级联保存,删除等
	//FetchType.EAGER 不延迟,立即加载关联对象
	@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	private Set<Rider> riders;
	
	
	public Illu() {
		super();
	}



	public int getId() {
		return id;
	}

	public int getSinglePrem() {
		return singlePrem;
	}


	public void setSinglePrem(int singlePrem) {
		this.singlePrem = singlePrem;
	}


	public Set<Client> getClients() {
		if(clients==null)
			clients = new HashSet<Client>();
		return clients;
	}


	public void setClients(Set<Client> clients) {
		this.clients = clients;
	}


	public Set<Rider> getRiders() {
		if(riders==null)
			riders = new HashSet<Rider>();
		return riders;
	}


	public void setRiders(Set<Rider> riders) {
		this.riders = riders;
	}
	
	
}

 

多对一双向关联,使用mappedBy=“xxx” ,即inverse=true的效果

 

 

@LazyCollection(LazyCollectionOption.EXTRA)
    private Set<Rider> riders;

 

 

抓取策略

在一的一端单个抓取关联对象(1个Group对象,去抓取其关联的ContactPerson对象)

@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    private Set<Client> clients;

在一的一端批量抓取关联对象(1批Group对象(List<Group>),去抓取其关联的ContactPerson对象)

@Fetch(FetchMode.SUBSELECT)
    private Set<Client> clients;

你可能感兴趣的:(Hibernate)