Persistence Unit

Managed classes and converters need to be listed in META-INF/persistence.xml or referenced orm.xml files. To keep Cmobilecom JPA light-weight, scanning jars to discover managed types is not supported. For example,

META-INF/persistence.xml

<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_2_0.xsd"
			 version="2.0">

	<persistence-unit name="jpa_example" transaction-type="RESOURCE_LOCAL">
		<mapping-file>orm.xml</mapping-file>

		<class>com.cmobilecom.jpa.example.type.Employee</class>
		
		<exclude-unlisted-classes>false</exclude-unlisted-classes>
		
		<properties>
			
		</properties>
		
	</persistence-unit>

</persistence>
If the mapped superclasses of listed classes are not listed, and the value of the element "exclude-unlisted-classes" is false, they will be added to metamodel automatically.

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_1.xsd"
	version="2.1">

	<description>Example managed types</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>
	
	<package>com.cmobilecom.jpa.example.type</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.