13

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

  • 雄猫 7.0.16
  • 甲骨文 11g
  • Oracle Java 内部版本 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 Vehicle 包含以下注释:

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不包含任何有关车辆的信息。我的理解是注释就足够了。那是对的吗?

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

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

4

6 回答 6

25

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

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

<class>com.foo.demos.car.model.Vehicle</class>
于 2013-10-28T10:55:52.580 回答
2

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

我需要persistence.xml 中的<class> 元素吗?

于 2013-10-28T10:57:38.347 回答
2

<property name="packagesToScan" value="" />您可以通过在 persistence.xml中指定您的实体位置

于 2018-03-09T05:39:22.700 回答
1

我的问题是我创建了一个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教程中的初始化时要小心。

于 2019-12-18T18:02:52.877 回答
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;
}
于 2019-12-08T20:07:35.090 回答
0

在 yml 文件中添加packages-to-scan条目。

jpa:
  default:
    packages-to-scan:
      - 'com.foo.demos.car.model'
      - 'your.entity.package.name'
于 2021-10-24T07:20:32.000 回答