Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA on Java SE: Object: entity.Customer@5e80188f is not a known entity type

I am following

https://glassfish.java.net/javaee5/persistence/persistence-example.html

to test JPA in a Java SE environment. In Eclipse, I:

  • created a new JPA (2.1) project;
  • in options->JPA->Persistent class management, I selected "Discover annotated classes automatically" instead of "Annotated classes must be listed in persistence.xml".

I successfully imported the tree Java classes that are in the zip file (Client.java Customer.java Order.java) and modified the persistence.xml file to fit my needs. But I obtain the following errors when trying to excecute main.

[EL Info]: 2013-10-18 17:37:54.749--ServerSession(263489307)--EclipseLink, version: Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5
[EL Info]: connection: 2013-10-18 17:37:55.34--ServerSession(263489307)--file:/home/caterpillar/workspace/JPA_Java_SE/build/classes/_JPA_Java_SE login successful
[EL Warning]: metamodel: 2013-10-18 17:37:55.359--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: entity.Customer@5e80188f is not a known entity type.
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4228)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)
    at client.Client.testInsert(Client.java:82)
    at client.Client.main(Client.java:49)

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="JPA_Java_SE">
        <properties>
            <property name="javax.persistence.logging.level" value="FINE"/>
            <property name="javax.persistence.logging.thread" value="false"/>
            <property name="javax.persistence.logging.session" value="false"/>
            <property name="javax.persistence.logging.timestamp" value="false"/>
            <property name="javax.persistence.logging.exceptions" value="false"/>

            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="password"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

Project dir tree:

$ tree
.
├── build
│   └── classes
│       ├── client
│       │   └── Client.class
│       ├── entity
│       │   ├── Customer.class
│       │   └── Order.class
│       └── META-INF
│           └── persistence.xml
├── sql
│   ├── tables_derby.sql
│   └── tables_oracle.sql
└── src
    ├── client
    │   └── Client.java
    ├── entity
    │   ├── Customer.java
    │   └── Order.java
    └── META-INF
        └── persistence.xml

10 directories, 10 files

All clases code is identical to example file avaible at http://glassfish.dev.java.net/javaee5/persistence/JPASE.zip

like image 819
Germano Massullo Avatar asked Oct 18 '13 16:10

Germano Massullo


2 Answers

The next line is missing in your persistence.xml:

<exclude-unlisted-classes>false</exclude-unlisted-classes>

This line is placed as in the following example:

<?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="SamplePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
      <property name="javax.persistence.jdbc.password" value="123"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="eclipselink.logging.level" value="ALL"/>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
  </persistence-unit>
</persistence>
like image 70
Paul Vargas Avatar answered Nov 05 '22 15:11

Paul Vargas


There is another option, similar to the one presented by Paul Vargas, also editing the persistence.xml file.

If you want to have more control over which classes are to be managed as entities, use:

<exclude-unlisted-classes>true</exclude-unlisted-classes>

and

<class>YOUR CLASS CANNONICAL NAME</class>

like image 39
Pedro García Medina Avatar answered Nov 05 '22 14:11

Pedro García Medina