在jpa中jpa默认的加载方式是lazy方式也就是在实际使用到数据的时候才加载相关数据,使用lazy时可以不用显示注明fetch=FetchType.LAZY
实体bean:carage
package com.hibernate.jpa.bean1;
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.OneToMany;
@Entity
public class Garage {
/**
* many to one 多对一
*/
private Integer gid;
private String garagenum;
private Set<Auto> autos = new HashSet<Auto>();
@Id @GeneratedValue
public Integer getGid() {
return gid;
}
public void setGid(Integer gid) {
this.gid = gid;
}
@Column(length=20)
public String getGaragenum() {
return garagenum;
}
public void setGaragenum(String garagenum) {
this.garagenum = garagenum;
}
@OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage")
public Set<Auto> getAutos() {
return autos;
}
public void setAutos(Set<Auto> autos) {
this.autos = autos;
}
public void addGarageAuto(Auto auto) {
auto.setGarage(this);
this.autos.add(auto);
}
}
实体bean:auto
package com.hibernate.jpa.bean1;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Auto {
/**
* one to many 一对多关联
*/
private Integer autoId;
private String autotype;
private String autonum;
private Garage garage;
@Id @GeneratedValue
public Integer getAutoId() {
return autoId;
}
public void setAutoId(Integer autoId) {
this.autoId = autoId;
}
public String getAutotype() {
return autotype;
}
public void setAutotype(String autotype) {
this.autotype = autotype;
}
public String getAutonum() {
return autonum;
}
public void setAutonum(String autonum) {
this.autonum = autonum;
}
@ManyToOne()
@JoinColumn(name="garageid")
public Garage getGarage() {
return garage;
}
public void setGarage(Garage garage) {
this.garage = garage;
}
}
junit的测试方法
@Test public void query() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate");
EntityManager em = factory.createEntityManager();
Garage garage = em.find(Garage.class, 1);
em.close();
factory.close();
}
调用query方法的时候发出的sql语句是:
Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?
也就是仅仅获取了garage对象,而没有获取与garage关联的auto对象
-----------------
(二)在Garage.java中添加fetch=FetchType.EAGER字段
@OneToMany(cascade={CascadeType.PERSIST},fetch=FetchType.EAGER,mappedBy="garage")
public Set<Auto> getAutos() {
return autos;
}
再次运行query方法,这一次发出的sql语句是:
Hibernate: select garage0_.gid as gid1_1_, garage0_.garagenum as garagenum1_1_, autos1_.garageid as garageid3_, autos1_.autoId as autoId3_, autos1_.autoId as autoId0_0_, autos1_.autonum as autonum0_0_, autos1_.autotype as autotype0_0_, autos1_.garageid as garageid0_0_ from Garage garage0_ left outer join Auto autos1_ on garage0_.gid=autos1_.garageid where garage0_.gid=?
这一次由于将jpa默认的抓取策略改为fetch=FetchType.EAGER
所以jpa在加载数据的时候一次性的加载了和garage相关联的数据
说明:由于fetch=FetchType.EAGER加载数据的时候是一次性加载可能会造成不必要的性能浪费,使用是应该慎重考虑
分享到:
相关推荐
Hibernate4之JPA规范配置详解
04_JPA详解_第一个JPA实例与JPA主键生成策略.zip
入门级别的jpa 详细解答,教程从简单到入门,容易上手
JPA注解 详解 1、@Entity(name="EntityName") 必须,name为可选,对应数据库中一的个表 2、@Table(name="",catalog="",schema="") 可选,通常和@Entity配合使用,只能标注在实体的class定义处,表示实体对应的数据库表...
jpa映射关系详解[参照].pdf
JPA JAPJPA JAPJPA JAPJPA JAPJPA JAPJPA JAPJPA JAPJPA JAPJPA JAPJPA JAP
java程序使用JPA注解详解 JPA注解详解 java 程序使用 JPA注解 详解
里面介绍了一些JPA的基础知识,适合想了解JPA的大虾门
04_传智播客JPA详解_第一个JPA实例与JPA主键生成策略
JPA注解详解 默认情况下,JPA 持续性提供程序假设实体的所有持久字段均存储到一个名称为实体名称的数据库表中(请参阅 @Entity )。 在以下条件下,使用 @Table 批注指定与实体关联的主表:
JPA 详解,详细介绍了JPA的使用,从基本概念到具体例子
JPA用于整合现有的ORM技术,可以简化现有Java EE和Java SE应用对象持久化的开发工作,实现ORM的统一。JPA详解视频教程 第4讲 第一个JPA实例与JPA主键生成策略.avi
JPA主键策略(针对数据库自增字段重置后无效检查项) JPA主键生成策略会影响数据库自增字段的重置
java程序使用JPA注解详解.zip java程序使用JPA注解详解.zip java程序使用JPA注解详解.zip java程序使用JPA注解详解.zip java程序使用JPA注解详解.zip
总结一下关于JPA的主键生成策略,JPA是用@GeneratedValue标记来注释的。一般的我把主键生成分成两大类。第一个就是简单的单字段主键类型,一个就是复杂的复合主键类型。我们分2种情况分别讨论。 第一种单字段主键...
java程序使用JPA注解详解.doc
只要1分就可获得JPA主键生成策略,很全的哦
07_JPA详解_使用JPA加载_更新_删除对象.zip 07_JPA详解_使用JPA加载_更新_删除对象.zip
05_JPA详解_日期_枚举等字段类型的JPA映射.zip 05_JPA详解_日期_枚举等字段类型的JPA映射.zip
EJB+JPA数据库持久层开发实践详解.pdf 完整版