Home » Java » java – JPA persistence.xml – add DAO from referenced library-Exceptionshub

java – JPA persistence.xml – add DAO from referenced library-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

My question is quite simple.
Is it possible to add a DAO from a referenced library?
Fx. if you have multiple micro-services running sharing a some DAOs.

<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <non-jta-data-source>DefaultDS</non-jta-data-source>

    <class>someproject.models.awesomeDAO</class>
    <class>referencedLibrary.models.sharedDAO</class>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    </properties>
</persistence-unit>
How to&Answers:

I assume by DAOs you actually mean Entities. You can add any entity class explictly with the <class> tag in persistence.xml as long as they are in the classpath.

You can even reference a jar file to be searched for annotated classes so you don’t need to declare them explicitly with the tag <jar-file>