Cmobilecom JPA 2.0.RC1 Developer Guide

3. Static Metamodel Generator

Static metamodel is used for constructing strongly-typed criteria. For example,
	EntityManager em = ...
	CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
	
	CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
	Root<Employee> root = criteriaQuery.from(Employee.class);
	root.fetch(Employee_.phoneNumbers, JoinType.LEFT);
	Join<Employee, Department> department = root.join(Employee_.department);
	criteriaQuery.where(criteriaBuilder.equals(department.get(Department_.name), "HR"));
	criteriaQuery.orderBy(criteriaBuilder.asc(root.get(Employee_.id)));
		
	TypedQuery<Employee> query = em.createQuery(criteriaQuery);
	List<Employee> employees = query.getResultList();
The Employee_ and Department_ are the static metamodel classes for Employee and Department respectively, which have the same package names as their corresponding classes.

To generate static metamodel for managed classes (Entity, MappedSuperclass and Embeddable), add cmobilecom-jpa-processor.jar (which contains an annotation processor) in annotation processor classpath, and pass option -ApersistenceXmlClasspath to the annotation processor.

The following examples for java and android use gradle build.

Java

Example: source set "test" contains managed classes.
configurations {
	processor
}
	
dependencies {
	// annotation processor: generate static metamodel
	processor 'com.cmobilecom:cmobilecom-jpa-processor:2.0.RC1'
}

// StaticMetamodelGenerator/Enhancer: classpath for loading META-INF/persistence.xml and 
// referenced orm mapping xml files.
def persistenceXmlClasspath = files('src/test/resources').asPath;
	
compileTestJava {
	// annotation processor: generate static metamodel source code which will be
	// added into sourceSet.java.srcDirs
	options.annotationProcessorPath = configurations.processor
	options.compilerArgs << '-ApersistenceXmlClasspath=' + persistenceXmlClasspath
}

Android

Example: source set "androidTest" contains managed classes.
configurations {
	processor
}
	
dependencies {
	// annotation processor: generate static metamodel
	processor 'com.cmobilecom:cmobilecom-jpa-processor:2.0.RC1'
}
	
dependencies {
	// Android does not have javax.annotation that is requried to compile generated sources,
	// but it is not needed at runtime.
	compileOnly group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
	annotationProcessor configurations.processor
}
	
afterEvaluate {
	// StaticMetamodelGenerator/Enhancer: classpath for loading META-INF/persistence.xml and 
	// referenced orm mapping xml files.
	def persistenceXmlClasspath = files('src/androidTest/resources').asPath;

	compileDebugAndroidTestJavaWithJavac {
		options.compilerArgs << '-ApersistenceXmlClasspath=' + persisteneXmlClasspath
	}
}	
The generated sources will be in the same source set and automatically compiled.
Get StartedManaged Type EnhancerFrames / No Frames