简体   繁体   中英

Hibernate join takes too long to work

Here is my problem:

I have this class it has few @oneToMany collections

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 ...

When I access this object from XHTML it takes too long to generate as I use <ui:repeat value=#{controller.ActivePropertyList.attributeList}> ...

PropertyAttributeLink has more than 5Mil rows and Images has more than 4Mil rows but when i use simple SQL query innerJoin i takes no more than few ms to generate Lists.

I've tried using namedQuery on AttributeList using HQL query but as AttributeList has no reference to ActivePropertyList as it is unidirectional @oneToMany it throws error on doing so.

Is there a way to create HQL NamedQuery to access each list just once and store it in controller?

something like

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

but this method doesn't work as hql needs AttributeList to know about PropertyAttributeLink

Pointing the joins just make the atributtes available for where conditions and other stuff, you should use FETCH to make the relations eager and have it inmediatly, avoiding the lazy iniciators, something like

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

As you see isn't so hard, i hope that helps you, you can get more information, as always, in the docs HQL - Associations and joins

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM