views:

27

answers:

1

The Problem:

I have two complementary projects, a base one (Base Project) with shared entities and a specific one (Specific Project) that needs to query the entities from the first project.

They both access the same database, but in different schemes and different users/grants. Nonetheless, both have at least 'SELECT' grant on the shared tables.

I've found three possibilities to integrate them in the Specific Project's persistence.xml:

  1. List every single entity from the Base Project using <class> tags in the Specific Projetc's persistence.xml. Downside: every time a new entity comes up it has to be added to the Specific Project to be visible

  2. Add the Base Project '.jar' file using a <jar-file> tag in the Specific Project's persistence.xml. Downside: it has to be a real path to the jar file, which may change;

  3. (BEST so far) Add the Base Project's entities PACKAGE in the persistence.xml using the <class> tag, as explained in Hibernate entityManager's setup documentation. Downside: have to use package level annotations through a package-info.java file, which I can't find anywhere how to do it.

The Question:

Does anyone knows if is really possible to use a package level annotation to denote an entity package in the persistence.xml?

The Hibernate documentation states that it can be done, but doesn't tell how:

class

The class element specifies a fully qualified class name that you will map. By default all properly annotated classes and all hbm.xml files found inside the archive are added to the persistence unit configuration. You can add some external entity through the class element though. As an extension to the specification, you can add a package name in the element (eg org.hibernate.eg). Caution, the package will include the metadata defined at the package level (ie in package-info.java), it will not include all the classes of a given package.

A: 

As suggested in that bug report, it is working properly. If you want an example of package-info.java, you can take a look at this hibernate forum post.

Note: This functionality is not part of the specification as described in the persistence XML schema.

      <xsd:element name="class" type="xsd:string" 
                   minOccurs="0" maxOccurs="unbounded">
        <xsd:annotation>
          <xsd:documentation>

            Class to scan for annotations.  It should be annotated 
            with either @Entity, @Embeddable or @MappedSuperclass.

          </xsd:documentation>
        </xsd:annotation>
      </xsd:element>
rochb