2013-05-06 64 views
1

我的供應商是: org.eclipse.persistence.jpa.PersistenceProviderJPA的FetchType.LAZY不起作用?

我的實體代碼:

@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標識,改性劑FROM B_BANK SELECT標識,CREATION_DATE,MODIFICATION_DATE,改性劑,創建者FROM B_USER WHERE(ID =?) 綁定=> [2]

因此似乎JPA用於查詢我把用戶fetch = FetchType.LAZY! 我想知道爲什麼JPA會爲用戶查詢。 任何幫助,將不勝感激。

回答

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

+0

謝謝克里斯 – 2013-05-07 03:56:01

+0

+ 1很酷,不知道 – kostja 2013-05-07 05:55:33