Java SE环境中的JPA:在persistence.xml中装入没有防御的实体类 [英] JPA in Java SE environment: Entity classes loaded without defenition in persistence.xml

查看:53
本文介绍了Java SE环境中的JPA:在persistence.xml中装入没有防御的实体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Oracle JPAD认证做准备,所以我在Java SE环境中与实体打交道(至少,这就是我的想法...).我注意到我希望有人能对此有所启发.

I'm prepping for Oracle JPAD certification, so I'm playing around a littlebit with Entities in a Java SE environment (at least, that's what I think...). I noticed something on which I'm hoping someone can shed a light on it.

我正在阅读Pro JPA2.在第2章末尾的某个地方,有一个示例persistence.xml文件,其中定义了一个类.好的,这就是在Java SE环境中使用的方法,因为您实际上并没有将整个容器放在容器中.但这是一件奇怪的事情:我有两个用@Entity注释的Entity类,并且还没有将其中任何一个添加到persistence.xml中.但是仍然在加载!我吓坏了.我现在唯一能想到的就是我不在SE环境中,或者它是一个新的休眠功能...

The book Pro JPA2 is what I'm reading. Somewhere at the end of chapter 2, there's an example persistence.xml file with a class defined in it. Ok, so that's the way to go in a Java SE environment, because you don't really have the whole shebang inside a container. But here's the strange thing: I have two Entity classes annotated with @Entity and haven't added any of these to the persistence.xml. But still the're being loaded! I'm gobsmacked. The only thing I can think of now, is that I'm not in an SE environment or it's a new hibernate feature...

Persistence.xml:

Persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="employees" transaction-type="RESOURCE_LOCAL">

    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:." />
      <property name="javax.persistence.jdbc.user" value="SA" />
      <property name="javax.persistence.jdbc.password" value="" />

      <property name="hibernate.show_sql" value="false" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
      <property name="hibernate.connection.shutdown" value="true" />
      <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    </properties>

  </persistence-unit>
</persistence> 

实体:

@Entity
   @Access(AccessType.FIELD)
   public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private Date startDate;
    private Long salary;

    public Employee() {
    }

    public Employee(String name) {
        this.name = name;
    }

    public Employee(String name, Long salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", salary=" + salary + ", startDate=" + startDate + "]";
    }
}

一个测试:

public class TestConnection {

    private static EntityManagerFactory entityManagerFactory;
    private static EntityManager entityManager;
    private static final String PERSISTENCE_UNIT_NAME = "employees";

    @BeforeClass
    public static void initEntityManager() throws Exception {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        entityManager = entityManagerFactory.createEntityManager();
        TableStructurePrinter.outputTableStructure(entityManager);
    }

    @AfterClass
    public static void closeEntityManager() {
        entityManager.close();
        entityManagerFactory.close();
    }

    @BeforeMethod
    public void setupDB() throws Exception {
        entityManager.getTransaction().begin();
        LoadEmployees.load(entityManager, 4);
    }

    @AfterMethod
    public void cleanDB() throws Exception {
        entityManager.getTransaction().rollback();
    }

    @Test
    public void testCountEmployees() {
        Query q = entityManager.createQuery("select e from Employee e");
        assertEquals(q.getResultList().size(), 4);
    }

}

还有我的TableStructurePrinter的输出(这是来自INFORMATION_SCHEMA.SYSTEM_TABLES和INFORMATION_SCHEMA.SYSTEM_COLUMNS的简单选择语句):

And the output from my TableStructurePrinter (this is a simple select statement from INFORMATION_SCHEMA.SYSTEM_TABLES and INFORMATION_SCHEMA.SYSTEM_COLUMNS):

T.TABLE_TYPE         T.TABLE_NAME           C.ORDINAL_POSITION C.COLUMN_NAME                 C.TYPE_NAME        C.COLUMN_SIZE
===================  ===================   =================== ===================   ===================  ===================
TABLE                EMPLOYEE                                1 ID                                 BIGINT                   64
TABLE                EMPLOYEE                                2 NAME                              VARCHAR                  255
TABLE                EMPLOYEE                                3 SALARY                             BIGINT                   64
TABLE                EMPLOYEE                                4 STARTDATE                       TIMESTAMP                   26

推荐答案

Hibernate至少在EE环境中也可以自动检测实体,但我想对SE应用程序也是如此. AFAIK将扫描包含persistence.xml文件的jar以及其中列出的所有jar或类.

Hibernate can also autodetect entities, at least in an EE environment but I'd say the same is true for an SE application. AFAIK it will scan the jar containing the persistence.xml file as well as any jars or classes listed there.

来自http://docs.oracle.com /cd/E16439_01/doc.1013/e13981/cfgdepds005.htm :

此持久性单元包括哪些持久性托管类?
您可以使用以下一种或多种方法来指定与持久性单元相关联的持久性托管类:

What Persistent Managed Classes Does This Persistence Unit Include?
You can specify the persistent managed classes associated with a persistence unit by using one or more of the following:

  • < mapping-file>元素:指定一个或多个对象关系映射XML文件(orm.xml文件).

  • <mapping-file> element: specifies one or more object-relational mapping XML files (orm.xml files).

< jar-file>元素:指定一个或多个将搜索类的JAR文件.

<jar-file> element: specifies one or more JAR files that will be searched for classes.

< class>元素:指定一个明确的类列表.

<class> element: specifies an explicit list of classes.

包含在持久性单元根目录中的带注释的托管持久性类.

The annotated managed persistence classes contained in the root of the persistence unit.

持久性单元的根是JAR文件或目录,其META-INF目录包含persistence.xml文件.要排除托管的持久性类,请在持久性单元中添加一个< exclude-unlisted-classes>元素.

The root of the persistence unit is the JAR file or directory, whose META-INF directory contains the persistence.xml file. To exclude managed persistence classes, add an <exclude-unlisted-classes> element to the persistence unit.

最后一部分与您的情况有关,即托管类(实体)包含在包含persistence.xml的同一jar中,因此会自动添加.

The last part is what is relevant in your case, i.e. the managed classes (entities) are contained in the same jar that contains the persistence.xml and thus are automatically added.

这篇关于Java SE环境中的JPA:在persistence.xml中装入没有防御的实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆