gpt4 book ai didi

java - Hibernate join 工作时间太长

转载 作者:行者123 更新时间:2023-11-29 09:15:08 25 4
gpt4 key购买 nike

这是我的问题:

我有这门课,它很少@oneToMany Collection 品

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 访问此对象时,由于我使用 <ui:repeat value=#{controller.ActivePropertyList.attributeList}> 生成时间太长...

PropertyAttributeLink 有超过 500 万行,Images 有超过 400 万行,但是当我使用简单的 SQL 查询 innerJoin 时,我只需要几毫秒就可以生成列表。

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

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

有点像

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

最佳答案

指向连接只是让属性可用于 where 条件和其他东西,你应该使用 FETCH 使关系变得急切并立即拥有它,避免懒惰的启动器,比如

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

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

关于java - Hibernate join 工作时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9991910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com