【问题标题】:Hibernate enableFetchProfile -> unknowHibernate 启用 Fetch Profile -> 未知
【发布时间】:2012-04-06 19:05:38
【问题描述】:

我想将 FetchProfile 与我的 DAO 一起用于实体示例。

这是我实体上的注释:

@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: Unknow fetch profile [example-profile]

我是否缺少映射或什么?

【问题讨论】:

    标签: java hibernate spring-mvc hibernate-annotations


    【解决方案1】:

    请确保您的具有此类 @FetchProfiles 的实体由 SessionFactory 加载。

    【讨论】:

      【解决方案2】:

      修改

      @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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-13
        • 2011-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多