2012-04-06 116 views
0

我想使用FetchProfile與我的DAO爲實體示例。休眠enableFetchProfile - >未知

這是對我的實體註釋:

@FetchProfiles({ @FetchProfile(name = "example-profile", fetchOverrides = { 
    @FetchProfile.FetchOverride(entity = Example.class, association = "association1", mode = FetchMode.JOIN), 
    @FetchProfile.FetchOverride(entity = Example.class, association = "association2", mode = FetchMode.JOIN) }) 
}) 

這是我的DAO:

@Autowired 
private SessionFactory sessionFactory; 

public Example getExample(Long id) { 
    sessionFactory.getCurrentSession().enableFetchProfile("example-profile"); 
    return (Example) sessionFactory.getCurrentSession().get(Example.class, id); 
} 

當我打電話getExample,我得到這個異常:

org.hibernate作爲.UnknownProfileException:未知獲取配置文件[example-profile]

我是否缺少映射或什麼?

回答

0

請確保您的實體具有@FetchProfilesSessionFactory加載。

0

修訂

@Autowired 
private SessionFactory sessionFactory; 

public Example getExample(Long id) { 

    Session session = sessionFactory.getCurrentSession(); 
    session.enableFetchProfile("example-profile"); 

    sessionFactory.getCurrentSession().enableFetchProfile("example-profile"); 
    return (Example) sessionFactory.getCurrentSession().get(Example.class, id); 
}