JPA exception: Object: … is not a known entity type

后端 未结 11 534

I\'m new to JPA and I\'m having problems with the autogeneration of primary key values.

I have the following entity:

package jpatest.entities;

impor         


        
相关标签:
11条回答
  • 2020-12-10 03:17

    I use this syntax rather than type AUTO

    @javax.persistence.Id
    @javax.persistence.GeneratedValue(strategy = GenerationType.IDENTITY)
    

    Then, I use the simple type "long" for ID's with a lowercase l :

    private long taskID;
    

    This may be unrelated, but I also specify a different table name for my entities:

    @javax.persistence.Entity(name = "Tasks")
    public class Task implements Serializable
    
    0 讨论(0)
  • 2020-12-10 03:17

    I also have to add one other item to my persistence.xml when changing class/table defs so that the EM knows to build/update tables:

    <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction=&apos;refresh&apos;)"/>
    

    If I want a fresh start, I instead use:

    <!--<property name="openjpa.jdbc.SynchronizeMappings"
                    value="buildSchema(SchemaAction='dropDB,add')"/>
                    -->
    

    I noticed that in your persistence.xml schema management is only set to "create tables" as opposed to drop/create, or update

    0 讨论(0)
  • 2020-12-10 03:19

    As Charles pointed out in his answer, the problem is not the id generation, but the persistence layer not finding the entity.

    As you, I am also new to JPA. I have tried to write a "Hello World" JPA application using org.eclipse.persistence.jpa.PersistenceProvider when I got this error. The mentioned workaround also worked for me. Moreover, through trial-error I also found that to declare your entities, you must always anotate @entity in each entity and:

    • if you set exclude-unlisted-classes to true, you also have to list the entities within class elements in your persistence.xml
    • if you set exclude-unlisted-classes to false the persistence layer can find the entities regardles of the class element in your persistence.xml.
    0 讨论(0)
  • TopLink used to require you to explicitly set GenerationType.IDENTITY for MySQL, so change this and drop the database. Then try running your sample again. Further you might also want to explcitly set the database platform:

     <property name="toplink.platform.class.name" 
                        value="oracle.toplink.platform.database.MySQL4Platform"/>
    

    Also I vaguely remember that you have to run Toplink using its Java agent in order to make it function properly with a resource local entitymanager.

    I did however successfully run your example using EclipseLink (which you should use since Toplink is outdated). Only cavat was that I did not have MySQL server handy, so I ran it using H2. I used the following Maven pom.xml to resolve the 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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.randompage</groupId>
        <artifactId>sandbox</artifactId>
        <packaging>jar</packaging>
        <version>1.0</version>
        <name>sandbox</name>
        <repositories>
            <repository>
                <id>EclipseLink Repo</id>
                <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
            </repository>
        </repositories>
        <dependencies>
            <dependency>
                <groupId>javax.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>eclipselink</artifactId>
                <version>2.0.0</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>1.2.130</version>
            </dependency>
        </dependencies>
    </project>
    

    and this persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
        <persistence-unit name="JPATestPU" transaction-type="RESOURCE_LOCAL">
            <provider>
                org.eclipse.persistence.jpa.PersistenceProvider
            </provider>
            <class>org.randompage.MyEntity</class>
            <properties>
                <property name="javax.persistence.jdbc.user" value="johndoe"/>
                <property name="javax.persistence.jdbc.password" value="secret"/>
                <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
                <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/.h2/testdb;FILE_LOCK=NO"/>
                <property name="eclipselink.ddl-generation" value="create-tables"/>
                <property name="eclipselink.logging.level" value="INFO"/>
            </properties>
        </persistence-unit>
    </persistence>
    

    With these settings your code ran as expected.

    0 讨论(0)
  • 2020-12-10 03:27

    You could try and leave the definition out of the persistnce.xml The Persistence provider should than scan all classes in the classpath for @Entity annotations.

    0 讨论(0)
提交回复
热议问题