Cmobilecom JPA 2.0.RC1 Developer Guide

6. Persistence Unit

One META-INF/persistence.xml file can define one or more persistence unit, and multiple META-INF/persistence.xml files can be in classpath. Persistence unit names must be unique among all META-INF/persistence.xml files.

A persistence unit includes ORM mapping files and managed classes. For example,

/path/
    |-- puRoot/
    |       |-- META-INF/
    |       |       |-- persistence.xml
    |       |       |-- orm.xml
    |       |-- libs/
    |               |-- managed-classes-1.jar 	
    |-- libs/managed-classes-2.jar
/path/puRoot is the persistence unit root for those defined in the META-INF/persistence.xml. The META-INF/orm.xml under the root is the default mapping file for the persistence units, which will be loaded regardless of other mapping files.
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
		version="2.2">

	<persistence-unit name="jpa_example" transaction-type="RESOURCE_LOCAL">
		<mapping-file>conf/example-orm.xml</mapping-file>
		<jar-file>libs/managed-classes-1.jar</jar-file>
		<jar-file>../libs/managed-classes-2.jar</jar-file>
	
		<class>com.cmobilecom.jpa.example.type.Employee</class>
		<exclude-unlisted-classes>true</exclude-unlisted-classes>
		
		<properties>
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa_example_db"/>
			<property name="javax.persistence.jdbc.user" value="jpa_example_user" />
			<property name="javax.persistence.jdbc.password" value="123456" />
		</properties>
	</persistence-unit>

</persistence>
The referenced mapping file "conf/example-orm.xml" must be in classpath to be loaded, and jar file paths are relative to the persistence unit root. The jar files will be scanned for managed classes.

META-INF/orm.xml

<entity-mappings
		xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_2.xsd"
		version="2.2">

	<description>Persistence unit defaults</description>
	
	<persistence-unit-metadata>
		<persistence-unit-defaults>
			<entity-listeners>
				<entity-listener class="com.cmobilecom.jpa.example.DefaultListener" />
			</entity-listeners>
		</persistence-unit-defaults>
	</persistence-unit-metadata>

</entity-mappings>
conf/example-orm.xml
<entity-mappings
		xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_2.xsd"
		version="2.2">

	<description>Example managed classes</description>
	
	<package>com.cmobilecom.jpa.example.managed_classes</package>

	<mapped-superclass class="Person" />
	
	<entity class="Department" />
	<entity class="Employee" />
	<entity class="Employer" />
	
	<embeddable class="Address" />
	
	<converter class="PhoneNumberConverter" />
</entity-mappings>

ON DELETE CASCADE

An entity type may be mapped to multiple tables including primary table, secondary tables, and tables for superclass entity types for joined inheritance. To improve performance on deletion, add "ON DELETE CASCADE" to foreign key constraints among these tables. For example, Project entity joined inheritance:
Project  -- ProjectDetail
   |
LargeProject
LargeProject is a subclass entity of Project entity, which has a secondary table ProjectDetail. They are mapped to the following database tables(e.g., MySql):
create table Project (
	id bigint(20) not null AUTO_INCREMENT,
	...
	PRIMARY KEY (id)
)

create table ProjectDetail (
	projectId bigint(20) not null,
	...
	PRIMARY KEY (projectId),
	Constraint FK1 FOREIGN KEY (projectId) REFERENCES project (id) ON DELETE CASCADE
)

create table LargeProject (
	id bigint(20) not null,
	...
	PRIMARY KEY (id),
	Constraint FK1 FOREIGN KEY (id) REFERENCES project (id) ON DELETE CASCADE
)
When database schema is auto generated, "ON DELETE CASCADE" will be added to those foreign key constraints.

To tell Cmobilecom JPA that "ON DELETE CASCADE" is enabled for a persistence unit, set the following property to true in META-INF/persistence.xml.

	<persistence-unit name="jpa_example" transaction-type="RESOURCE_LOCAL">

		...
				
		<properties>
			<property name="CmobilecomJPA.PKasFK.onDeleteCascade" value="true" />
		</properties>
		
	</persistence-unit>
For MySql database, "ON DELETE CASCADE" must be added to those tables in entity inheritance hierarchy due to MySql relevant issues in multi-table deletion and update target table in subquery from clause.
Schema GenerationO/R MappingFrames / No Frames