将Hibernate迁移到没有注释的JPA [英] Migrating Hibernate to JPA without annotations

查看:129
本文介绍了将Hibernate迁移到没有注释的JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的非Java EE,基于JSF的web应用程序项目。我们的系统是分层的(在源代码意义上):有一个数据模型包,就是DAO包。我们仅在DAO包中使用Hibernate的XML配置映射。我们真的不想用注释来混淆数据模型,但是并没有特别地嵌入到Hibernate中(除了映射非常复杂)。



我是强烈考虑向Java EE迈进并将我们的DAO对象构建为EJB。但是,由于我们不愿放弃Hibernate的XML,这导致我有几个问题:


  • 是否可以在没有JPA的情况下使用Hibernate和JPA注释模型?

  • 如果不是,我的EJB是否可以与Hibernate进行事务处理?我认为这被称为JTA支持,但不确定。我喜欢免费获得交易的想法;现在我们有一个自定义编码的阶段侦听器,我想删除它来处理Hibernate事务。
  • 有没有一种方法可以从Hibernate的XML映射配置迁移到某种JPA XML映射?我没有办法做到这一点,但显然减少我们与Hibernate的耦合会更好。
  • 谢谢! / p>

    解决方案

    可以在模型上使用没有JPA注解的Hibernate和JPA吗?


    是的,这很有可能。您可以在JPA标准映射文件 - orm.xml 中指定对象模型类的所有相关属性。您也可以拥有自己的映射文件,但您需要在 persistence.xml 中指定它们。


    如果不是,我的EJB是否可以与Hibernate进行事务处理?我认为这被称为JTA支持,但不确定。

    如果您使用的是EJB,您会发现容器管理事务是相当有用。您需要做的只是使用所需的 @TransactionManagement @TransactionAttribute 注释,并且容器将自动为您管理事务,包括在方法末尾提交事务(如果适用),并在发生运行时异常或应用程序异常时回滚事务。



    全部 EntityManager 实例,即注入到这些EJB中的所有持久化上下文会自动与底层的JTA交易相关联。请注意,只有当您允许容器注入 EntityManager 实例时,才是如此。如果您自己管理持久性上下文(通过从 EntityManagerFactory 实例),那么您可能需要调用 EntityManager.joinTransaction() 将持久化上下文中执行的所有工作与底层JTA交易。这是必要的,因为如果您已将持久性上下文配置为使用JTA数据源,则 EntityManager 通常会将其自身与底层JTA事务关联。如果没有JTA事务存在,则不会发生此类关联,因此如果要将更改刷新到数据库,则必须加入活动的JTA转换。


    有没有一种方法可以从Hibernate的XML映射配置迁移到某种JPA XML映射?

    我不知道任何这样的工具,因为我从来不需要迁移项目,但这并不意味着没有任何项目可以实现这一目的。您可能想看看支持JPA的 Hibernate Tools 项目,尽管我不确定它是否允许您从Hibernate XML格式转换为JPA格式。


    I have a large non-Java EE, JSF-based web app project. Our system is layered (in the source code sense): there's a data model package, building on that is the DAO package. We are using Hibernate's XML configuration mapping exclusively in the DAO package. We really don't want to muddle the data model with annotations, but aren't wedded to Hibernate specifically (except that the mapping is quite complex).

    I'm strongly considering making a move towards Java EE and building our DAO objects as EJBs. But as we're unwilling to discard Hibernate's XML, this leads me to several questions:

    • Is it possible to use Hibernate with JPA without JPA annotations on the model?
    • If not, is it possible for my EJBs to behave transactionally with Hibernate anyway? I think this is called JTA support, but not sure. I like the idea of getting transactions "for free"; right now we have a custom-coded phase listener I'd like to remove which handles Hibernate transactions.
    • Is there a way to migrate from Hibernate's XML mapping configuration to some sort of JPA XML mapping? I don't see a way to do this, but obviously it would be better to reduce our coupling to Hibernate.

    Thanks!

    解决方案

    Is it possible to use Hibernate with JPA without JPA annotations on the model?

    Yes, this is quite possible. You can specify all pertinent attributes of your object model classes, in JPA standard mapping file - orm.xml. Your can also have your own mapping files, but you'll need to specify them in the persistence.xml.

    If not, is it possible for my EJBs to behave transactionally with Hibernate anyway? I think this is called JTA support, but not sure.

    If you are using EJBs, you'll find container managed transactions to be quite useful. All you need to do is to annotate your EJBs with the required @TransactionManagement and @TransactionAttribute annotations, and the container will automatically manage the transactions for you, including committing the transaction at the end of the method (if applicable), and rolling back the transaction in the event of a runtime exception or an application exception.

    All EntityManager instances, i.e. all persistence contexts, injected into such EJBs, would automatically be associated with the underlying JTA transaction. Note, that this is true only if you allow the container to inject EntityManager instances. If you manage the persistence contexts yourself (by creating them from EntityManagerFactory instances), then you might have to invoke EntityManager.joinTransaction() to associated all the work performed in the persistence context with the underlying JTA transaction. This is necessary as the EntityManager typically associates itself with the underlying JTA transaction, if you have configured the persistence context to use JTA datasources. If no JTA transaction exists, then no such association will occur, and therefore you will have to join with an active JTA transation if you want to flush the changes to a database.

    Is there a way to migrate from Hibernate's XML mapping configuration to some sort of JPA XML mapping?

    I'm unaware of any such tool as I've never had to migrate a project, but that does not mean that there are none to serve this purpose. You might want to take a look at the Hibernate Tools project, which does support JPA, although I'm unsure of whether it will allow you to convert from the Hibernate XML format to the JPA format.

    这篇关于将Hibernate迁移到没有注释的JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆