branch develop updated (644750d -> d6e7362)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository topia. See http://git.nuiton.org/topia.git from 644750d Introduce attributeType tag value Closes #3765 Merge branch 'feature/3765' into develop new 7004d98 Add generateForeignKeyNames tag value definition (See #3722) new 5d0c239 - Generate foreign key names if tag value found (See #3722) - Generate schema everywhere in hibernate mapping (See #3726) new 694659d Fix hibernateType and attributeType tag value loading when specify on an attribute. new d6e7362 Consider schema everywhere (Closes #3726) Generate foreign key names (Closes #3722) The 4 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit d6e7362270a41d327a1a872f212ebf21ace6f626 Merge: 644750d 694659d Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 23 19:24:26 2015 +0200 Consider schema everywhere (Closes #3726) Generate foreign key names (Closes #3722) Merge branch 'feature/schema_and_dbnames_customisation' into develop commit 694659d0182a0738758fb88a68c51a161656158f Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 23 19:18:17 2015 +0200 Fix hibernateType and attributeType tag value loading when specify on an attribute. commit 5d0c23912fd2e6c8d4704539c1b5b640c7615d41 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 23 19:13:21 2015 +0200 - Generate foreign key names if tag value found (See #3722) - Generate schema everywhere in hibernate mapping (See #3726) commit 7004d98708d998217a44c26b51d3dc6b034eecbd Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 23 19:12:19 2015 +0200 Add generateForeignKeyNames tag value definition (See #3722) Summary of changes: .../templates/EntityHibernateMappingGenerator.java | 233 ++++++++++++++------- .../org/nuiton/topia/templates/TopiaTagValues.java | 45 +++- .../topia/templates/TopiaTemplateHelper.java | 40 ++++ 3 files changed, 243 insertions(+), 75 deletions(-) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository topia. See http://git.nuiton.org/topia.git commit 7004d98708d998217a44c26b51d3dc6b034eecbd Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 23 19:12:19 2015 +0200 Add generateForeignKeyNames tag value definition (See #3722) --- .../org/nuiton/topia/templates/TopiaTagValues.java | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTagValues.java b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTagValues.java index eda2348..ebc18d4 100644 --- a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTagValues.java +++ b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTagValues.java @@ -353,6 +353,16 @@ public class TopiaTagValues extends TagValueDefinitionProvider { public static final String TAG_INDEX_FOREIGN_KEYS = "indexForeignKeys"; /** + * Tag to generate deterministic foreign key names in hibernate mapping files. + * + * @see #isGenerateForeignKeyNames(ObjectModelClassifier, ObjectModel) + * @since 3.0.1 + */ + @TagValueDefinition(target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To generate deterministic foreign keys names in hibernate mappings.") + public static final String TAG_GENERATE_FOREIGN_KEY_NAMES = "generateForeignKeyNames"; + + /** * Tag to specify a super class to use instead of * {@link org.nuiton.topia.persistence.internal.AbstractTopiaPersistenceContext}. * <p/> @@ -730,6 +740,23 @@ public class TopiaTagValues extends TagValueDefinitionProvider { return value; } + /** + * Obtain the value of the {@link TopiaTagValues#TAG_GENERATE_FOREIGN_KEY_NAMES} + * tag value on the given model or classifier and returns {@code true} if the tag value was found and value {@code true}. + * + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return {@code true} if tag value was found on classifier or model and his value is {@code true}, otherwise {@code false}. + * @see TopiaTagValues#TAG_GENERATE_FOREIGN_KEY_NAMES + * @since 2.10 + */ + public boolean isGenerateForeignKeyNames(ObjectModelClassifier classifier, ObjectModel model) { + boolean value = findBooleanTagValue(TAG_GENERATE_FOREIGN_KEY_NAMES, classifier, model); + return value ; + } + protected static final Map<String, String> HIBERNATE_ATTRIBUTE_TYPE_DEFAULT_VALUES = Maps.newHashMap(); static { -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository topia. See http://git.nuiton.org/topia.git commit 5d0c23912fd2e6c8d4704539c1b5b640c7615d41 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 23 19:13:21 2015 +0200 - Generate foreign key names if tag value found (See #3722) - Generate schema everywhere in hibernate mapping (See #3726) --- .../templates/EntityHibernateMappingGenerator.java | 233 ++++++++++++++------- .../topia/templates/TopiaTemplateHelper.java | 40 ++++ 2 files changed, 203 insertions(+), 70 deletions(-) diff --git a/topia-templates/src/main/java/org/nuiton/topia/templates/EntityHibernateMappingGenerator.java b/topia-templates/src/main/java/org/nuiton/topia/templates/EntityHibernateMappingGenerator.java index 9a37a83..c3f3868 100644 --- a/topia-templates/src/main/java/org/nuiton/topia/templates/EntityHibernateMappingGenerator.java +++ b/topia-templates/src/main/java/org/nuiton/topia/templates/EntityHibernateMappingGenerator.java @@ -31,6 +31,7 @@ import org.apache.commons.logging.LogFactory; import org.nuiton.eugene.EugeneStereoTypes; import org.nuiton.eugene.GeneratorUtil; import org.nuiton.eugene.java.JavaGeneratorUtil; +import org.nuiton.eugene.models.object.ObjectModel; import org.nuiton.eugene.models.object.ObjectModelAssociationClass; import org.nuiton.eugene.models.object.ObjectModelAttribute; import org.nuiton.eugene.models.object.ObjectModelClass; @@ -98,10 +99,68 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { public static final String HIBERNATE_ATTRIBUTE_DISCRIMINATOR_VALUE= "discriminator-value"; + public static final String HIBERNATE_ATTRIBUTE_FOREIGN_KEY = "foreign-key"; + protected TopiaTemplateHelper templateHelper; protected TopiaTagValues topiaTagValues; + class ClassContext { + + private final ObjectModel model; + + private final ObjectModelClass input; + + private final boolean generateForeignKeyNames; + + private final String tableName; + + private final String schema; + + private ObjectModelPackage aPackage; + + ClassContext(ObjectModel model, ObjectModelPackage aPackage, ObjectModelClass input) { + this.model = model; + this.input = input; + this.aPackage=aPackage; + this.generateForeignKeyNames = topiaTagValues.isGenerateForeignKeyNames(input, model); + this.tableName = templateHelper.getDbName(input); + this.schema = topiaTagValues.getDbSchemaNameTagValue(input, aPackage, model); + } + + public boolean isGenerateForeignKeyNames() { + return generateForeignKeyNames; + } + + public String getTableName() { + return tableName; + } + + public boolean isUseSchema() { + return schema != null; + } + + public String getSchema() { + return schema; + } + + public String getForeignKeyName(String attrColumn) { + return getForeignKeyName(tableName , attrColumn).toLowerCase(); + } + + public String getForeignKeyName(String tableName, String attrColumn) { + return ("fk_" + tableName + "_" + attrColumn).toLowerCase(); + } + + public ObjectModelClass getInput() { + return input; + } + + public ObjectModelPackage getPackage() { + return aPackage; + } + } + @Override public String getFilenameForClass(ObjectModelClass clazz) { if (templateHelper == null) { @@ -127,6 +186,10 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { templateHelper.PERSISTENCE_TYPE_HIBERNATE.equals(persistenceType)) { return; } + + ObjectModelPackage aPackage = model.getPackage(input); + ClassContext classContext = new ClassContext(model, aPackage, input); + /*{<?xml version="1.0" encoding="UTF-8"?> <hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping" xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping classpath://org/hibernate/hibernate-mapping-4.0.xsd" @@ -140,15 +203,15 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { List<ObjectModelAttribute> noneNaturalAttributes = new ArrayList<ObjectModelAttribute>(); String clazzDOType = templateHelper.getDOType(input, model); - String tableName = templateHelper.getDbName(input); + String tableName = classContext.getTableName(); String isAbstract = BooleanUtils.toStringTrueFalse(input.isAbstract()); String clazzFQN = input.getQualifiedName(); Map<String,String> optionalAttributesMap = new HashMap<String, String>(); - ObjectModelPackage aPackage = model.getPackage(input); - String schema = topiaTagValues.getDbSchemaNameTagValue(input, aPackage, model); - generateFromTagValue(optionalAttributesMap,HIBERNATE_ATTRIBUTE_SCHEMA, schema); + if (classContext.isUseSchema()) { + generateFromTagValue(optionalAttributesMap,HIBERNATE_ATTRIBUTE_SCHEMA, classContext.getSchema()); + } //On précise au proxy de quelle interface hérite l'objet @@ -213,7 +276,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { } /*{ <natural-id<%=mutableStr%>> }*/ - generateAttributes(output, aPackage, input, naturalAttributes, " "); + generateAttributes(output, classContext, naturalAttributes, " "); /*{ </natural-id> }*/ } @@ -222,7 +285,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { }*/ } - generateAttributes(output, aPackage, input, noneNaturalAttributes, ""); + generateAttributes(output, classContext, noneNaturalAttributes, ""); if (haveSuper) { /*{ </<%=inheritanceStrategy%>> @@ -232,21 +295,22 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { }*/ } - generateDatabaseObjects(output, input, naturalAttributes); - generateDatabaseObjects(output, input, noneNaturalAttributes); + generateDatabaseObjects(output, classContext, naturalAttributes); + generateDatabaseObjects(output, classContext, noneNaturalAttributes); /*{</hibernate-mapping> }*/ } protected void generateDatabaseObjects(Writer output, - ObjectModelClass clazz, - List<ObjectModelAttribute> attributes) throws IOException { + ClassContext classContext, + List<ObjectModelAttribute> attributes) throws IOException { for (ObjectModelAttribute attribute : attributes) { if (!attribute.isNavigable() || attribute.hasAssociationClass() || !GeneratorUtil.isNMultiplicity(attribute) || + GeneratorUtil.isNMultiplicity(attribute.getReverseMaxMultiplicity()) || attribute.getClassifier() == null || !templateHelper.isEntity(attribute.getClassifier()) || templateHelper.isAbstract(attribute.getClassifier()) @@ -256,7 +320,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { continue; } - ObjectModelPackage aPackage = model.getPackage(clazz); + ObjectModelPackage aPackage = classContext.getPackage(); boolean indexForeignKeys = topiaTagValues.getIndexForeignKeysTagValue(attribute, aPackage, model); @@ -269,28 +333,34 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { // add database-object to create and drop index + String schema = classContext.getSchema(); + boolean withSchema = classContext.isUseSchema(); String tableName; - String indexName = "idx_" + clazz.getName() + "_" + attribute.getName(); String propertyName; - - - if (GeneratorUtil.isNMultiplicity(attribute.getReverseMaxMultiplicity())) { - - // many to many - tableName = templateHelper.getManyToManyTableName(attribute); - propertyName = templateHelper.getDbName(attribute.getReverseAttribute()); - } else { + // On many to many relation, the index is already generated by the other table +// if (GeneratorUtil.isNMultiplicity(attribute.getReverseMaxMultiplicity())) { +// +// // many to many +// tableName = templateHelper.getManyToManyTableName(attribute); +// propertyName = templateHelper.getReverseDbNameOnReverseAttribute(attribute); +// } else { // one to many tableName =templateHelper.getDbName(attribute.getClassifier()); - propertyName = templateHelper.getDbName(attribute.getReverseAttribute()); - } + propertyName = templateHelper.getReverseDbNameOnReverseAttribute(attribute.getReverseAttribute()); +// } // add schema if exist (http://nuiton.org/issues/2052) - String schema = topiaTagValues.getDbSchemaNameTagValue(clazz, aPackage, model); - if (StringUtils.isNotEmpty(schema)) { + String indexName = "idx"; + if (withSchema) { + indexName += '_' + schema; + } + indexName += '_' + tableName+ '_' + propertyName ; + indexName = indexName.toLowerCase(); + if (withSchema) { tableName = schema + "." + tableName; } + /*{ <database-object> <create>CREATE INDEX <%=indexName%> ON <%=tableName%>(<%=propertyName%>)</create> <drop>DROP INDEX <%=indexName%></drop> @@ -300,8 +370,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { } } protected void generateAttributes(Writer output, - ObjectModelPackage aPackage, - ObjectModelClass clazz, + ClassContext classContext, List<ObjectModelAttribute> attributes, String prefix) throws IOException { for (ObjectModelAttribute attr : attributes) { @@ -311,35 +380,37 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { // pour pouvoir supprimer en cascade l'asso lors de la suppression // d'un des cotes if (attr.isNavigable() - || templateHelper.hasUnidirectionalRelationOnAbstractType(reverse, model) - || attr.hasAssociationClass()) { + || templateHelper.hasUnidirectionalRelationOnAbstractType(reverse, model) + || attr.hasAssociationClass()) { if (!GeneratorUtil.isNMultiplicity(attr)) { if (attr.getClassifier() != null && templateHelper.isEntity(attr.getClassifier())) { if (GeneratorUtil.isNMultiplicity(attr.getReverseMaxMultiplicity()) && !attr.hasAssociationClass()) { - generateHibernateManyToOne(output, aPackage, clazz,attr, prefix); + generateHibernateManyToOne(output, classContext, attr, prefix); } else { - generateHibernateOneToOne(output, aPackage, clazz,attr, prefix); + generateHibernateOneToOne(output, classContext, attr, prefix); } } else { - generateHibernateProperty(output, aPackage, clazz, attr, prefix); + generateHibernateProperty(output, classContext, attr, prefix); } } else { if (attr.getClassifier() != null && templateHelper.isEntity(attr.getClassifier())) { if (GeneratorUtil.isNMultiplicity(attr.getReverseMaxMultiplicity()) && !attr.hasAssociationClass()) { - generateHibernateManyToMany(output, aPackage, clazz,attr, prefix); + generateHibernateManyToMany(output, classContext, attr, prefix); } else { - generateHibernateOneToMany(output, aPackage, clazz,attr, prefix); + generateHibernateOneToMany(output, classContext, attr, prefix); } } else { - generateHibernateMany(output, aPackage, clazz, attr, prefix); + generateHibernateMany(output, classContext, attr, prefix); } } } } //Attributs pour les classes d'association + ObjectModelClass clazz = classContext.getInput(); + ObjectModelPackage aPackage = classContext.getPackage(); if (clazz instanceof ObjectModelAssociationClass) { - ObjectModelAssociationClass assoc = (ObjectModelAssociationClass)clazz; + ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) clazz; for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { @@ -355,7 +426,11 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { String attrType = getType(aPackage, clazz, attr, true); String lazy = generateFromTagValue(HIBERNATE_ATTRIBUTE_LAZY, topiaTagValues.getLazyTagValue(attr)); String attrColumn = templateHelper.getDbName(attr); -/*{<%=prefix%> <many-to-one name="<%=attrName%>" class="<%=attrType%>" <%=lazy%>column="<%=attrColumn%>" node="<%=attrName%>/@topiaId" <%=notNull%>/> + String foreignKeyName = ""; + if (classContext.isGenerateForeignKeyNames()) { + foreignKeyName = " " + generateFromTagValue(HIBERNATE_ATTRIBUTE_FOREIGN_KEY, classContext.getForeignKeyName(attrColumn)).trim(); + } +/*{<%=prefix%> <many-to-one name="<%=attrName%>" class="<%=attrType%>" <%=lazy%>column="<%=attrColumn%>" node="<%=attrName%>/@topiaId" <%=notNull%><%=foreignKeyName%>/> }*/ // } //Ne sert plus grâce à l'utilisation de la navigabilité @@ -388,6 +463,10 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { return result; } + protected String getType(ClassContext clazzContext, ObjectModelAttribute attr) { + return getType(clazzContext.getPackage(), clazzContext.getInput(), attr, false); + } + protected String getType(ObjectModelPackage aPackage, ObjectModelClass clazz, ObjectModelAttribute attr) { return getType(aPackage, clazz, attr, false); } @@ -399,7 +478,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { } String type = attr.getType(); - String attrType = topiaTagValues.getHibernateAttributeType(attr, clazz, aPackage, model); + String attrType = topiaTagValues.getHibernateAttributeType(attr, clazz, aPackage, model); if (StringUtils.isNotEmpty(attrType)) { // tag value detected of the attribute type = attrType; @@ -435,7 +514,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { // tag value detected of the model String deprecatedTagName = "model.tagValue." + type; - String tagName = "model.tagValue." + topiaTagValues.TAG_HIBERNATE_ATTRIBUTE_TYPE + + String tagName = "model.tagValue." + TopiaTagValues.TAG_HIBERNATE_ATTRIBUTE_TYPE + "." + type; log.warn("---------"); log.warn("You are using a deprecated tagValue ("+deprecatedTagName+"), replace it by"); @@ -467,11 +546,10 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { } protected void generateHibernateProperty(Writer output, - ObjectModelPackage aPackage, - ObjectModelClass clazz, + ClassContext classContext, ObjectModelAttribute attr, String prefix) throws IOException { - String attrType = getType(aPackage, clazz, attr); + String attrType = getType(classContext, attr); String accessField = "field"; String tagValue = topiaTagValues.getAccessTagValue(attr); @@ -490,8 +568,9 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { Map<String,String> optionalAttributesMap = new HashMap<String, String>(); - String schema = topiaTagValues.getDbSchemaNameTagValue(clazz, aPackage, model); - generateFromTagValue(optionalAttributesMap, HIBERNATE_ATTRIBUTE_SCHEMA, schema); + if (classContext.isUseSchema()) { + generateFromTagValue(optionalAttributesMap, HIBERNATE_ATTRIBUTE_SCHEMA, classContext.getSchema()); + } if (JavaGeneratorUtil.isOrdered(attr)) { String indexName = tableName + "_idx"; @@ -567,7 +646,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { // if the user tuned the model to use name instead of // ordinal to store the values, we must add a clause - boolean useEnumerationName = topiaTagValues.hasUseEnumerationNameTagValue(attr, clazz, aPackage, model); + boolean useEnumerationName = topiaTagValues.hasUseEnumerationNameTagValue(attr, classContext.getInput(), classContext.getPackage(), model); if (useEnumerationName) { String enumSQLType = String.valueOf(Types.VARCHAR); /*{ @@ -619,8 +698,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { } protected void generateHibernateOneToOne(Writer output, - ObjectModelPackage aPackage, - ObjectModelClass clazz, + ClassContext classContext, ObjectModelAttribute attr, String prefix) throws IOException { // boolean accessField = hasUnidirectionalRelationOnAbstractType(attr.getReverseAttribute(), model); @@ -631,13 +709,12 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { // but if it is one-to-zero-or-one unique contraints is violated // with null values boolean unique = GeneratorUtil.isOneMultiplicity(attr); - generateHibernateManyToOne(output, aPackage, clazz, attr, unique, prefix); + generateHibernateManyToOne(output, classContext, attr, unique, prefix); } protected void generateHibernateOneToMany(Writer output, - ObjectModelPackage aPackage, - ObjectModelClass clazz, + ClassContext classContext, ObjectModelAttribute attr, String prefix) throws IOException { boolean needsIndex = JavaGeneratorUtil.isOrdered(attr); @@ -645,7 +722,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { isInverse |= templateHelper.hasUnidirectionalRelationOnAbstractType(attr, model); String attrName = getName(attr); // ??? - String attrType = getType(aPackage, clazz, attr); + String attrType = getType(classContext, attr); String reverseAttrDBName = templateHelper.getReverseDbName(attr); String orderBy = generateFromTagValue(HIBERNATE_ATTRIBUTE_ORDER_BY, topiaTagValues.getOrderByTagValue(attr)); @@ -663,16 +740,21 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { if (isInverse) { inverse = HIBERNATE_ATTRIBUTE_INVERSE+"=\"true\" "; } + String foreignKeyAttribute = ""; + if (classContext.isGenerateForeignKeyNames()) { + String columnName = templateHelper.getDbName(attr); + foreignKeyAttribute = " " + generateFromTagValue(HIBERNATE_ATTRIBUTE_FOREIGN_KEY, classContext.getForeignKeyName(columnName)).trim(); + } if (needsIndex) { /*{<%=prefix%> <<%=collType%> name="<%=attrName%>" <%=inverse%><%=lazy%><%=cascade%>node="<%=attrName%>"> -<%=prefix%> <key column="<%=reverseAttrDBName%>"/> +<%=prefix%> <key column="<%=reverseAttrDBName%>"<%=foreignKeyAttribute%>/> <%=prefix%> <list-index column="<%=reverseAttrDBName%>_idx"/> <%=prefix%> <one-to-many class="<%=attrType%>" node="topiaId"/> <%=prefix%> </<%=collType%>> }*/ }else { /*{<%=prefix%> <<%=collType%> name="<%=attrName%>" <%=inverse%><%=orderBy%><%=fetch%><%=lazy%><%=cascade%>node="<%=attrName%>"> -<%=prefix%> <key column="<%=reverseAttrDBName%>"/> +<%=prefix%> <key column="<%=reverseAttrDBName%>"<%=foreignKeyAttribute%>/> <%=prefix%> <one-to-many class="<%=attrType%>" node="topiaId"/> <%=prefix%> </<%=collType%>> }*/ @@ -730,16 +812,19 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { } protected void generateHibernateMany(Writer output, - ObjectModelPackage aPackage, - ObjectModelClass clazz, + ClassContext classContext, ObjectModelAttribute attr, String prefix) throws IOException { boolean needsIndex = JavaGeneratorUtil.isOrdered(attr); String attrName = getName(attr); - String attrType = getType(aPackage, clazz, attr); + String attrType = getType(classContext, attr); String collType = templateHelper.getNMultiplicityHibernateType(attr); String lazy = generateFromTagValue(HIBERNATE_ATTRIBUTE_LAZY, topiaTagValues.getLazyTagValue(attr)); String attrColumn = templateHelper.getDbName(attr); + String foreignKeyAttribute = ""; + if (classContext.isGenerateForeignKeyNames()) { + foreignKeyAttribute = " " + HIBERNATE_ATTRIBUTE_FOREIGN_KEY + "=\"" + classContext.getTableName() + "_" + attrColumn + "\""; + } boolean attrIsEnumeration = attr.getClassifier() != null && attr.getClassifier().isEnum(); @@ -752,7 +837,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { } if (attrIsEnumeration) { - boolean useEnumerationName = topiaTagValues.hasUseEnumerationNameTagValue(attr, clazz, aPackage, model); + boolean useEnumerationName = topiaTagValues.hasUseEnumerationNameTagValue(attr, classContext.getInput(), classContext.getPackage(), model); /*{<%=prefix%> <element column="<%=attrColumn%>" node="id"> <%=prefix%> <type name="org.hibernate.type.EnumType"> <%=prefix%> <param name="<%=org.hibernate.type.EnumType.ENUM%>"><%=attrType%></param> @@ -777,26 +862,28 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { } protected void generateHibernateManyToOne(Writer output, - ObjectModelPackage aPackage, - ObjectModelClass clazz, + ClassContext classContext, ObjectModelAttribute attr, String prefix) throws IOException { - generateHibernateManyToOne(output, aPackage, clazz, attr, false, prefix); + generateHibernateManyToOne(output, classContext, attr, false, prefix); } protected void generateHibernateManyToOne(Writer output, - ObjectModelPackage aPackage, - ObjectModelClass clazz, + ClassContext classContext, ObjectModelAttribute attr, boolean isUnique, String prefix) throws IOException { String attrName = getName(attr); - String attrType = getType(aPackage, clazz, attr); + String attrType = getType(classContext, attr); String attrColumn = templateHelper.getDbName(attr); /*{<%=prefix%> <many-to-one name="<%=attrName%>" class="<%=attrType%>" column="<%=attrColumn%>" }*/ if (attr.isComposite() || attr.hasAssociationClass()) { /*{cascade="delete" }*/ } + if (classContext.isGenerateForeignKeyNames()) { + String foreignKeyName = generateFromTagValue(HIBERNATE_ATTRIBUTE_FOREIGN_KEY, classContext.getForeignKeyName(attrColumn)); +/*{<%=foreignKeyName%>}*/ + } // Pour le test suivant, on verifie d'abord que l'attribut a un reverse. // S'il n'en a pas, cela signifie qu'il ne s'agit pas d'un entite // (au sens stereotype entity), donc a donc pas besoin de faire un access=field. @@ -818,8 +905,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { } protected void generateHibernateManyToMany(Writer output, - ObjectModelPackage aPackage, - ObjectModelClass clazz, + ClassContext classContext, ObjectModelAttribute attr, String prefix) throws IOException { // On ne met le inverse="true" uniquement pour un seul coté de la relation. @@ -849,7 +935,7 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { cascade = " " + HIBERNATE_ATTRIBUTE_CASCADE + "=\"delete,delete-orphan\""; } - String attrType = getType(aPackage, clazz, attr); + String attrType = getType(classContext, attr); String attrName = getName(attr); String attrColumn = templateHelper.getDbName(attr); String lazy = generateFromTagValue(HIBERNATE_ATTRIBUTE_LAZY, topiaTagValues.getLazyTagValue(attr), "true"); @@ -861,19 +947,26 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { inverse = HIBERNATE_ATTRIBUTE_INVERSE+"=\"true\" "; } String reverseAttrDBName = templateHelper.getReverseDbName(attr); - Map<String,String> optionalAttributesMap = new HashMap<String, String>(); - String schema = topiaTagValues.getDbSchemaNameTagValue(clazz, aPackage, model); - generateFromTagValue(optionalAttributesMap, HIBERNATE_ATTRIBUTE_SCHEMA, schema); + Map<String, String> optionalAttributesMap = new HashMap<String, String>(); + if (classContext.isUseSchema()) { + generateFromTagValue(optionalAttributesMap, HIBERNATE_ATTRIBUTE_SCHEMA, classContext.getSchema()); + } String optionalAttributes=attributesToString(optionalAttributesMap); + String foreignKeyName = ""; + String reverseForeignKeyName = ""; + if (classContext.isGenerateForeignKeyNames()) { + foreignKeyName = " " + generateFromTagValue(HIBERNATE_ATTRIBUTE_FOREIGN_KEY, classContext.getForeignKeyName(tableName, reverseAttrDBName)).trim(); + reverseForeignKeyName = " " + generateFromTagValue(HIBERNATE_ATTRIBUTE_FOREIGN_KEY, classContext.getForeignKeyName(tableName, attrColumn)).trim(); + } /*{<%=prefix%> <<%=collType%> name="<%=attrName%>" table="<%=tableName%>" <%=inverse%><%=lazy%><%=cascade%> node="<%=attrName%>"<%=optionalAttributes%>> -<%=prefix%> <key column="<%=reverseAttrDBName%>"/> +<%=prefix%> <key column="<%=reverseAttrDBName%>"<%=foreignKeyName%>/> }*/ if (needsIndex) { /*{<%=prefix%> <list-index column="<%=reverseAttrDBName%>_idx"/> }*/ } -/*{<%=prefix%> <many-to-many class="<%=attrType%>" column="<%=attrColumn%>" <%=orderBy%>node="topiaId"/> +/*{<%=prefix%> <many-to-many class="<%=attrType%>" column="<%=attrColumn%>" <%=orderBy%>node="topiaId"<%=reverseForeignKeyName%>/> <%=prefix%> </<%=collType%>> }*/ } diff --git a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTemplateHelper.java b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTemplateHelper.java index 1d0020d..24821ed 100644 --- a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTemplateHelper.java +++ b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTemplateHelper.java @@ -284,6 +284,46 @@ public class TopiaTemplateHelper { } /** + * Obtain the reverse db name of a reverse attribute. + * + * <strong>Note that the reverse attribute can't be null here.</strong> + * <ul> + * <li>Try first to get the reverse db Name from the ReverseDbname tag-value</li> + * <li>If not found, try then the ReverseDbname tag-value on the same attribute but from this other side of the relation</li> + * <li>If not found, try then just get the name of the reverse attribute</li> + * </ul> + * @param attr the attribute to seek + * @return the value of the reverse db name on the reverse attribute + * @since 2.9.5.2 + */ + public String getReverseDbNameOnReverseAttribute(ObjectModelAttribute attr) { + + ObjectModelAttribute reverseAttribute = attr.getReverseAttribute(); + + if (reverseAttribute == null) { + throw new IllegalArgumentException("The reverse attribute can't be null, but was on " + attr); + } + + String result = getReverseDbName(reverseAttribute); + if (StringUtils.isEmpty(result)) { + + // Try to get it from the other site of the relation + ObjectModelAttribute reverseAttribute2 = reverseAttribute.getClassifier().getAttribute(attr.getName()); + result = getReverseDbName(reverseAttribute2); + + } + + if (StringUtils.isEmpty(result)) { + + result = GeneratorUtil.toLowerCaseFirstLetter(reverseAttribute.getName()); + + } + + return result; + + } + + /** * Cherche et renvoie la liste des attributs constituant la clef metier * d'une classe. * -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository topia. See http://git.nuiton.org/topia.git commit 694659d0182a0738758fb88a68c51a161656158f Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 23 19:18:17 2015 +0200 Fix hibernateType and attributeType tag value loading when specify on an attribute. --- .../org/nuiton/topia/templates/TopiaTagValues.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTagValues.java b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTagValues.java index ebc18d4..d1e195d 100644 --- a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTagValues.java +++ b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTagValues.java @@ -787,10 +787,14 @@ public class TopiaTagValues extends TagValueDefinitionProvider { */ public String getHibernateAttributeType(ObjectModelAttribute attribute, ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - String tagValueName = TAG_HIBERNATE_ATTRIBUTE_TYPE + "." + attribute.getType(); - String defaultValue = HIBERNATE_ATTRIBUTE_TYPE_DEFAULT_VALUES.get(attribute.getType()); - String value = TagValues.findTagValue(tagValueName, defaultValue, attribute, classifier, aPackage, model); + String value = TagValues.findDirectTagValue(TAG_HIBERNATE_ATTRIBUTE_TYPE, null, attribute); + if (value == null) { + String tagValueName = TAG_HIBERNATE_ATTRIBUTE_TYPE + "." + attribute.getType(); + String defaultValue = HIBERNATE_ATTRIBUTE_TYPE_DEFAULT_VALUES.get(attribute.getType()); + value = TagValues.findTagValue(tagValueName, defaultValue, classifier, aPackage, model); + } return value; + } /** @@ -803,9 +807,13 @@ public class TopiaTagValues extends TagValueDefinitionProvider { */ public String getAttributeType(ObjectModelAttribute attribute, ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - String tagValueName = TAG_ATTRIBUTE_TYPE + "." + attribute.getType(); - String value = TagValues.findTagValue(tagValueName, attribute.getType(), attribute, classifier, aPackage, model); + String value = TagValues.findDirectTagValue(TAG_HIBERNATE_ATTRIBUTE_TYPE, null, attribute); + if (value == null) { + String tagValueName = TAG_ATTRIBUTE_TYPE + "." + attribute.getType(); + value = TagValues.findTagValue(tagValueName, attribute.getType(), classifier, aPackage, model); + } return value; + } /** -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository topia. See http://git.nuiton.org/topia.git commit d6e7362270a41d327a1a872f212ebf21ace6f626 Merge: 644750d 694659d Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 23 19:24:26 2015 +0200 Consider schema everywhere (Closes #3726) Generate foreign key names (Closes #3722) Merge branch 'feature/schema_and_dbnames_customisation' into develop .../templates/EntityHibernateMappingGenerator.java | 233 ++++++++++++++------- .../org/nuiton/topia/templates/TopiaTagValues.java | 45 +++- .../topia/templates/TopiaTemplateHelper.java | 40 ++++ 3 files changed, 243 insertions(+), 75 deletions(-) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm