2

I have two entity managers instances defined in my class, one of which overrides the properties attribute of PersistenceContext, and one which does not:

@PersistenceContext
protected EntityManager em;

@PersistenceContext(properties={@PersistenceProperty(name="hibernate.default_schema", value="archive")})
protected EntityManager emArchive;

I've done this in order to define one entity manager attached to my default schema and the other to be attached to my archive schema.

Here is my persistance.xml:

<persistence-unit name="primary" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/PCMain_DS</jta-data-source> 

    <properties>
        <!-- Properties for Hibernate -->
        <!--  property name="hibernate.hbm2ddl.auto" value="update" /-->
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit> 

Unfortunately, my program doesn't seem to be working, as when I save an entity with the archive entity manager (emArchive), it gets saved to the default public schema. It seems that the property hibernate.default_schema doesn't get set - but why? How I do switch schema?

My tools/environment:

  • JBoss 7.1
  • EJB
  • JPA
  • Hibernate

All help appreciated, thanks.

Perception
  • 79,279
  • 19
  • 185
  • 195
Krab
  • 21
  • 1
  • 2
  • Never tried this myself, but have you tried two different persistence units pointing to the same data source with a different schema ? It's a little more markup but if it's urgent... :-) – Jan Goyvaerts Feb 08 '13 at 16:03
  • Yes, but this not work for me... If I tried two different persistence units pointing, my application not deployed with message: Caused by: java.lang.IllegalArgumentException: JBAS011470: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment "highway-ear.ear". Either change the application to have only one persistence unit definition or specify the unitName for each reference to a persistence unit. But I sets unit names: @PersistenceContext(unitName="primary") & @PersistenceContext(unitName="archive") – Krab Feb 08 '13 at 16:10
  • On second thought, it *might* be that what you're trying won't work in JPA. Because an entity class maps to a specific table in the database. You are trying to have two mappings for the same class. You're detaching before merging into the archive persistence unit, right ? Because it would certainly not work when it's still attached. – Jan Goyvaerts Feb 08 '13 at 16:15
  • [This answer](http://stackoverflow.com/a/5066829/1125172) may help. – Linuslabo Mar 07 '17 at 09:39

1 Answers1

0

There can be other ways to do it, but here is one alternative:

  1. Define two elements in your persistence.xml
  2. For each persistence-unit defined, specify the names of the entities targeted for each Persistence Unit using the element

That should take care of isolation of entities persisting in each persistence unit.

Horizon
  • 548
  • 3
  • 7