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.
configurations { processor } dependencies { // annotation processor: generate static metamodel processor 'com.cmobilecom:cmobilecom-jpa-processor:2.2.2' } // 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 }
configurations { processor } dependencies { // annotation processor: generate static metamodel processor 'com.cmobilecom:cmobilecom-jpa-processor:2.2.2' } dependencies { // Android does not have javax.annotation that is required 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=' + persistenceXmlClasspath } }The generated sources will be in the same source set and automatically compiled.
See Managed Type Enhancer for Kotlin DSL.