持久化JPA模型时出错

时间:2016-07-16 03:55:22

标签: hibernate jpa persistence persistence.xml

我在保存数据时遇到错误,我不知道那里有什么问题!!

以下是Persistence.xml的内容

    <persistence 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"
             version="2.0">

    <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">

        <properties>

            <!--PostgreSQL-->
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.connection.username" value="postgres"/>
            <property name="hibernate.connection.password" value="postgres"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.id.new_generator_mappings" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.ejb.metamodel.generation" value="disabled"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultComponentSafeNamingStrategy"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>

    </persistence-unit>

</persistence>

这是Rest webservice调用:

@GET
@Path("/")
public Response test() {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
    EntityManager em = emf.createEntityManager();

    Member member = new Member("doux");
    em.persist(member);


    em.close();
    emf.close();

    return Response.status(Response.Status.OK).build();
}

这是我得到的错误!! :

message Unknown entity: ca.products.jpa_models.Member

    description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête.

    exception

    java.lang.IllegalArgumentException: Unknown entity: ca.products.jpa_models.Member
        org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1149)
        ca.products.services.RestServices.test(RestServices.java:41)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        java.lang.reflect.Method.invoke(Method.java:497)
        com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
        com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
        com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
        com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
        com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
        com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
        com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

我应该在persistence.xml中声明该类的链接,因为它不属于同一个包!

<class>ca.products.jpa_models.Member</class>

以下是来源:

  

class:class元素指定一个完全限定的类名   你会映射。默认情况下,所有正确注释的类和所有类   在存档中找到的hbm.xml文件将添加到持久性中   单位配置。您可以通过类添加一些外部实体   虽然元素。作为规范的扩展,您可以添加   元素中的包名称(例如   org.hibernate.eg)。小心,包将包括   在包级别定义的元数据(即在package-info.java中),   它不会包含给定包的所有类。