-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Multiple Persistence Units in JavaSE environment
PostPosted: Fri Dec 12, 2008 8:18 am 
Newbie

Joined: Fri Dec 12, 2008 7:31 am
Posts: 1
Location: Vienna
Hi,

is it possible to use multiple persistence units in one Java SE project? What I have accomplished was, to use multiple units, but the <class>-tag within the <persistence-unit>-tag in persistence.xml seems to be ignored, since all annotated entity classes will be managed by both units. Means: in both databases there are tables for all entities? how can I avoid this?

Here is the persistence.xml of a simple sample project:


Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   <!-- persitence unit for core -->   
   <persistence-unit name="Core" transaction-type="RESOURCE_LOCAL">
       <class>org.example.model.Pojo</class>
       <class>org.example.model.admin.User</class>
       <class>org.example.model.misc.Project</class>
       <properties>
           <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
           <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
           <property name="hibernate.connection.username" value="user" />
           <property name="hibernate.connection.password" value="pass" />
           <property name="hibernate.connection.url" value="jdbc:mysql://localhost/jpatestcore" />
           <property name="hibernate.hbm2ddl.auto" value="update" />
       </properties>
   </persistence-unit>

   <!-- persistence unit for cache-->
   <persistence-unit name="Cache" transaction-type="RESOURCE_LOCAL">
      <class>org.example.model.cache.CachedPojo</class>
      <class>org.example.model.cache.CachedFoo</class>
      <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
          <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
          <property name="hibernate.connection.username" value="user" />
          <property name="hibernate.connection.password" value="pass" />
          <property name="hibernate.connection.url" value="jdbc:mysql://localhost/jpatestcache" />
          <property name="hibernate.hbm2ddl.auto" value="create" />
      </properties>
   </persistence-unit>
</persistence>




Thanks for your help.

cheers, markus


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 10:27 am 
Newbie

Joined: Tue Sep 09, 2008 11:35 am
Posts: 10
Try to instantiate your entity managers like below:
Code:
  protected static EntityManager coreEntityManager;
   Map<String, String> configOverrides = new HashMap<String, String>();
    configOverrides.put( "hibernate.show_sql", Boolean.toString( true ) );
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "CORE", configOverrides );
    coreEntityManager= entityManagerFactory.createEntityManager();
    coreEntityManager.setFlushMode( FlushModeType.AUTO );


and

Code:
  protected static EntityManager cacheEntityManager;
    Map<String, String> configOverrides = new HashMap<String, String>();
    configOverrides.put( "hibernate.show_sql", Boolean.toString( true ) );
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "CACHE", configOverrides );
    cacheEntityManager= entityManagerFactory.createEntityManager();
    cacheEntityManager.setFlushMode( FlushModeType.AUTO );


Maybe it works like this.


Top
 Profile  
 
 Post subject: Re: Multiple Persistence Units in JavaSE environment
PostPosted: Sun Jan 18, 2009 4:52 am 
Newbie

Joined: Sun Jan 18, 2009 4:44 am
Posts: 1
Hello,
I am not a JPA nor Spring expert, but I think you are missing the following line after your <class> definitions:

<exclude-unlisted-classes>true</exclude-unlisted-classes>

That way you will limit each persistance unit to update the database just with the classes that you have defined within the <class> tags.

I hope that helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.