JPA未知实体。 Hibernate impl

时间:2017-06-08 14:42:35

标签: java hibernate jpa

我的测试班:

package com.example.permissions.impl;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table
@Data
@Entity
public class TestEnt {
    @Id
    @GeneratedValue
    private long id;

    private String testStr;
}

我的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="persistence" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <shared-cache-mode>ALL</shared-cache-mode>
        <validation-mode>AUTO</validation-mode>

        <properties>
            <property name="hibernate.archive.autodetection" value="class, hbm"/>

            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/data" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="qwerty" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />

            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />

            <!-- Configuring Connection Pool -->
            <property name="hibernate.c3p0.min_size" value="5" />
            <property name="hibernate.c3p0.max_size" value="20" />
            <property name="hibernate.c3p0.timeout" value="500" />
            <property name="hibernate.c3p0.max_statements" value="50" />
            <property name="hibernate.c3p0.idle_test_period" value="2000" />
        </properties>
    </persistence-unit>
</persistence>

我有多模块项目。主模块使用hibernate和其他子模块加载数据管理器。

我使用hibernate-core:5.2.10.Finalhibernate-c3p0:5.2.10.Final与mysql连接器v6.0.6

我无法使用EntityManager保留TestEnt

    EntityManager em = PersistenceManager.getEntityManager();
    em.getTransaction().begin();
    em.persist(ent);
    em.getTransaction().commit();

我得到了这个例外: java.lang.IllegalArgumentException: Unknown entity: permissions.impl.TestEnt

我尝试在persistence.xml中添加带com.example.permissions.impl.TestEnt的类节点 - 同样的例外。

PersisteceManager,如果需要:

package com.example.data.api;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class PersistenceManager {

    private static EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("persistence");

    public static EntityManager getEntityManager() {
        return emFactory.createEntityManager();
    }

    public static void close() {
        emFactory.close();
    }
}
UPD:好的。根据这个:docs - 我需要持久性存档。(jar和模型的persistence.xml)。 试试吧!

UPD2:不。相同的异常(我刚刚复制了persistence.xml并添加了class节点。

0 个答案:

没有答案