JPA的FetchType.LAZY不起作用?

时间:2013-05-06 11:35:32

标签: jpa eclipselink lazy-loading

我的提供者是: org.eclipse.persistence.jpa.PersistenceProvider

我的实体代码是:

@Entity
@NamedQueries( { @NamedQuery(name = "Bank.findAll", query = "select o from Bank o") })
@Table(name = "B_BANK")
public class Bank implements Serializable {

private final static String ID_GENERATOR = "Bank";

private Long id;
private User modifier;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_GENERATOR)
@SequenceGenerator(name = ID_GENERATOR, allocationSize = 1, sequenceName = "sq_B_BANK")
@Column(name = "id")
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "modifier")
public User getModifier() {
    return modifier;
}

public void setModifier(User modifier) {
    this.modifier = modifier;
}

}

我的测试代码只是:

    entityManagerHelper = new EntityManagerHelper("Model", isAutoCommit);

   List l = entityManagerHelper.getEntityManager().createQuery("select b from Bank b").getResultList();

然而,在我的控制台中我有这个:

SELECT id,modifier FROM B_BANK SELECT id,CREATION_DATE,MODIFICATION_DATE,修饰符,创建者FROM B_USER WHERE(id =?)     bind => [2]

因此,似乎JPA向用户询问我将fetch = FetchType.LAZY放在上面! 我想知道为什么JPA也会询问用户。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

虽然答案是正确的,但EclipseLink使用编织来实现OneToOne和ManyToOne关系的延迟提取。如果您不是在容器环境中运行,那么您必须添加代理或设置您的实体以静态编织以在1:1上使用延迟,如下所述:http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Dynamic_Weaving和此处http://www.eclipse.org/eclipselink/documentation/2.4/concepts/app_dev007.htm