How to bind an entity to a specific persistence-unit
Asked Answered
P

3

8

In a web application using struts2 ejb hibernate, is it possible to tell the application to find or create an entity for a specific persistence-unit name, which is written in persistence.xml file, in the deployment time?

I have two persistence-unit in persistence.xml, and one datasource (including two "local-tx-datasource") xml file under the jboss node.

To clearify, I mean, I tried this;

@Entity  
@PersistenceContext(unitName="MY JNDI NAME specified in persistence.xml") 
public abstract class Vehicle {

and doesnt work.. Then tried this and etc..

@PersistenceContext(name="MY PERSISTENCE UNIT NAME specified in persistence.xml")

@PersistenceUnit(name="MY PERSISTENCE UNIT NAME specified in persistence.xml")

and also I tried these above with the "UnitName=.." instead of "name=.." but anything is worked for me...

[SOLVED]

<.exclude-unlisted-classes>true<./exclude-unlisted-classes> has solved my problem

Phonemics answered 5/8, 2010 at 14:26 Comment(1)
Could you confirm that you combined the <exclude-unlisted-classes>true</exclude-unlisted-classes> with listing all entity classes (<class>com.mycompany.Foo</class> etc.)? Preferably edit that info into the [SOLVED] part of your question.Landreth
O
9

Update: Based on your comment (this is not what I understood from the original question), I don't think you have any other option than disabling "discovery" and listing explicitly your entities in their respective persistence unit:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
    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_2_0.xsd"
    version="2.0">

  <persistence-unit name="MyPu1" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.mycompany.Foo</class>
    ...
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <!-- H2 in memory -->
      <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
      <property name="javax.persistence.jdbc.username" value="APP"/>
      <property name="javax.persistence.jdbc.password" value="APP"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>

  <persistence-unit name="MyPu2" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.mycompany.Bar</class>
    ...
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <!-- Derby server -->
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.user" value="APP"/>
      <property name="javax.persistence.jdbc.password" value="APP"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Pu2;create=true"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

I'm not aware of any syntax at the entity level allowing to assign it to a persistence unit.


I'm not sure I understood what you're trying to do but if you want to get an Entity Manager for a specific persistence unit injected, you should do:

@Stateless
public class FooBean implements Foo {
    @PersistenceContext(unitName="MyPu1")
    EntityManager em1;

    // ...
}

If this is not what you want, please clarify the question.

Ordain answered 5/8, 2010 at 16:26 Comment(3)
At first, thanks for your reply. As we know, when I add an @Entity anno. to a class, it will search and should create the table in the DB just while in the deployment time. In my application, all entities are being searched in only one persistenceUnit and it is the wrong one. I mean, I want my entities to be searched or created in another DB which is specified in the other persistenceUnit. As a result, I want to specificly say my application that the selected entities must be searched or created for only the selected persistenceUnit object(which includes the right DB settings). Any idea?ThanksPhonemics
BTW, it is worth to tell again that I have two persistence-unit in persistence.xml. I mean, I have two DB connection settings in my application and I want to use one or another depend on the entities I want. I want to be able to tell, "hey you should see this entity as a table in only that DB (because it is already there) which you can find the settings of the DB in that persistence-unit name! So, use this persistence-unit for this entity"Phonemics
<exclude-unlisted-classes>true</exclude-unlisted-classes> is solved my problem, thanks a lot PascalPhonemics
I
0

What you're looking for is probably <exclude-unlisted-classes>true</exclude-unlisted-classes>.

Check the documentation on jboss.org:

In my configuration I had two databases (let's say A and B) and I wanted two separate persistence units, where one contains all entities, but one, while the other persistence unit contains the remaining entity. My persistence.xml looks like this:

<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="A" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="${hibernate.dialect}" />
    </properties>
</persistence-unit>

<persistence-unit  name="B" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.dialect" value="${rud.hibernate.dialect}"/>
    </properties>
    <class>com.sgk.Person</class>
</persistence-unit>

Icken answered 13/12, 2016 at 12:38 Comment(0)
L
-1

@Pascal Thivent

I haven't tried using multiple EntityManager at once, but looking at above mentioned problem this may help if it works.

@PersistenceContext(unitName="MyPu1") EntityManager em1;

@PersistenceContext(unitName="MyPu2") EntityManager em2;

Lorsung answered 5/8, 2010 at 18:24 Comment(3)
Well, that's exactly my suggestion. But it seems the OP is looking for something else.Ordain
Thanks Nayan. However, the problem is not about entitymanager injection indeed. I have added a comment to the first answer above. If you have any idea about the problem I mentioned, I would be great to hear that.Phonemics
@Downvoter OP clarified in comment which was different from original question; what went wrong to downvote unnecessarily.Lorsung

© 2022 - 2024 — McMap. All rights reserved.