#693 - ENH: Add support for mapping @DbJson to list of scalar types like List<String>

This commit is contained in:
Robin Bygrave committed 2016-05-05 16:36:16 +12:00
1 parent 8c86976e68
commit 2c95d9dc9a
30 files changed
+524 -98

No files matched your search

@@ -984,11 +984,11 @@ public class BeanProperty implements ElPropertyValue, Property {
/**
* Return the DB column type definition.
*/
public String renderDbType(DbType dbType) {
public String renderDbType(DbType dbType, boolean strict) {
if (dbColumnDefn != null) {
return dbColumnDefn;
}
return dbType.renderType(dbLength, dbScale);
return dbType.renderType(dbLength, dbScale, strict);
}
/**
@@ -175,12 +175,16 @@ public class AnnotationFields extends AnnotationParser {
if (comment != null) {
prop.setDbComment(comment.value());
}
if (get(prop, DbHstore.class) != null) {
util.setDbHstore(prop);
}
DbJson dbJson = get(prop, DbJson.class);
if (dbJson != null) {
util.setDbJsonType(prop, dbJson);
} else {
if (get(prop, DbJsonB.class) != null) {
util.setDbJsonBType(prop);
DbJsonB dbJsonB = get(prop, DbJsonB.class);
if (dbJsonB != null) {
util.setDbJsonBType(prop, dbJsonB);
}
}
@@ -197,10 +201,6 @@ public class AnnotationFields extends AnnotationParser {
prop.setDocProperty(docProperty);
}
if (get(prop, DbHstore.class) != null) {
util.setDbHstore(prop);
}
Formula formula = get(prop, Formula.class);
if (formula != null) {
prop.setSqlFormula(formula.select(), formula.join());
@@ -223,21 +223,18 @@ public class DeployCreateProperties {
Class<?> propertyType = field.getType();
ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
if (manyToOne != null){
Class<?> tt = manyToOne.targetEntity();
if (tt != null && !tt.equals(void.class)){
if (!tt.equals(void.class)){
propertyType = tt;
}
}
if (isMappedType(field)) {
if (isSpecialScalarType(field)) {
return new DeployBeanProperty(desc, propertyType, null, null);
}
// check for Collection type (list, set or map)
ManyType manyType = determineManyType.getManyType(propertyType);
if (manyType != null) {
// List, Set or Map based object
Class<?> targetType = determineTargetType(field);
@@ -299,7 +296,7 @@ public class DeployCreateProperties {
/**
* Return true if the field has one of the special mappings.
*/
private boolean isMappedType(Field field) {
private boolean isSpecialScalarType(Field field) {
return (field.getAnnotation(DbJson.class) != null)
|| (field.getAnnotation(DbJsonB.class) != null)
|| (field.getAnnotation(DbHstore.class) != null);
@@ -6,6 +6,7 @@ import javax.persistence.Enumerated;
import javax.persistence.PersistenceException;
import com.avaje.ebean.annotation.DbJson;
import com.avaje.ebean.annotation.DbJsonB;
import com.avaje.ebean.annotation.DbJsonType;
import com.avaje.ebean.config.EncryptDeploy;
import com.avaje.ebean.config.EncryptDeployManager;
@@ -103,6 +104,7 @@ public class DeployUtil {
return new DataEncryptSupport(encryptKeyManager, bytesEncryptor, table, column);
}
@SuppressWarnings("unchecked")
public void setEnumScalarType(Enumerated enumerated, DeployBeanProperty prop) {
Class<?> enumType = prop.getPropertyType();
@@ -200,11 +202,9 @@ public class DeployUtil {
* Map to Postgres HSTORE type.
*/
public void setDbHstore(DeployBeanProperty prop) {
ScalarType<?> scalarType = typeManager.getScalarType(DbType.HSTORE);
if (scalarType == null) {
// this should never occur actually
throw new RuntimeException("No ScalarType found for HSTORE on [" + prop.getFullBeanName() + "]");
throw new RuntimeException("No ScalarType found for HSTORE on [" + prop.getFullBeanName() + "] ?");
}
prop.setDbType(DbType.HSTORE);
prop.setScalarType(scalarType);
@@ -219,22 +219,20 @@ public class DeployUtil {
setDbJsonType(prop, dbType, dbJsonType.length());
}
public void setDbJsonBType(DeployBeanProperty prop) {
setDbJsonType(prop, DbType.JSONB, 0);
public void setDbJsonBType(DeployBeanProperty prop, DbJsonB dbJsonB) {
setDbJsonType(prop, DbType.JSONB, dbJsonB.length());
}
private void setDbJsonType(DeployBeanProperty prop, int dbType, int dbLength) {
Class<?> type = prop.getPropertyType();
ScalarType<?> scalarType = typeManager.getJsonScalarType(type, dbType);
ScalarType<?> scalarType = typeManager.getJsonScalarType(type, dbType, dbLength);
if (scalarType == null) {
// this should never occur actually
throw new RuntimeException("No ScalarType for JSON type [" + type + "] [" + dbType + "]");
}
prop.setDbType(dbType);
prop.setScalarType(scalarType);
if (dbType == Types.VARCHAR) {
if (dbType == Types.VARCHAR || dbLength > 0) {
// determine the db column size
int columnLength = (dbLength > 0) ? dbLength : DEFAULT_JSON_VARCHAR_LENGTH;
prop.setDbLength(columnLength);