Hibernate没有为父/子关系做延迟加载。相反,它在查询时执行2次SQL加载

时间:2014-08-29 12:43:26

标签: hibernate

我有这样的家长/孩子关系:

@Entity
@Table(name = "Agent", schema = "dbo", catalog = "MyTime")
public class Agent implements Serializable {
    private Set<AgentAttr> attributes = new HashSet<AgentAttr>();

    @OneToMany
    @JoinColumn(name="Agent_GUID")
    public Set<AgentAttr> getAttributes() {
        return attributes;
    }

    public void setAttributes(Set<AgentAttr> attributes) {
        this.attributes = attributes;
    }
}

此DAO调用是在Agent上执行查询。

Query query = getSession().createQuery("from Agent where userName = :userName");
query.setParameter("userName", userName);
List list = query.list();

查询返回Agent对象,并在列表的每个元素中加载属性集合。 检查Hibernate日志时,它显示它执行2个SQL查询,一个用于加载代理, 第二个加载属性。

我完全没想到这一点,因为Hibernate文档说延迟加载是默认行为。 我希望查询返回的属性为null。然后Hibernate只在调用Agent.getAttributes()时加载它。 这是延迟加载的重点,即只在需要时加载它,对吗?我也试过在那里明确地放置@OneToMany(fetch = FetchType.LAZY), 但仍然一样,不做懒加载。

欣赏任何我可能做错的指针。感谢。

这是Hibernate日志:

2014/08/28 16:21:14.487 [DEBUG] <http-bio-8080-exec-3> (SqlStatementLogger.logStatement:109) - 
    /* 
from
    Agent 
where
    userName = :userName */ select
        agent0_.Agent_GUID as Agent_GU1_1_,
        agent0_.Address_Email as Address_2_1_,
        agent0_.Address_SIP as Address_3_1_,
        agent0_.Address_TEL as Address_4_1_,
        agent0_.Address_XMPP as Address_5_1_,
        agent0_.Company_GUID as Company_6_1_,
        agent0_.Create_Tstamp as Create_T7_1_,
        agent0_.Create_User as Create_U8_1_,
        agent0_.Enabled as Enabled9_1_,
        agent0_.First_Name as First_N10_1_,
        agent0_.Last_Name as Last_Na11_1_,
        agent0_.Location as Locatio12_1_,
        agent0_.Password as Passwor13_1_,
        agent0_.Role as Role14_1_,
        agent0_.Security_Ans as Securit15_1_,
        agent0_.Security_Q as Securit16_1_,
        agent0_.Update_Tstamp as Update_17_1_,
        agent0_.Update_User as Update_18_1_,
        agent0_.User_Name as User_Na19_1_,
        agent0_.Work_Status as Work_St20_1_ 
    from
        MyTime.dbo.Agent agent0_ 
    where
        agent0_.User_Name=?
2014/08/28 16:21:14.487 [TRACE] <http-bio-8080-exec-3> (BasicBinder.bind:81) - binding parameter [1] as [VARCHAR] - [achansv@contactsolutions.com]
2014/08/28 16:21:14.794 [TRACE] <http-bio-8080-exec-3> (BasicExtractor.extract:78) - extracted value ([Agent_GU1_1_] : [VARCHAR]) - [9E5537A6-96FF-4656-9F75-70B58E44C0E9]
2014/08/28 16:21:20.632 [DEBUG] <http-bio-8080-exec-3> (SqlStatementLogger.logStatement:109) - 
    /* 
from
    AgentAttr 
where
    agentGuid = :agentGuid */ select
        agentattr0_.Agent_Attr_GUID as Agent_At1_4_,
        agentattr0_.Agent_GUID as Agent_GU2_4_,
        agentattr0_.Attr_Category as Attr_Cat3_4_,
        agentattr0_.Attr_Name as Attr_Nam4_4_,
        agentattr0_.Attr_Value as Attr_Val5_4_,
        agentattr0_.Create_Tstamp as Create_T6_4_,
        agentattr0_.Create_User as Create_U7_4_,
        agentattr0_.Enabled as Enabled8_4_,
        agentattr0_.Location as Location9_4_,
        agentattr0_.Update_Tstamp as Update_10_4_,
        agentattr0_.Update_User as Update_11_4_ 
    from
        MyTime.dbo.Agent_Attr agentattr0_ 
    where
        agentattr0_.Agent_GUID=?
2014/08/28 16:21:20.632 [TRACE] <http-bio-8080-exec-3> (BasicBinder.bind:81) - binding parameter [1] as [VARCHAR] - [9E5537A6-96FF-4656-9F75-70B58E44C0E9]

1 个答案:

答案 0 :(得分:0)

根据发现的here规范:

LAZY策略是持久性提供程序运行时的提示,在首次访问数据时应该懒惰地获取数据。允许实施急切地获取已指定LAZY策略提示的数据。