You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2022/11/24 15:48:27 UTC
[cayenne] branch master updated: CAY-2781 Enable 'Create PK properties' by default in the cgen configuration
This is an automated email from the ASF dual-hosted git repository.
ntimofeev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git
The following commit(s) were added to refs/heads/master by this push:
new 61586e329 CAY-2781 Enable 'Create PK properties' by default in the cgen configuration
61586e329 is described below
commit 61586e3290ed38f6ca3e85fdebfe337f98cba203
Author: Nikita Timofeev <st...@gmail.com>
AuthorDate: Thu Nov 24 18:44:56 2022 +0300
CAY-2781 Enable 'Create PK properties' by default in the cgen configuration
---
.../java/org/apache/cayenne/gen/PropertyUtils.java | 31 +++++++++++++---------
.../cayenne/gen/BaseTemplatesGenerationTest.java | 10 +++++++
.../resources/templateTest/_auto/_ObjEntity.java | 3 +++
3 files changed, 32 insertions(+), 12 deletions(-)
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
index 4c4f7782b..c98550423 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
@@ -110,8 +110,11 @@ public class PropertyUtils {
this.logger = logger;
}
- public void addImportForPK(EntityUtils entityUtils) throws ClassNotFoundException {
+ public void addImportForPK(EntityUtils entityUtils) {
DbEntity entity = entityUtils.objEntity.getDbEntity();
+ if(entity == null) {
+ return;
+ }
boolean needToCreatePK = false;
for(DbAttribute attribute : entity.getPrimaryKeys()) {
@@ -134,7 +137,7 @@ public class PropertyUtils {
importUtils.addType(entity.getJavaClassName());
}
- public void addImport(ObjAttribute attribute) throws ClassNotFoundException {
+ public void addImport(ObjAttribute attribute) {
importUtils.addType(PropertyFactory.class.getName());
importUtils.addType(attribute.getType());
importUtils.addType(getPropertyDescriptor(attribute.getType()).getPropertyType());
@@ -143,13 +146,13 @@ public class PropertyUtils {
}
}
- public void addImport(EmbeddedAttribute attribute) throws ClassNotFoundException {
+ public void addImport(EmbeddedAttribute attribute) {
importUtils.addType(PropertyFactory.class.getName());
importUtils.addType(attribute.getType());
importUtils.addType(getPropertyDescriptor(EmbeddableObject.class.getName()).getPropertyType());
}
- public void addImport(EmbeddableAttribute attribute) throws ClassNotFoundException {
+ public void addImport(EmbeddableAttribute attribute) {
importUtils.addType(PropertyFactory.class.getName());
importUtils.addType(attribute.getType());
importUtils.addType(getPropertyDescriptor(attribute.getType()).getPropertyType());
@@ -174,7 +177,7 @@ public class PropertyUtils {
propertyType, propertyType);
}
- public String propertyDefinition(ObjEntity entity, DbAttribute attribute) throws ClassNotFoundException {
+ public String propertyDefinition(ObjEntity entity, DbAttribute attribute) {
StringUtils utils = StringUtils.getInstance();
String attributeType = TypesMapping.getJavaBySqlType(attribute);
@@ -193,7 +196,7 @@ public class PropertyUtils {
);
}
- public String propertyDefinition(ObjAttribute attribute) throws ClassNotFoundException {
+ public String propertyDefinition(ObjAttribute attribute) {
StringUtils utils = StringUtils.getInstance();
String attributeType = utils.stripGeneric(importUtils.formatJavaType(attribute.getType(), false));
PropertyDescriptor propertyDescriptor = getPropertyDescriptor(attribute.getType());
@@ -218,7 +221,7 @@ public class PropertyUtils {
return name;
}
- public String propertyDefinition(EmbeddedAttribute attribute) throws ClassNotFoundException {
+ public String propertyDefinition(EmbeddedAttribute attribute) {
StringUtils utils = StringUtils.getInstance();
String attributeType = utils.stripGeneric(importUtils.formatJavaType(attribute.getType(), false));
PropertyDescriptor propertyDescriptor = getPropertyDescriptor(EmbeddableObject.class.getName());
@@ -232,7 +235,7 @@ public class PropertyUtils {
);
}
- public String propertyDefinition(EmbeddableAttribute attribute) throws ClassNotFoundException {
+ public String propertyDefinition(EmbeddableAttribute attribute) {
StringUtils utils = StringUtils.getInstance();
String attributeType = utils.stripGeneric(importUtils.formatJavaType(attribute.getType(), false));
PropertyDescriptor propertyDescriptor = getPropertyDescriptor(attribute.getType());
@@ -339,10 +342,14 @@ public class PropertyUtils {
return FACTORY_METHODS.get(propertyType);
}
- private String getPkPropertyTypeForType(String attributeType) throws ClassNotFoundException {
- Class<?> javaClass = Class.forName(attributeType);
- if (Number.class.isAssignableFrom(javaClass)) {
- return NumericIdProperty.class.getName();
+ private String getPkPropertyTypeForType(String attributeType) {
+ try {
+ Class<?> javaClass = Class.forName(attributeType);
+ if (Number.class.isAssignableFrom(javaClass)) {
+ return NumericIdProperty.class.getName();
+ }
+ } catch (ClassNotFoundException ex) {
+ return BaseIdProperty.class.getName();
}
return BaseIdProperty.class.getName();
}
diff --git a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/BaseTemplatesGenerationTest.java b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/BaseTemplatesGenerationTest.java
index c3437615b..61a50253e 100644
--- a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/BaseTemplatesGenerationTest.java
+++ b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/BaseTemplatesGenerationTest.java
@@ -22,6 +22,7 @@ package org.apache.cayenne.gen;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.exp.ExpressionFactory;
import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.DbAttribute;
import org.apache.cayenne.map.DbEntity;
import org.apache.cayenne.map.ObjAttribute;
import org.apache.cayenne.map.ObjEntity;
@@ -41,6 +42,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
@@ -121,7 +123,15 @@ public class BaseTemplatesGenerationTest {
dataMap.setName("ObjEntity");
DbEntity dbEntity = new DbEntity();
+ DbAttribute dbAttribute = new DbAttribute("id");
+ dbAttribute.setMandatory(true);
+ dbAttribute.setPrimaryKey(true);
+ dbAttribute.setType(Types.INTEGER);
+ dbEntity.addAttribute(dbAttribute);
dbEntity.setName("EntityTest");
+ dbEntity.setDataMap(dataMap);
+ dataMap.addDbEntity(dbEntity);
+
objEntity.setDbEntity(dbEntity);
objEntity.setClassName("test.ObjEntity");
objEntity.setDataMap(dataMap);
diff --git a/cayenne-cgen/src/test/resources/templateTest/_auto/_ObjEntity.java b/cayenne-cgen/src/test/resources/templateTest/_auto/_ObjEntity.java
index 472967583..e9b9a9f20 100644
--- a/cayenne-cgen/src/test/resources/templateTest/_auto/_ObjEntity.java
+++ b/cayenne-cgen/src/test/resources/templateTest/_auto/_ObjEntity.java
@@ -6,6 +6,7 @@ import java.io.ObjectOutputStream;
import org.apache.cayenne.BaseDataObject;
import org.apache.cayenne.exp.property.EntityProperty;
+import org.apache.cayenne.exp.property.NumericIdProperty;
import org.apache.cayenne.exp.property.PropertyFactory;
import test.ObjEntity;
@@ -21,6 +22,8 @@ public abstract class _ObjEntity extends BaseDataObject {
public static final EntityProperty<ObjEntity> SELF = PropertyFactory.createSelf(ObjEntity.class);
+ public static final NumericIdProperty<Integer> ID_PK_PROPERTY = PropertyFactory.createNumericId("id", "null", Integer.class);
+ public static final String ID_PK_COLUMN = "id";
@Override