JPA: java.lang.IllegalArgumentException: Not a entity

2022-09-04 06:53:27

我写了一个简单的Java,它使用POJO,servlets,JSP。我正在使用:

  • 雄猫 7.0.16
  • 甲骨文 11g
  • Oracle Java build 1.6.0_26-b03

我使用 ivy 来解决我的依赖关系:

    <dependency org="oracle" name="ojdbc6" rev="11g" conf="default -> default" />

    <dependency org="org.hibernate" name="hibernate-commons-annotations" rev="3.2.0.Final" conf="compile -> runtime" />
    <dependency org="org.hibernate" name="hibernate-annotations" rev="3.5.6-Final" conf="default -> runtime" />
    <dependency org="org.hibernate" name="hibernate-core" rev="3.6.5.Final" conf="default -> runtime" />
    <dependency org="org.hibernate" name="hibernate-validator" rev="4.0.2.GA" conf="runtime -> runtime" />
    <dependency org="org.hibernate.javax.persistence" name="hibernate-jpa-2.0-api" rev="1.0.1.Final" conf="compile -> default" />
    <dependency org="org.hibernate" name="hibernate-entitymanager" rev="3.6.5.Final" conf="compile -> default" />

我的POJO车辆包含以下注释:

import java.io.Serializable;

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

@Entity
public class Vehicle implements Serializable{


    /**
     * 
     */
    private static final long serialVersionUID = 8560417193012227968L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long identifier;

    private String serialNumber;
    private String model;
    private String manufacturer;
    private int year;
    private String condition;

    @Version
    private long version;

    public Vehicle(){
    }

当我运行我的代码时,我得到以下内容:

2013-10-28 11:40:50 ERROR SqlVehicleService:42 - java.lang.IllegalArgumentException: Not an entity: class com.foo.demos.car.model.Vehicle
java.lang.IllegalArgumentException: Not an entity: class com.foo.demos.car.model.Vehicle
    at org.hibernate.ejb.metamodel.MetamodelImpl.entity(MetamodelImpl.java:160)
    at org.hibernate.ejb.criteria.QueryStructure.from(QueryStructure.java:138)
    at org.hibernate.ejb.criteria.CriteriaQueryImpl.from(CriteriaQueryImpl.java:179)
    at com.foo.demos.car.impl.SqlVehicleService.getVehicles(SqlVehicleService.java:37)
    at org.apache.jsp.secured.vehicles_jsp._jspService(vehicles_jsp.java:194)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:572)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:301)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:162)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

My 不包含任何有关车辆的信息。我的理解是,注释就足够了。这是对的吗?persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd"
 version="1.0">

 <persistence-unit name="cardemo">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <!-- the JNDI data source -->
  <non-jta-data-source>java:comp/env/jdbc/cardemo</non-jta-data-source>
  <properties>
 <property name="javax.persistence.validation.mode" value="NONE" />
   <!-- if this is true, hibernate will print (to stdout) the SQL it executes, 
    so you can check it to ensure it"s not doing anything crazy -->
   <property name="hibernate.show_sql" value="true" />
   <property name="hibernate.format_sql" value="true" />
   <!-- since most database servers have slightly different versions of the 
    SQL, Hibernate needs you to choose a dialect so it knows the subtleties of 
    talking to that server -->
   <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
   <!-- this tell Hibernate to update the DDL when it starts, very useful 
    for development, dangerous in production -->
   <property name="hibernate.hbm2ddl.auto" value="update" />
  </properties>
 </persistence-unit>
</persistence>

我显然忽略了一些东西。什么?我错过了什么?


答案 1

我的坚持.xml不包含任何有关车辆的信息。我的理解是,注释就足够了。这是对的吗?

不,它不是。实体必须列在持久性.xml文件中持久性单元元素下:

<class>com.foo.demos.car.model.Vehicle</class>

答案 2

您需要提示在哪里可以找到持久性实体。我通常通过Spring进行配置,因此没有所需的确切信息,但是在这里进行进一步的讨论。

持久性中是否需要<类>元素.xml?


推荐