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 clients;
	
	//级联保存,删除等
	//FetchType.EAGER 不延迟,立即加载关联对象
	@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	private Set riders;
	
	
	public Illu() {
		super();
	}



	public int getId() {
		return id;
	}

	public int getSinglePrem() {
		return singlePrem;
	}


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


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


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


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


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

 

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

 

 

@LazyCollection(LazyCollectionOption.EXTRA)
    private Set riders;

 

 

抓取策略

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

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

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

@Fetch(FetchMode.SUBSELECT)
    private Set clients;

你可能感兴趣的:(框架@Hibernate,java)