具有持久性的Java Hibernate问题---如果未定义FetchType,那么默认方法是什么? [英] Java Hibernate with Persistence Question---if FetchType is not defined, what is the default method?

查看:500
本文介绍了具有持久性的Java Hibernate问题---如果未定义FetchType,那么默认方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我是Hibernate和JPA的新手



我写了一些函数,最初我在 fetch = FetchType.LAZY 实体类。
但它给了我错误:
org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话

  @OneToMany(cascade = CascadeType.ALL,mappedBy =logins,fetch = FetchType.LAZY,targetEntity = Invoice.class)
public List< Invoice> getInvoiceList(){
返回invoiceList;
}

public void setInvoiceList(List< Invoice> invoiceList){
this.invoiceList = invoiceList;
}

然后我将它改为fetch = FetchType.EAGER,它工作正常。 ...
我想知道如果我不声明FetchType会发生什么,Hibernate是否确定使用哪种方法?或者它是由EAGER默认的?

  @OneToMany(cascade = CascadeType.ALL,mappedBy =logins,fetch = FetchType .EAGER,targetEntity = Invoice.class)
public List< Invoice> getInvoiceList(){
返回invoiceList;
}

public void setInvoiceList(List< Invoice> invoiceList){
this.invoiceList = invoiceList;
}

谢谢!!!!!!!!!

解决方案


我在想如果我不声明 FetchType ,Hibernate确定自己使用哪种方法?或者它是由EAGER默认的?


实际上,这种行为不是Hibernate特有的,但是由JPA规范定义,你会发现 OneToMany 注释或来源。来源:


  / **(可选)关联是否应该是
*懒惰加载或必须热切地提取。
* {@link FetchType#EAGER EAGER}策略是持久性提供程序运行时
*的
*要求,即必须急切地提取相关性。
* {@link FetchType#LAZY LAZY}策略是对持久性提供者运行时的提示
*。
* /
FetchType fetch()默认LAZY;


就是说,尽管 FetchType.EAGER ,使用 EAGER 来避免 LazyInitializationException (当您尝试在分离的对象上加载一个惰性关联时发生)比实际的解决方案更重要。

Hi guys I am new to Hibernate and JPA

I wrote some functions, initially, I set fetch = FetchType.LAZY in the entity class. But it gave me error: "org.hibernate.LazyInitializationException: could not initialize proxy - no Session"

@OneToMany(cascade = CascadeType.ALL, mappedBy = "logins", fetch=FetchType.LAZY,targetEntity=Invoice.class)
public List<Invoice> getInvoiceList() {
    return invoiceList;
}

public void setInvoiceList(List<Invoice> invoiceList) {
    this.invoiceList = invoiceList;
}

Then I changed it to fetch = FetchType.EAGER, it worked fine..... I am wondering what happen if I don't declare FetchType, does Hibernate determine itself which method to use? Or is it defaulted by EAGER??

@OneToMany(cascade = CascadeType.ALL, mappedBy = "logins", fetch=FetchType.EAGER,targetEntity=Invoice.class)
public List<Invoice> getInvoiceList() {
    return invoiceList;
}

public void setInvoiceList(List<Invoice> invoiceList) {
    this.invoiceList = invoiceList;
}

THANKS!!!!!!!!!

解决方案

I am wondering what happens if I don't declare FetchType, does Hibernate determine itself which method to use? Or is it defaulted by EAGER??

Actually, this behavior is not Hibernate specific but defined by the JPA specification and you'd find the answer in the spec or in the javadoc of the OneToMany annotation or the sources. From the sources:

/** (Optional) Whether the association should be
 * lazily loaded or must be eagerly fetched. The
 * {@link FetchType#EAGER EAGER} strategy is a 
 * requirement on the persistenceprovider runtime 
 * that the associatedentities must be eagerly fetched. 
 * The {@link FetchType#LAZY LAZY} strategy is a hint 
 * to the persistence provider runtime.
 */
FetchType fetch() default LAZY;

That being said, while there are very legitimate use cases for FetchType.EAGER, using EAGER just to avoid the LazyInitializationException (which occurs when you try to load a lazy association on a detached object) is more a work around than a real solution.

这篇关于具有持久性的Java Hibernate问题---如果未定义FetchType,那么默认方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆