views:

493

answers:

1

Hi,

I have one jar library A (or project in eclipse), which has it's own persistence unit (META-INF/persistence.xml) and some entity classes, and another project (B) using this one. In project B there is also persistence unit and entity classes.

In project B I need to use both entity classes from project A and B. But if I set "A" as persistence unit name, EntityManager cannot create named query if this query is in entity from project B. If I set "B" as persistence unit name, it cannot create named queries from entities from project A. Error message is:

NamedQuery of name: MyEntityName.myQueryName not found.

Can persistence units somehow include other persistence units? Or is there any other way to solve this problem?

A: 

You can list your classes needed by A in one persistence unit, and classes needed you B in an other:

<persistence ...>
    <persistence-unit name="projectA" ...>
        ....
        <class>a.Class1</class>
        <class>a.Class2</class>
        <class>a.Class3</class>
    </persistence-unit>

    <persistence-unit name="projectB" ...>
        ...
        <class>a.Class1</class>
        <class>a.Class2</class>
        <class>a.Class3</class>
        <class>b.Class1</class>
        <class>b.Class2</class>
        <class>b.Class3</class>
    </persistence-unit>
</persistence>

Alternatively, you can use the <jar-file> element, quoting from the JPA spec (6.2.1.6): "If specified, these JAR files will be searched for managed persistence classes, and any mapping metadata annotations found on them will be processed, or they will be mapped using the mapping annotation defaults defined by this specification. Such JAR files are specified relative to the root of the persistence unit (e.g., utils/myUtils.jar)."

<persistence ...>
    <persistence-unit name="projectA" ...>
        ...
        <jar-file>relative/path/to/your/library.jar</jar-file>
    </persistence-unit>
</persistence>
candiru
Yes, I know that. But now i use almost empty persistence.xml, so EclipseLink can scan classes and add the ones which have the annotation. I would like to keep this if possible.
amorfis
I see. Check my edited anwser, I hope this will help.
candiru