【问题标题】:Can hbm.xml file and hibernate annotations work together?hbm.xml 文件和hibernate注解可以一起工作吗?
【发布时间】:2012-09-18 14:57:13
【问题描述】:

当我通过注释声明命名查询时,Hibernate Session 无法找到它,但是当我放入 hbm.xml 时它工作正常。

这是一个遗留系统,其中所有休眠实体都通过 hbm.xml 定义,我正在慢慢尝试将其迁移到注释。

Is it possible to distribute hibernate metadata via both hbm.xml and annotations?

例外是

org.hibernate.MappingException: Named query not known: queryName

实体类是

@Entity
@NamedQueries({

    @NamedQuery(name="queryName", queryString="from Product pro")
})

public class Product extends HibernateEntity

获取会话

        SessionFactory hibernateSessionFactory = (SessionFactory) hibernateSessionFactories.get(targetDB);

        if (hibernateSessionFactory != null) 
        {
            session = hibernateSessionFactory.openSession(new HibernateEntityDifferenceInterceptor());

            session.setFlushMode(FlushMode.COMMIT);
        }

【问题讨论】:

  • 看看如何配置 SessionFactory 很有用。
  • 使用 SessionFactory 代码更新
  • 仍然没有创建SessionFactory的代码。
  • 有了新发现,我意识到将 Annotation 和 hbm.xml 混合用于同一实体类的问题。我猜 Hibernate 不允许这样做。

标签: hibernate


【解决方案1】:

有了 Spring 支持,它是 definitely possible,所以如果你不使用 Spring,你可以查看它里面的源代码并弄清楚它是如何在那里实现的。

【讨论】:

  • 是否可以仅使用 Spring 为 Annotation 配置 SessionFactory 并保持 hbm.xml 配置不变?
  • Nupe,这意味着您将创建多个SessionFactories。但是您需要一个既能看到某些实体的 XML 文件,又能看到其他实体的注释的工具。
  • 由于我的应用程序是一个遗留系统,我很难移动逻辑来创建会话工厂。这个问题还有其他工作吗?
  • 你看一下它在Spring中是如何实现的吗?此外,我个人认为 XML 是一种比注释更好的 Hibernate 映射方式,但这是一个圣战话题。
猜你喜欢
  • 2012-01-08
  • 1970-01-01
  • 1970-01-01
  • 2013-07-09
  • 2016-01-29
  • 2017-02-07
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
相关资源
最近更新 更多