2012-04-06 117 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); 
}