简体   繁体   中英

How do I use the `properties` attribute of @PersistenceContext annotation?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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