English 中文(简体)
EclipseLink, EntityManager with two persistence units needed
原标题:

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?

问题回答

EclipseLink 2.3 introduced Composite Persistence Units, which allows you to create a persistence unit that essentially acts only as a container for two or more actual persistence units. You are then able to use this single composite persistence unit in your application as if you had only one persistence unit. This should meet your goals of keeping your persistence.xml files clean for easy synchronization of your model to database. Pretty cool stuff.

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>




相关问题
query must begin with SELECT or FROM: delete [delete

I am using JPA in addition to spring(3.0.0.M4). While deleting multiple records using query.executeUpdate() i am getting the following exception. org.springframework.web.util.NestedServletException: ...

Last update timestamp with JPA

I m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that s supposed to reflect whenever that entity was last updated. What are the strategies for making ...

@IdClass with non primative @Id

I m trying to add a composite primary key to a class and having a bit of trouble. Here are the classes. class User { private long id; ... } class Token { private User user; private ...

Database not dropped in between unit test

Hello good people i came accross a weird behaviour in my test.I m using JPA hibernate annotation with spring. let say i have an Class MyObject and it s property email is marqued @Column(name="EMAIL", ...

Toplink trying to persist null object

I have an object "Instance" with another object "Course" inside. When trying to persist a new Instance object, I get the following error if Course is null: java.lang.IllegalStateException: During ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签