JPA Persistence Unit

JPA Persistence Unit

A JPA Persistence Unit is a logical grouping of user defined persistable classes (entity classes, embeddable classes and mapped superclasses) with related settings. Defining a persistence unit is optional when using ObjectDB, but required by JPA.

This page covers the following topics:

persistence.xml

Persistence units are defined in a persistence.xml file, which has to be located in the META-INF directory in the classpath. One persistence.xml file can include definitions for one or more persistence units. The portable way to instantiate an EntityManagerFactory close() EntityManagerFactory's method Close the factory, releasing any resources that it holds.
See JavaDoc Reference Page...
in JPA (as explained in the JPA Overview section) requires a persistence unit.

The following persistence.xml file defines one persistence unit:

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

A persistence unit is defined by a persistence-unit XML element. The required name attribute (“my-pu” in the example) identifies the persistence unit when instantiating an EntityManagerFactory close() EntityManagerFactory's method Close the factory, releasing any resources that it holds.
See JavaDoc Reference Page...
. It may also have optional sub elements:

  • The provider element indicates which JPA implementation should be used. ObjectDB is represented by the com.objectdb.jpa.Provider string. If not specified, the first JPA implementation that is found is used.
  • The mapping-file elements specify XML mapping files that are added to the default META-INF/orm.xml mapping file. Every annotation that is described in this manual can be replaced by equivalent XML in the mapping files (as explained below).
  • The jar-file elements specify JAR files that should be searched for managed persistable classes.
  • The class elements specify names of managed persistable classes (see below).
  • The property elements specify general properties. JPA 2 defines standard properties for specifying database url, username and password, as demonstrated above.

XML Mapping Metadata

ObjectDB supports using XML metadata as an alternative to annotations. Both JPA mapping files and JDO package.jdo files are supported. This manual focuses on using annotations which are more common and usually more convenient. Details on using XML metadata can be found in the JPA and JDO specifications and books.

Managed Persistable Classes

JPA requires registration of all the user defined persistable classes (entity classes, embeddable classes and mapped superclasses), which are referred to by JPA as managed classes, as part of a persistence unit definition.

Classes that are mentioned in mapping files as well as annotated classes in the JAR that contains the persistence.xml file (if it is packed) are registered automatically. If the application is not packed in a JAR yet, ObjectDB (as an extension) automatically registers classes under the classpath root directory that contains the META-INF/persistence.xml file. Other classes have to be registered explicitly by using class elements (for single class registration) or jar-file elements (for registration of all the classes in the jar file).

ObjectDB does not enforce registration of all the managed classes. However, it might be useful to register classes that define generators and named queries (by annotations). Otherwise, the generators and named queries are available only when the containing classes become known to ObjectDB, for example when a first instance of the class is stored in the database.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值