简体   繁体   English

Spring-Hibernate JPA和JBOSS-将对象保存到第二个数据库

[英]Spring-hibernate jpa and jboss - Saving objects to a second database

Note: Although at first similar, this is not a duplicate of Using Spring, JPA with Hibernate to access multiple databases/datasources configured in Jboss 注意:尽管起初相似,但这与使用Hibernate的JPA使用Spring,以访问Jboss中配置的多个数据库/数据源不是重复的

Dear Stackoverflow, 亲爱的Stackoverflow,

I had a spring-jpa with hibernate application running on jboss-4.2.1.GA and using a single database. 我有一个运行在jboss-4.2.1.GA上并使用单个数据库的休眠应用程序的spring-jpa。

I now have a second spring-hibernate project bundled up in the same ear file with the project described above but it needs to use a second database. 现在,我有一个第二个Spring-hibernate项目与上述项目捆绑在同一个耳文件中,但是它需要使用另一个数据库。 This second hibernate/spring project is set up with the database.properties and hibernate.cfg.xml files. 第二个hibernate / spring项目是使用database.properties和hibernate.cfg.xml文件设置的。

The two databases details are stored on jboss oracle-ds.xml file: 这两个数据库的详细信息存储在jboss oracle-ds.xml文件中:

<datasources>
    <local-tx-datasource>
        <jndi-name>DefaultDS</jndi-name>
         ...
     </local-tx-datasource>
     <local-tx-datasource>
        <jndi-name>SecondDS</jndi-name>
         ...
     </local-tx-datasource>
</datasources>

My question is, in the second project, with objects for the second database and not the first one, how can I call sessionFactory for the second database whose details are stored on the oracle-ds.xml instead of using database.properties files? 我的问题是,在第二个项目中,有第二个数据库的对象而不是第一个数据库的对象,如何为第二个数据库调用sessionFactory,其详细信息存储在oracle-ds.xml中而不是使用database.properties文件? I have seen an example calling 我看过一个例子

@Resource(mappedName = "java:SecondDS")
private DataSource secondDS;
...
java.sql.Connection conn = secondDS.getConnection();

If it is that easy to obtain a connection, that is only useful for prepared statements, how can I in get hold of the sessionFactory? 如果很容易获得连接(仅对准备好的语句有用),我如何掌握sessionFactory? Is there a similar approach? 有没有类似的方法?

All examples I have seen refer to database.properties and not the jboss ds.xml file. 我看过的所有示例都引用了database.properties,而不是jboss ds.xml文件。

Thanks in advance 提前致谢

There is several solution, depending on how you bind the data source to a presistance context, Spring way, JPA way or Hibernate way.... 有几种解决方案,具体取决于您如何将数据源绑定到持久性上下文,Spring方式,JPA方式或Hibernate方式。

Spring 弹簧

To link spring/hibernate application to JNDI data source, you will need to use JndiObjectFactoryBean 要将spring / hibernate应用程序链接到JNDI数据源,您将需要使用JndiObjectFactoryBean

<bean id="serverDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/blah"/>
  <property name="proxyInterface" value="javax.sql.DataSource"></property>
</bean>

This way you have a spring bean representing the JNDI data source. 这样,您就有了一个表示JNDI数据源的spring bean。 You will need to create 2 of them (one for each of you data source). 您将需要创建2个(每个数据源一个)。 They you need to inject the data source in your spring defined SessionFactory. 您需要在弹簧定义的SessionFactory中注入数据源。 The same can be use if you use Spring managed JPA entity manager. 如果使用Spring托管的JPA实体管理器,则可以使用相同的方法。

JPA JPA

If you are using JPA (session factory is hibernate not jpa...) you can also defined a jndi data source name in the corresponding persistance.xml file. 如果使用的是JPA(会话工厂是休眠而不是jpa ...),则还可以在相应的persistance.xml文件中定义jndi数据源名称。

<persistence-unit name="sample">
  <jta-data-source>java:/DefaultDS</jta-data-source>
  ...
</persistence-unit>

You need to use unitName parameter when injecting the entityManager: 注入entityManager时需要使用unitName参数:

    @PersistenceContext(unitName="sample")  

Hibernate 冬眠

For hibernate.cfg.xml file you can specify the JNDI data source with this property 对于hibernate.cfg.xml文件,可以使用此属性指定JNDI数据源。

<property name="connection.datasource">java:/comp/env/jdbc/MyDB</property>

The database.property should be removed to be sure the jndi data source is used. 应该删除database.property以确保使用了jndi数据源。

These are only example, the final result will depend on how you made your plumbing. 这些仅是示例,最终结果将取决于您制作管道的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM