【问题标题】:Hibernate join takes too long to work休眠加入需要太长时间才能工作
【发布时间】:2012-04-17 00:39:09
【问题描述】:

这是我的问题:

我有这门课它很少有@oneToMany 集合

public class ActivePropertyList implements Serializable 
{

 @OneToMany
 @JoinTable(name = "PropertyAttributeLink",
 joinColumns =
 @JoinColumn(name = "EANHotelID"),
 inverseJoinColumns =
 @JoinColumn(name = "AttributeID", referencedColumnName="AttributeID"))
 private Collection<AttributeList> attributeList;

 @OneToMany(fetch= FetchType.LAZY)
 @JoinColumn(name="EANHotelID")
 private Collection<Hotelimageslist> hotelimageslist;

 @OneToMany(fetch= FetchType.LAZY)
 @JoinColumn(name="EANHotelID")

 private Collection<Roomtypelist> roomtypelist;
//Getters & Setters ...

当我从 XHTML 访问这个对象时,生成时间太长,因为我使用 &lt;ui:repeat value=#{controller.ActivePropertyList.attributeList}&gt; ...

PropertyAttributeLink 有超过 500 万行,图像有超过 400 万行,但是当我使用简单的 SQL 查询 innerJoin 时,生成列表的时间不超过几毫秒。

我尝试使用 HQL 查询在 AttributeList 上使用 namedQuery,但由于 AttributeList 没有引用 ActivePropertyList,因为它是单向的@oneToMany,这样做会引发错误。

有没有办法创建 HQL NamedQuery 来访问每个列表一次并将其存储在控制器中?

类似

public List<AttributeList> getAttributeListByHotelID(int hotelID){
    Query q = session().createQuery("from AttributeList AL inner join PropertyAttributeLink PA where PA.hotelID=:hotelID");
    return q.list();
}

但是这个方法不起作用,因为 hql 需要 AttributeList 来了解 PropertyAttributeLink

【问题讨论】:

    标签: java hibernate jakarta-ee jsf-2


    【解决方案1】:

    指向连接只是使属性可用于条件和其他东西,您应该使用 FETCH 使关系急切并立即拥有它,避免懒惰的发起者,例如

    from AttributeList AL inner join FETCH PropertyAttributeLink PA where PA.hotelID=:hotelID
    

    正如您所见并不难,希望对您有所帮助,您可以一如既往地在文档HQL - Associations and joins中获得更多信息

    【讨论】:

      猜你喜欢
      • 2022-12-23
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      相关资源
      最近更新 更多