1

I have a JPA-Project, which I'm trying to update to JPA 2.2.0 and EclipseLink 5.7.1 since I ran into bug 429992 of EclipseLink. With the new versions in place, I'm not able to execute my application anymore – EclipseLink throws an exception similar to the following (Short variant from my example below):

[EL Warning]: metamodel: 2018-06-20 22:38:14.1--Thread(Thread[main,5,main])--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units.  Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
[...]

Exception in thread "main" java.lang.IllegalArgumentException: Object: Artifact@17d919b6 is not a known Entity type.
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4324)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:593)
    at Main.main(Main.java:12)

Before the update, everything worked fine (besides the aforementioned bug), and also if I checkout an earlier commit, there are no problems.

I have reproduced this behaviour with the minimal setup attached below.

The project is compiled using Java SE 10, as IDE I'm using Eclipse, but in my project properties, I only have the option to select "Generic 2.1" as JPA-Platform. May this be an problem?

Are you able to reproduce this error?

As far as I can see, the Entity-class is listed in the persistence.xml and also annotated with @Entity, but not loaded by EclipseLink. Cleaning the project or even creating a new one does not solve the problem.

Do you have an idea, what my mistake might be? Am I missing any fundamental point about the usage of JPA 2.2/EclipseLink 2.7.1?

Thank you for any hints or comments!


main method in the main class:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Example");
EntityManager entityManager = entityManagerFactory.createEntityManager();

entityManager.getTransaction().begin();
Artifact artifact = new Artifact();
entityManager.persist(artifact);
entityManager.getTransaction().commit();

entityManager.close();
entityManagerFactory.close();

Entity Artifact:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;

@Entity
@NamedQuery(name = "Artifact.findAll", query = "SELECT a FROM Artifact a")
public class Artifact {
    private int id;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

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="Example">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>Artifact</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:./inventory;create=true"/>
            <property name="javax.persistence.jdbc.user" value="APP"/>
            <property name="javax.persistence.jdbc.password" value="APP"/>
            <property name="eclipselink.logging.level" value="FINEST"/>            
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
        </properties>
    </persistence-unit>
</persistence>

pom.xml for Maven dependencies:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>JPATest</groupId>
    <artifactId>JPATest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <release>10</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.7.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
        </dependency>
    </dependencies>
</project>
MWiesner
  • 7,913
  • 11
  • 31
  • 66
Janik
  • 13
  • 5
  • Does your log (Put EclipseLink logging to finest if you haven't already) show it is loading the Artifact entity class at all? It should say something about it directly since you mentioned it in the persistence unit. If it doesn't, make sure your package name is correct in the persistence.xml. If it does mention it was loaded, check that the class loader used to load the EMF/EM matches the one used for the application Artifact instances. The warning you are seeing can be ignored: https://www.eclipse.org/forums/index.php/t/365263/ – Chris Jun 21 '18 at 15:21
  • ` Artifact` did You try long (qualified) class name? – Jacek Cz Jun 22 '18 at 11:49
  • Thank you both for your comments so far! – Janik Jul 05 '18 at 06:47
  • @JacekCz: in my example, it is the qualified name, but even within a package giving the full name it is not working. – Janik Jul 05 '18 at 06:55
  • @Chris: The log is not showing any other entries about the entity at all. That's the point why I'm confused. The package name and the class name are both correct, Eclipse is also able to refactor it. I've included the warning, as it states that "[...] Model classes may not have been found during entity search [...]". – Janik Jul 05 '18 at 06:55
  • The warning you mention is talking about metamodel classes, but if you are not seeing any messages stating entities are getting loaded it is the same problem. You mention if you check out an earlier commit, there are no problems - what exactly are the differences in your project, is it just lambda expressions in this entity? If so, open a bug on the 2.7.1 version. – Chris Jul 05 '18 at 15:19

2 Answers2

4

I had the same problem too. finally, it was solved with an update in pom.xml.

Eclipselink 2.7.1 version has some bugs that fixed at newer versions.

     <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.7.1</version>
    </dependency>

to

 <version>2.7.5</version>

before the update, entities are not loaded and em.getMetamodel() was empty. after the update, all of my entities are loaded successfully

issues and the fixed bugs can be accessed from this link.

afshar
  • 392
  • 1
  • 12
-2

I have pretty much the same environment and experience the same problem. I hardly dare to say but in eclipse a "project/maven/update project ..." helped (so far).

ruu
  • 41
  • 1
  • 6
  • So this is an answer which refers to the Eclipse IDE and it doesn't even say so. It also does not say WHY this would help with the eclipselink issue. E.g. please explain what Eclipse IDE will do in the background for you, and how you could do this on a command line. – Ben Jun 18 '19 at 08:26
  • Hmm, Janik obviously uses eclipse, maven and eclipselink, so did I and I clearly said so. In my case "project/maven/update" solved the problem. Meanwhile (for other reasons) I switched to hibernate as orm provider, so I can't reproduce this setting anymore. I'm sorry that my hint didn't help. And I didn't (want to) check what is going on in "the backgroung". If you haven't done so, consider giving it a try ;) – ruu Jun 18 '19 at 17:48
  • "obviously uses eclipse" -- yes, but if he's using maven, maven will generate the reproducible build. Always use the build tool first and make it work there. – Ben Jun 25 '19 at 09:19