简体   繁体   English

Hibernate映射实体不存在于persistence.xml中

[英]Hibernate mapping entities not present in persistence.xml

I am using persistence.xml to specify my mapping setting in Hibernate. 我正在使用persistence.xml在Hibernate中指定我的映射设置。 That is I am using the EntityManager class to get my transactions. 那就是我正在使用EntityManager类来获取我的交易。

Now I have the following classes listed in my persistence.xml 现在,我的persistence.xml列出了以下类

<class>test.entity.Course</class>
<class>test.entity.Semester</class>
<class>test.entity.Subject</class>

Also in my Eclipse project I have two more classes (Student.java and User.java) that are marked with the @Entity annotation but are not listed in the persistence.xml file. 同样在我的Eclipse项目中,我还有另外两个类(Student.java和User.java),它们用@Entity批注标记,但未在persistence.xml文件中列出。

But when I run my project Hibernate actually maps those two classes as well. 但是当我运行我的项目时,Hibernate实际上也映射了这两个类。 What I mean is that it creates database tables for those two classes as well (I have hbm2ddl set to auto ). 我的意思是,它也为这两个类创建数据库表(我将hbm2ddl设置为auto )。

Why is it doing this ? 为什么这样做呢? Isn't it suppose to map only the files listed in persistence.xml ? 它不是只映射persistence.xml列出的文件吗?

By default, JPA standard states, that in application server environment, all classes annotated with @Entity are considered, whether they are listed in persistence.xml or not. 默认情况下,JPA标准指出,在应用程序服务器环境中,无论是否在persistence.xml中列出,都将考虑所有带@Entity注释的类。 If you really want to include only those 3 entities mentioned in persistence.xml, you need to add following setting into your persistence.xml file into the persistence-unit element: 如果您确实只想包括persistence.xml中提到的那三个实体,则需要在persistence.xml文件中的persistence-unit元素中添加以下设置:

<exclude-unlisted-classes>true</exclude-unlisted-classes>

可以使用persistence.xml或JPA批注配置Hibernate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM