简体   繁体   中英

Hibernate mapping entities not present in persistence.xml

I am using persistence.xml to specify my mapping setting in Hibernate. That is I am using the EntityManager class to get my transactions.

Now I have the following classes listed in my 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.

But when I run my project Hibernate actually maps those two classes as well. What I mean is that it creates database tables for those two classes as well (I have hbm2ddl set to auto ).

Why is it doing this ? Isn't it suppose to map only the files listed in 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. 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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