0

I have a maven project, a standalone JPA tool.

This main project depends on another maven project containing all the entity classes.

However, JPA doesn't seem to find my entities automatically. My Eclipse project > properties > jpa > auto discover, is enabled, too.

My persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="myapp-ds" transaction-type="RESOURCE_LOCAL">        
        <description>Vecchio</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myapp" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="xxx" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />            
            <property name="hibernate.archive.autodetection" value="class"/>
        </properties>

    </persistence-unit>

</persistence>

It is located in target/classes/META-INF.

The error, when using the entity is:

Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: model.legacy.dto.myentity
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1184)
    at it.test.Main.main(Main.java:23)
Fabio B.
  • 9,138
  • 25
  • 105
  • 177

2 Answers2

1

If you use a standalone project, only classes included in main jar will be automatically scanned by the persistence unit. Other jars will not be included in an archive like what happens for a war, but will be searched in the classpath as required => the entity classes including in the jar created by the other project are not scanned by default.

You can use <jar-file>full jar location</jar-file>, but it will be highly non portable unless you manage to package all jars in same directory, but even then, it will be only a convention for installation. That is the reason why many documents (like Hibernate Entity Manager guide) recommends to explicitely list all entity classes outside of current jar in a JSE environment.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

You try to read here. Otherwise, If auto detect doesn't work, you can insert manually the entities in persistence.xml:

    <persistence-unit ...>
        <class>...</class>
  </persistence>
Community
  • 1
  • 1
Michel Foucault
  • 1,724
  • 3
  • 25
  • 48