<persistence xmlns="..." >
<persistence-unit name="pu1">
<provider>com.objectdb.jpa.Provider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="objectdb://localhost/my.odb"/>
...
</properties>
<class>com.liyl.Entity1</class>
...
原文:https://www.objectdb.com/java/jpa/entity/persistence-unit
一个持久化单元指的是包含一组用户自定义的 @Entity @MappedSuperClass @Embeddable 类。在JPA中必须显式地声明持久化单元。
在JPA中,持久化单元需要声明在classpath下的META-INF/persistence.xml文件中,用 <persistence-unit name="pu-name">
元素声明,name用于实例化一个EntityManagerFactory时使用,示例如下:
<?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="my-pu">
<description>My Persistence Unit</description>
<provider>com.objectdb.jpa.Provider</provider>
<mapping-file>META-INF/mappingFile.xml</mapping-file>
<jar-file>packedEntity.jar</jar-file>
<class>sample.MyEntity1</class>
<class>sample.MyEntity2</class>
<properties>
<property name="javax.persistence.jdbc.url"
value="objectdb://localhost/my.odb"/>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="admin"/>
</properties>
</persistence-unit>
</persistence>
JPA要求,对于所有用户自定义的 @Entity类型,都要将其显式地注册到持久化单元中进行托管,称之为托管类。
persistence.xml中的<class>
元素和<jar>
元素中所有的注解类,都会被注册。