From cd868abbbd3cad1de7ca7c3a81ee499fadbabd14 Mon Sep 17 00:00:00 2001 From: John Interrante Date: Tue, 12 Aug 2014 19:25:51 -0400 Subject: [PATCH 1/3] Add AntJPADomainExporter and its test files Since querydsl-apt's apt-maven-plugin can't generate Querydsl query types when the JPA metamodel is specified in a mapping file (orm.xml) instead of using JPA annotations, add a custom Ant task which calls JPADomainExporter to generate Querydsl query types from the JPA metamodel in persistence.xml and orm.xml. Also add test files (persistence.xml, orm.xml, Departments.java, and AntJPADomainExporterTest.java) to make sure the Ant task works. --- .../query/jpa/ant/AntJPADomainExporter.java | 154 ++++++++++++++++++ .../jpa/ant/AntJPADomainExporterTest.java | 38 +++++ .../com/mysema/query/jpa/ant/Departments.java | 65 ++++++++ .../src/test/resources/META-INF/orm.xml | 32 ++++ .../test/resources/META-INF/persistence.xml | 18 ++ 5 files changed, 307 insertions(+) create mode 100644 querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java create mode 100644 querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/AntJPADomainExporterTest.java create mode 100644 querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/Departments.java create mode 100644 querydsl-jpa-codegen/src/test/resources/META-INF/orm.xml create mode 100644 querydsl-jpa-codegen/src/test/resources/META-INF/persistence.xml diff --git a/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java new file mode 100644 index 000000000..1dfe28f2b --- /dev/null +++ b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java @@ -0,0 +1,154 @@ +/** + * Copyright 2014, General Electric Company + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mysema.query.jpa.ant; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.metamodel.Metamodel; + +import com.mysema.query.jpa.codegen.JPADomainExporter; + +/** + * AntJPADomainExporter exports JPA 2 metamodels to Querydsl expression types + * + * @author 200003548 + */ +public class AntJPADomainExporter { + + /** + * Additional property to use when creating the JPA entity manager factory + */ + public static class Property { + private String name; + private String value; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + } + + /** + * Set of additional properties to use when creating the JPA entity manager factory + */ + public static class Configuration { + private Map properties = new HashMap(); + public Map getProperties() { + return properties; + } + public void addConfiguredProperty(Property property) { + properties.put(property.getName(), property.getValue()); + } + } + + /** + * Set of additional properties to use when creating the JPA entity manager factory + * (no default - not required) + */ + private Configuration configuration; + + /** + * Name prefix for generated query types (default: "Q") + */ + private String namePrefix = "Q"; + + /** + * Name suffix for generated query types (default: "") + */ + private String nameSuffix = ""; + + /** + * Target folder in which to generate query types (no default - required) + */ + private String targetFolder; + + /** + * Name of persistence unit from which to generate query types (no default - required) + */ + private String persistenceUnitName; + + public Configuration getConfiguration() { + return configuration; + } + + public void addConfiguration(Configuration configuration) { + this.configuration = configuration; + } + + public String getNamePrefix() { + return namePrefix; + } + + public void setNamePrefix(String namePrefix) { + this.namePrefix = namePrefix; + } + + public String getNameSuffix() { + return nameSuffix; + } + + public void setNameSuffix(String nameSuffix) { + this.nameSuffix = nameSuffix; + } + + public String getTargetFolder() { + return targetFolder; + } + + public void setTargetFolder(String targetFolder) { + this.targetFolder = targetFolder; + } + + public String getPersistenceUnitName() { + return persistenceUnitName; + } + + public void setPersistenceUnitName(String persistenceUnitName) { + this.persistenceUnitName = persistenceUnitName; + } + + /** + * Exports the named persistence unit's metamodel to Querydsl query types. Expects to be + * called by Ant via name convention using a method with signature public void execute(). + */ + public void execute() { + // We can assume we have the named persistence unit and its mapping file in our classpath, + // but we may have to allow some properties in that persistence unit to be overridden before + // we can successfully get that persistence unit's metamodel. + final Map properties = (configuration != null) ? configuration.getProperties() : null; + final EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, properties); + + // Now we can get the persistence unit's metamodel and export it to Querydsl query types. + final Metamodel configuration = emf.getMetamodel(); + final JPADomainExporter exporter = new JPADomainExporter(namePrefix, nameSuffix, new File(targetFolder), configuration); + try { + exporter.execute(); + } catch (IOException e) { + throw new RuntimeException("Error in JPADomainExporter", e); + } + } + +} diff --git a/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/AntJPADomainExporterTest.java b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/AntJPADomainExporterTest.java new file mode 100644 index 000000000..ecfd32803 --- /dev/null +++ b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/AntJPADomainExporterTest.java @@ -0,0 +1,38 @@ +/** + * Copyright 2014, General Electric Company + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mysema.query.jpa.ant; + +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.junit.Test; + +public class AntJPADomainExporterTest { + + @Test + public void testExecute() { + // Simulate configuring the task in an Ant build.xml file. + AntJPADomainExporter exporter = new AntJPADomainExporter(); + exporter.setNamePrefix("Q"); + exporter.setNameSuffix(""); + exporter.setTargetFolder("target/AntJPADomainExporterTest"); + exporter.setPersistenceUnitName("AntJPADomainExporterTest"); + exporter.execute(); + + // Verify that the Querydsl query type was created successfully. + assertTrue(new File("target/AntJPADomainExporterTest/com/mysema/query/jpa/ant/QDepartments.java").exists()); + } + +} diff --git a/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/Departments.java b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/Departments.java new file mode 100644 index 000000000..5064e18a0 --- /dev/null +++ b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/Departments.java @@ -0,0 +1,65 @@ +package com.mysema.query.jpa.ant; + +/** + * Auto-generated by: + * org.apache.openjpa.jdbc.meta.ReverseMappingTool$ReverseCodeGenerator + */ +public class Departments { + private Integer commentId; + + private String deptDesc; + + private int deptId; + + private String tag; + + private String timeZone; + + + public Departments() { + } + + public Departments(int deptId) { + this.deptId = deptId; + } + + public Integer getCommentId() { + return commentId; + } + + public void setCommentId(Integer commentId) { + this.commentId = commentId; + } + + public String getDeptDesc() { + return deptDesc; + } + + public void setDeptDesc(String deptDesc) { + this.deptDesc = deptDesc; + } + + public int getDeptId() { + return deptId; + } + + public void setDeptId(int deptId) { + this.deptId = deptId; + } + + public String getTag() { + return tag; + } + + public void setTag(String tag) { + this.tag = tag; + } + + public String getTimeZone() { + return timeZone; + } + + public void setTimeZone(String timeZone) { + this.timeZone = timeZone; + } +} diff --git a/querydsl-jpa-codegen/src/test/resources/META-INF/orm.xml b/querydsl-jpa-codegen/src/test/resources/META-INF/orm.xml new file mode 100644 index 000000000..5454ddac9 --- /dev/null +++ b/querydsl-jpa-codegen/src/test/resources/META-INF/orm.xml @@ -0,0 +1,32 @@ + + + + + com.mysema.query.jpa.ant + + + PROPERTY + + + + + + + + + + + + + + + + + + + + + + + diff --git a/querydsl-jpa-codegen/src/test/resources/META-INF/persistence.xml b/querydsl-jpa-codegen/src/test/resources/META-INF/persistence.xml new file mode 100644 index 000000000..89caf1507 --- /dev/null +++ b/querydsl-jpa-codegen/src/test/resources/META-INF/persistence.xml @@ -0,0 +1,18 @@ + + + + + org.hibernate.ejb.HibernatePersistence + META-INF/orm.xml + com.mysema.query.jpa.ant.Departments + true + + + + + + + + From 75795eb3c9788aee4b1efc041808aa5e29de91f1 Mon Sep 17 00:00:00 2001 From: John Interrante Date: Wed, 13 Aug 2014 15:53:36 -0400 Subject: [PATCH 2/3] Remove final modifiers --- .../com/mysema/query/jpa/ant/AntJPADomainExporter.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java index 1dfe28f2b..e11f380b7 100644 --- a/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java +++ b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java @@ -138,12 +138,12 @@ public class AntJPADomainExporter { // We can assume we have the named persistence unit and its mapping file in our classpath, // but we may have to allow some properties in that persistence unit to be overridden before // we can successfully get that persistence unit's metamodel. - final Map properties = (configuration != null) ? configuration.getProperties() : null; - final EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, properties); + Map properties = (configuration != null) ? configuration.getProperties() : null; + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, properties); // Now we can get the persistence unit's metamodel and export it to Querydsl query types. - final Metamodel configuration = emf.getMetamodel(); - final JPADomainExporter exporter = new JPADomainExporter(namePrefix, nameSuffix, new File(targetFolder), configuration); + Metamodel configuration = emf.getMetamodel(); + JPADomainExporter exporter = new JPADomainExporter(namePrefix, nameSuffix, new File(targetFolder), configuration); try { exporter.execute(); } catch (IOException e) { From 1f049a337eeb190883e9d52aa5752910967a00b8 Mon Sep 17 00:00:00 2001 From: John Interrante Date: Wed, 13 Aug 2014 16:06:04 -0400 Subject: [PATCH 3/3] Remove copyrights --- .../mysema/query/jpa/ant/AntJPADomainExporter.java | 13 ------------- .../query/jpa/ant/AntJPADomainExporterTest.java | 13 ------------- 2 files changed, 26 deletions(-) diff --git a/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java index e11f380b7..8dc5af85d 100644 --- a/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java +++ b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/ant/AntJPADomainExporter.java @@ -1,16 +1,3 @@ -/** - * Copyright 2014, General Electric Company - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package com.mysema.query.jpa.ant; import java.io.File; diff --git a/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/AntJPADomainExporterTest.java b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/AntJPADomainExporterTest.java index ecfd32803..fff9e5744 100644 --- a/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/AntJPADomainExporterTest.java +++ b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/ant/AntJPADomainExporterTest.java @@ -1,16 +1,3 @@ -/** - * Copyright 2014, General Electric Company - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package com.mysema.query.jpa.ant; import static org.junit.Assert.assertTrue;