JPA:java.lang.IllegalArgumentException:不是实体

时间:2013-10-28 10:52:16

标签: java oracle hibernate jpa

我编写了一个使用POJO,servlet和JSP的简单Java。我正在使用:

  • Tomcat 7.0.16
  • Oracle 11g
  • Oracle Java build 1.6.0_26-b03

我使用常春藤来解决我的依赖关系:

    <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)

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

<?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>

我显然忽视了一些事情。那是什么?我错过了什么?

5 个答案:

答案 0 :(得分:17)

  

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

不,不是。实体必须在persistence.xml文件中的persistence-unit元素下列出:

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

答案 1 :(得分:2)

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

Do I need <class> elements in persistence.xml?

答案 2 :(得分:0)

您可以通过persistence.xml

中的<property name="packagesToScan" value="" />指定您的实体位置

答案 3 :(得分:0)

如果您动态构建持久性并添加属性,则可以使用'org.reflections:reflections:0.9.11' 声明实体类

new HibernatePersistenceProvider()
        .createContainerEntityManagerFactory(
            new javax.persistence.spi.PersistenceUnitInfoImpl(persistenceUnitName),
            ImmutableMap.<String, Object>builder()
                .put(AvailableSettings.LOADED_CLASSES, getEntities())
                .build());

...


private List<Class<?>> getEntities() {

  Set<Class<?>> entitiesInPackage = new Reflections("rf.dom.model")
    .getTypesAnnotatedWith(Entity.class); // Annotated @Entity

  List entities = new ArrayList<>(entitiesInPackage);

  return entities;
}

答案 4 :(得分:0)

我的问题是我创建了这样的SessionFactory

        Configuration configuration = new Configuration();
        configuration.configure();

        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

而不是这样:

        Configuration configuration = new Configuration();
        configuration.configure();
        sessionFactory = configuration.buildSessionFactory();

当我用前者替换前者时,它起作用了!因此,在使用教程中的SessionFactory进行初始化时要小心。