diff options
author | mtaal | 2011-02-21 06:39:57 +0000 |
---|---|---|
committer | mtaal | 2011-02-21 06:39:57 +0000 |
commit | b1365cfa82b08482eeef9ba2980505e6cd594fe0 (patch) | |
tree | e577d2b49ec343d8da11b35fdb1f9c25638382a3 | |
parent | 91e3ae211a3ba9fc9368f4df2fa43f9adef22bd2 (diff) | |
download | org.eclipse.emf.teneo-b1365cfa82b08482eeef9ba2980505e6cd594fe0.tar.gz org.eclipse.emf.teneo-b1365cfa82b08482eeef9ba2980505e6cd594fe0.tar.xz org.eclipse.emf.teneo-b1365cfa82b08482eeef9ba2980505e6cd594fe0.zip |
[337580]
7 files changed, 748 insertions, 385 deletions
diff --git a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/AbstractAssociationMapper.java b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/AbstractAssociationMapper.java index 8a9f2974e..685675f29 100644 --- a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/AbstractAssociationMapper.java +++ b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/AbstractAssociationMapper.java @@ -216,8 +216,11 @@ public abstract class AbstractAssociationMapper extends AbstractMapper { joinColumn.isUnique() ? "true" : "false"); if (joinColumn.getName() != null) { - columnElement.addAttribute("name", - getHbmContext().trunc(joinColumn.getName())); + columnElement + .addAttribute( + "name", + getHbmContext().trunc(joinColumn, + joinColumn.getName())); final String uc = getHbmContext().getUniqueConstraintKey( joinColumn.getName()); if (uc != null) { @@ -386,7 +389,7 @@ public abstract class AbstractAssociationMapper extends AbstractMapper { + aFeature.getModelEStructuralFeature().getName()); collElement.addElement("list-index").addAttribute("column", - getHbmContext().trunc(name)); + getHbmContext().trunc(aFeature.getListIndexColumn(), name)); } protected String getIndexColumnName(PAnnotatedEStructuralFeature aFeature) { @@ -485,8 +488,10 @@ public abstract class AbstractAssociationMapper extends AbstractMapper { final PAnnotatedEAttribute paAttribute = (PAnnotatedEAttribute) aref .getPaModel().getPAnnotated(keyFeature); final Element mapKeyElement = collElement.addElement("map-key") - .addAttribute("column", - getHbmContext().trunc(mapKey.getName())); + .addAttribute( + "column", + getHbmContext().trunc(hbRef.getMapKey(), + mapKey.getName())); setType(paAttribute, mapKeyElement); } else if (hbRef.getMapKeyManyToMany() != null) { final MapKeyManyToMany mkm = hbRef.getMapKeyManyToMany(); @@ -857,8 +862,10 @@ public abstract class AbstractAssociationMapper extends AbstractMapper { final Element ce = keyElement .addElement("column") - .addAttribute("name", - getHbmContext().trunc(joinColumn.getName())) + .addAttribute( + "name", + getHbmContext().trunc(joinColumn, + joinColumn.getName())) .addAttribute("not-null", joinColumn.isNullable() ? "false" : "true") .addAttribute("unique", @@ -938,7 +945,7 @@ public abstract class AbstractAssociationMapper extends AbstractMapper { } if (joinTable.getName() != null) { collElement.addAttribute("table", - getHbmContext().trunc(joinTable.getName())); + getHbmContext().trunc(joinTable, joinTable.getName())); } if (joinTable.getUniqueConstraints().size() > 0) { log.error("Unsupported unique constraints in " + joinTable); diff --git a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/AbstractMapper.java b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/AbstractMapper.java index b244a84c9..5d370494e 100644 --- a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/AbstractMapper.java +++ b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/AbstractMapper.java @@ -3,7 +3,7 @@ * reserved. This program and the accompanying materials are made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html Contributors: Martin Taal Brian - * Vetter </copyright> $Id: AbstractMapper.java,v 1.53 2010/02/04 11:03:48 mtaal Exp $ + * Vetter </copyright> $Id: AbstractMapper.java,v 1.54 2011/02/21 06:39:57 mtaal Exp $ */ package org.eclipse.emf.teneo.hibernate.mapper; @@ -69,7 +69,8 @@ public abstract class AbstractMapper { /** Returns the correct temporal type for hibernate */ private static String hbType(TemporalType temporalType) { - return TEMPORAL_TYPE_NAMES[temporalType != null ? temporalType.getValue() : TemporalType.TIMESTAMP.getValue()]; + return TEMPORAL_TYPE_NAMES[temporalType != null ? temporalType + .getValue() : TemporalType.TIMESTAMP.getValue()]; } /** logs it all */ @@ -81,7 +82,8 @@ public abstract class AbstractMapper { if (paReference.getModelEReference().getEOpposite() == null) { return null; } - return paReference.getPaModel().getPAnnotated(paReference.getModelEReference().getEOpposite()); + return paReference.getPaModel().getPAnnotated( + paReference.getModelEReference().getEOpposite()); } /** The mapping context of this mapping action */ @@ -109,7 +111,8 @@ public abstract class AbstractMapper { addAccessor(element, hbmContext.getPropertyHandlerName()); } - protected void setType(PAnnotatedEStructuralFeature paFeature, Element propElement) { + protected void setType(PAnnotatedEStructuralFeature paFeature, + Element propElement) { if (paFeature instanceof PAnnotatedEAttribute) { setType((PAnnotatedEAttribute) paFeature, propElement); } else { @@ -121,20 +124,31 @@ public abstract class AbstractMapper { protected void setType(PAnnotatedEReference paReference, Element propElement) { final External external = paReference.getExternal(); if (external == null) { - throw new MappingException("External annotation not set on eReference " - + StoreUtil.toString(paReference.getModelEReference())); + throw new MappingException( + "External annotation not set on eReference " + + StoreUtil.toString(paReference + .getModelEReference())); } final Element typeElement = propElement.addElement("type"); if (external.getType() == null) { // standard external - typeElement.addAttribute("name", getHbmContext().getExternalUserType()); + typeElement.addAttribute("name", getHbmContext() + .getExternalUserType()); } else { typeElement.addAttribute("name", external.getType()); } - typeElement.addElement("param").addAttribute("name", HbMapperConstants.ECLASS_NAME_META).addText( - paReference.getModelEReference().getEReferenceType().getName()); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.EPACKAGE_PARAM).addText( - paReference.getModelEReference().getEReferenceType().getEPackage().getNsURI()); + typeElement + .addElement("param") + .addAttribute("name", HbMapperConstants.ECLASS_NAME_META) + .addText( + paReference.getModelEReference().getEReferenceType() + .getName()); + typeElement + .addElement("param") + .addAttribute("name", HbMapperConstants.EPACKAGE_PARAM) + .addText( + paReference.getModelEReference().getEReferenceType() + .getEPackage().getNsURI()); } @@ -144,7 +158,8 @@ public abstract class AbstractMapper { // handle the type annotation final HbAnnotatedEAttribute hea = (HbAnnotatedEAttribute) paAttribute; final EDataType ed = (EDataType) hea.getModelEAttribute().getEType(); - final HbAnnotatedEDataType hed = (HbAnnotatedEDataType) hea.getPaModel().getPAnnotated(ed); + final HbAnnotatedEDataType hed = (HbAnnotatedEDataType) hea + .getPaModel().getPAnnotated(ed); final String name; final List<Parameter> params; @@ -166,71 +181,97 @@ public abstract class AbstractMapper { // simple propElement.addAttribute("type", name); } else { - final Element typeElement = propElement.addElement("type").addAttribute("name", name); + final Element typeElement = propElement.addElement("type") + .addAttribute("name", name); for (Parameter param : params) { - typeElement.addElement("param").addAttribute("name", param.getName()).addText(param.getValue()); + typeElement.addElement("param") + .addAttribute("name", param.getName()) + .addText(param.getValue()); } } } else if (paAttribute.getEnumerated() != null) { handleEnumType(paAttribute, propElement); } else if (StoreUtil.isQName(paAttribute.getModelEAttribute())) { - propElement.addAttribute("type", "org.eclipse.emf.teneo.hibernate.mapping.QNameUserType"); + propElement.addAttribute("type", + "org.eclipse.emf.teneo.hibernate.mapping.QNameUserType"); } else { final String hType = hbType(paAttribute); if (hType != null) { propElement.addAttribute("type", hType); } else { - final Element typeElement = propElement.addElement("type").addAttribute("name", - hbmContext.getDefaultUserType()); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.EDATATYPE_PARAM).addText( - paAttribute.getModelEAttribute().getEAttributeType().getName()); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.EPACKAGE_PARAM).addText( - paAttribute.getModelEAttribute().getEType().getEPackage().getNsURI()); + final Element typeElement = propElement.addElement("type") + .addAttribute("name", hbmContext.getDefaultUserType()); + typeElement + .addElement("param") + .addAttribute("name", HbMapperConstants.EDATATYPE_PARAM) + .addText( + paAttribute.getModelEAttribute() + .getEAttributeType().getName()); + typeElement + .addElement("param") + .addAttribute("name", HbMapperConstants.EPACKAGE_PARAM) + .addText( + paAttribute.getModelEAttribute().getEType() + .getEPackage().getNsURI()); } } } /** Handle the enum type */ - private void handleEnumType(PAnnotatedEAttribute paAttribute, Element propElement) { + private void handleEnumType(PAnnotatedEAttribute paAttribute, + Element propElement) { final Enumerated enumerated = paAttribute.getEnumerated(); assert (enumerated != null); final EAttribute eattr = paAttribute.getModelEAttribute(); final EClassifier eclassifier = eattr.getEType(); - if (!getHbmContext().isGeneratedByEMF() && getHbmContext().getInstanceClass(eclassifier) != null) { - final Class<?> instanceClass = getHbmContext().getInstanceClass(eclassifier); + if (!getHbmContext().isGeneratedByEMF() + && getHbmContext().getInstanceClass(eclassifier) != null) { + final Class<?> instanceClass = getHbmContext().getInstanceClass( + eclassifier); final Element typeElement = propElement.addElement("type") .addAttribute("name", getEnumUserType(enumerated)); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.ENUM_CLASS_PARAM).addText( - instanceClass.getName()); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.ECLASSIFIER_PARAM).addText( - eclassifier.getName()); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.EPACKAGE_PARAM).addText( - eclassifier.getEPackage().getNsURI()); - } else if (getHbmContext().isGeneratedByEMF() && eclassifier.getInstanceClass() != null) { + typeElement.addElement("param") + .addAttribute("name", HbMapperConstants.ENUM_CLASS_PARAM) + .addText(instanceClass.getName()); + typeElement.addElement("param") + .addAttribute("name", HbMapperConstants.ECLASSIFIER_PARAM) + .addText(eclassifier.getName()); + typeElement.addElement("param") + .addAttribute("name", HbMapperConstants.EPACKAGE_PARAM) + .addText(eclassifier.getEPackage().getNsURI()); + } else if (getHbmContext().isGeneratedByEMF() + && eclassifier.getInstanceClass() != null) { final Element typeElement = propElement.addElement("type") .addAttribute("name", getEnumUserType(enumerated)); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.ENUM_CLASS_PARAM).addText( - eclassifier.getInstanceClass().getName()); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.ECLASSIFIER_PARAM).addText( - eclassifier.getName()); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.EPACKAGE_PARAM).addText( - eclassifier.getEPackage().getNsURI()); + typeElement.addElement("param") + .addAttribute("name", HbMapperConstants.ENUM_CLASS_PARAM) + .addText(eclassifier.getInstanceClass().getName()); + typeElement.addElement("param") + .addAttribute("name", HbMapperConstants.ECLASSIFIER_PARAM) + .addText(eclassifier.getName()); + typeElement.addElement("param") + .addAttribute("name", HbMapperConstants.EPACKAGE_PARAM) + .addText(eclassifier.getEPackage().getNsURI()); } else { // must be emf dynamic - final Element typeElement = propElement.addElement("type").addAttribute("name", - hbDynamicEnumType(enumerated)); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.ECLASSIFIER_PARAM).addText( - eclassifier.getName()); - typeElement.addElement("param").addAttribute("name", HbMapperConstants.EPACKAGE_PARAM).addText( - eclassifier.getEPackage().getNsURI()); + final Element typeElement = propElement.addElement("type") + .addAttribute("name", hbDynamicEnumType(enumerated)); + typeElement.addElement("param") + .addAttribute("name", HbMapperConstants.ECLASSIFIER_PARAM) + .addText(eclassifier.getName()); + typeElement.addElement("param") + .addAttribute("name", HbMapperConstants.EPACKAGE_PARAM) + .addText(eclassifier.getEPackage().getNsURI()); } } // gather the pafeatures of the supertypes also - protected List<PAnnotatedEStructuralFeature> getAllFeatures(PAnnotatedEClass componentAClass) { + protected List<PAnnotatedEStructuralFeature> getAllFeatures( + PAnnotatedEClass componentAClass) { final ArrayList<PAnnotatedEStructuralFeature> result = new ArrayList<PAnnotatedEStructuralFeature>(); result.addAll(componentAClass.getPaEStructuralFeatures()); for (EClass eClass : componentAClass.getModelEClass().getESuperTypes()) { - final PAnnotatedEClass aSuperClass = componentAClass.getPaModel().getPAnnotated(eClass); + final PAnnotatedEClass aSuperClass = componentAClass.getPaModel() + .getPAnnotated(eClass); if (aSuperClass != null) { result.addAll(getAllFeatures(aSuperClass)); } @@ -245,9 +286,10 @@ public abstract class AbstractMapper { */ protected String hbType(PAnnotatedEAttribute paAttribute) { final EAttribute eAttribute = paAttribute.getModelEAttribute(); - final HbAnnotatedEDataType hed = (HbAnnotatedEDataType) paAttribute.getPaModel().getPAnnotated( - eAttribute.getEAttributeType()); - final EDataType eDataType = paAttribute.getModelEAttribute().getEAttributeType(); + final HbAnnotatedEDataType hed = (HbAnnotatedEDataType) paAttribute + .getPaModel().getPAnnotated(eAttribute.getEAttributeType()); + final EDataType eDataType = paAttribute.getModelEAttribute() + .getEAttributeType(); if (hed != null && hed.getHbTypeDef() != null) { return hed.getHbTypeDef().getName(); } else if (paAttribute.getLob() != null) { @@ -256,28 +298,35 @@ public abstract class AbstractMapper { } else if (EcoreDataTypes.INSTANCE.isEString(eDataType)) { return "text"; } else { - throw new MappingException("Lob annotations can only be used with Strings or byte arrays. " - + "Attribute is of type: " + eDataType); + throw new MappingException( + "Lob annotations can only be used with Strings or byte arrays. " + + "Attribute is of type: " + eDataType); } - } else if (EcoreDataTypes.INSTANCE.isEWrapper(eDataType) || EcoreDataTypes.INSTANCE.isEPrimitive(eDataType)) { + } else if (EcoreDataTypes.INSTANCE.isEWrapper(eDataType) + || EcoreDataTypes.INSTANCE.isEPrimitive(eDataType)) { return eDataType.getInstanceClassName(); } else if (EcoreDataTypes.INSTANCE.isEString(eDataType)) { return eDataType.getInstanceClassName(); } else if (EcoreDataTypes.INSTANCE.isEDateTime(eDataType) - || (paAttribute.getTemporal() != null && paAttribute.getTemporal().getValue().getValue() == TemporalType.TIMESTAMP_VALUE)) { + || (paAttribute.getTemporal() != null && paAttribute + .getTemporal().getValue().getValue() == TemporalType.TIMESTAMP_VALUE)) { return getEDateTimeClass(paAttribute); } else if (EcoreDataTypes.INSTANCE.isEDuration(eDataType)) { return hbmContext.getDurationType(); - } else if (EcoreDataTypes.INSTANCE.isEDate(eDataType, getHbmContext().getPersistenceOptions()) - || (paAttribute.getTemporal() != null && paAttribute.getTemporal().getValue().getValue() == TemporalType.DATE_VALUE)) { + } else if (EcoreDataTypes.INSTANCE.isEDate(eDataType, getHbmContext() + .getPersistenceOptions()) + || (paAttribute.getTemporal() != null && paAttribute + .getTemporal().getValue().getValue() == TemporalType.DATE_VALUE)) { return getEDateClass(paAttribute); - } else if (eDataType.getInstanceClass() != null && eDataType.getInstanceClass() == Object.class) { + } else if (eDataType.getInstanceClass() != null + && eDataType.getInstanceClass() == Object.class) { // null forces caller to use usertype return null; // "org.eclipse.emf.teneo.hibernate.mapping.DefaultToStringUserType"; // } else if (eDataType.getInstanceClass() != null) { // return eDataType.getInstanceClassName(); } else { - final String result = EcoreDataTypes.INSTANCE.getTargetTypeName(paAttribute); + final String result = EcoreDataTypes.INSTANCE + .getTargetTypeName(paAttribute); if (result.compareTo(Object.class.getName()) == 0) { // all edatatypes are translatable to a string, done by caller return null; // "org.eclipse.emf.teneo.hibernate.mapping.DefaultToStringUserType"; @@ -290,12 +339,19 @@ public abstract class AbstractMapper { protected String getTargetTypeName(PAnnotatedEAttribute aAttribute) { final EAttribute eAttribute = aAttribute.getModelEAttribute(); // check on equality on object.class is used for listunion simpleunions - final Class<?> instanceClass = eAttribute.getEAttributeType().getInstanceClass(); - if (instanceClass != null && !Object.class.equals(instanceClass) && !List.class.equals(instanceClass)) { + final Class<?> instanceClass = eAttribute.getEAttributeType() + .getInstanceClass(); + if (instanceClass != null && !Object.class.equals(instanceClass) + && !List.class.equals(instanceClass)) { if (instanceClass.isArray()) { // get rid of the [] at the end - return eAttribute.getEType().getInstanceClassName().substring(0, - eAttribute.getEType().getInstanceClassName().length() - 2); + return eAttribute + .getEType() + .getInstanceClassName() + .substring( + 0, + eAttribute.getEType().getInstanceClassName() + .length() - 2); } return instanceClass.getName(); } else { @@ -308,8 +364,8 @@ public abstract class AbstractMapper { // of target // entities this is required for listunion types but is not // according to the ejb3 spec! - ArrayList<EClassifier> eclassifiers = EcoreDataTypes.INSTANCE.getItemTypes((EDataType) eAttribute - .getEType()); + ArrayList<EClassifier> eclassifiers = EcoreDataTypes.INSTANCE + .getItemTypes((EDataType) eAttribute.getEType()); if (eclassifiers.size() > 0) { StringBuffer result = new StringBuffer(); for (int i = 0; i < eclassifiers.size(); i++) { @@ -327,7 +383,8 @@ public abstract class AbstractMapper { } /** - * Returns the correct enum primitive hibernate type, for Elver this is a hibernate user type. + * Returns the correct enum primitive hibernate type, for Elver this is a + * hibernate user type. */ public String getEnumUserType(Enumerated enumerated) { if (EnumType.STRING == enumerated.getValue()) { @@ -338,7 +395,8 @@ public abstract class AbstractMapper { } /** - * Returns the correct enum primitive hibernate type, for Elver this is a hibernate user type. + * Returns the correct enum primitive hibernate type, for Elver this is a + * hibernate user type. */ protected String hbDynamicEnumType(Enumerated enumerated) { if (EnumType.STRING == enumerated.getValue()) { @@ -352,9 +410,11 @@ public abstract class AbstractMapper { * @return The name of the java class needed to map the date type */ public String getEDateClass(PAnnotatedEAttribute paAttribute) { - final EDataType eDataType = paAttribute.getModelEAttribute().getEAttributeType(); + final EDataType eDataType = paAttribute.getModelEAttribute() + .getEAttributeType(); - assert (EcoreDataTypes.INSTANCE.isEDate(eDataType, getHbmContext().getPersistenceOptions())); + assert (EcoreDataTypes.INSTANCE.isEDate(eDataType, getHbmContext() + .getPersistenceOptions())); if (XMLTypePackage.eINSTANCE.getDate().equals(eDataType)) { return getHbmContext().getXSDDateUserType(); @@ -366,7 +426,8 @@ public abstract class AbstractMapper { } // only override if the user did not specify a more specific class - if (EcoreDataTypes.INSTANCE.isEDate(eDataType, getHbmContext().getPersistenceOptions())) { + if (EcoreDataTypes.INSTANCE.isEDate(eDataType, getHbmContext() + .getPersistenceOptions())) { // EMF returns an XSD Date type as an Object instance. go figure. // note that I would prefer to use the class instance to get the // name @@ -382,10 +443,12 @@ public abstract class AbstractMapper { } /* - * @return The name of the java class needed to map the datetime/timestamp type + * @return The name of the java class needed to map the datetime/timestamp + * type */ public String getEDateTimeClass(PAnnotatedEAttribute paAttribute) { - final EDataType eDataType = paAttribute.getModelEAttribute().getEAttributeType(); + final EDataType eDataType = paAttribute.getModelEAttribute() + .getEAttributeType(); assert (EcoreDataTypes.INSTANCE.isEDateTime(eDataType)); @@ -399,7 +462,8 @@ public abstract class AbstractMapper { } // bugzilla 277546 - if (eDataType.getInstanceClass() != null && Date.class.isAssignableFrom(eDataType.getInstanceClass())) { + if (eDataType.getInstanceClass() != null + && Date.class.isAssignableFrom(eDataType.getInstanceClass())) { return eDataType.getInstanceClass().getName(); } @@ -419,11 +483,12 @@ public abstract class AbstractMapper { } /** - * Returns the (possibly overridden) JoinColumns annotations for the given reference or an empty list if no - * JoinColumns were defined. + * Returns the (possibly overridden) JoinColumns annotations for the given + * reference or an empty list if no JoinColumns were defined. */ protected List<JoinColumn> getJoinColumns(PAnnotatedEReference paReference) { - List<JoinColumn> joinColumns = getHbmContext().getAssociationOverrides(paReference); + List<JoinColumn> joinColumns = getHbmContext().getAssociationOverrides( + paReference); if (joinColumns == null) { joinColumns = paReference.getJoinColumns(); } @@ -436,9 +501,11 @@ public abstract class AbstractMapper { /** Adds a cache element */ protected void addCacheElement(Element parent, Cache cache) { // translate to hibernate specific notation - final String usage = cache.getUsage().getName().toLowerCase().replaceAll("_", "-"); + final String usage = cache.getUsage().getName().toLowerCase() + .replaceAll("_", "-"); - Element cacheElement = parent.addElement("cache").addAttribute("usage", usage); + Element cacheElement = parent.addElement("cache").addAttribute("usage", + usage); if (cache.getRegion() != null) { cacheElement.addAttribute("region", cache.getRegion()); } @@ -450,15 +517,18 @@ public abstract class AbstractMapper { } /** Same as above only handles multiple columns */ - protected void addColumnsAndFormula(Element propertyElement, PAnnotatedEStructuralFeature pef, - List<Column> columns, boolean isNullable, boolean setColumnAttributesInProperty) { - addColumnsAndFormula(propertyElement, pef, columns, isNullable, setColumnAttributesInProperty, false, false); + protected void addColumnsAndFormula(Element propertyElement, + PAnnotatedEStructuralFeature pef, List<Column> columns, + boolean isNullable, boolean setColumnAttributesInProperty) { + addColumnsAndFormula(propertyElement, pef, columns, isNullable, + setColumnAttributesInProperty, false, false); } /** Same as above only handles multiple columns */ - protected void addColumnsAndFormula(Element propertyElement, PAnnotatedEStructuralFeature pef, - List<Column> columns, boolean isNullable, boolean setColumnAttributesInProperty, boolean isUnique, - boolean isIdProperty) { + protected void addColumnsAndFormula(Element propertyElement, + PAnnotatedEStructuralFeature pef, List<Column> columns, + boolean isNullable, boolean setColumnAttributesInProperty, + boolean isUnique, boolean isIdProperty) { // if no columns set then use some default final HbAnnotatedETypeElement hbFeature = (HbAnnotatedETypeElement) pef; if (columns.isEmpty() && hbFeature.getFormula() == null) { @@ -471,7 +541,8 @@ public abstract class AbstractMapper { col.setUnique(isUnique); } - if (pef instanceof HbAnnotatedEAttribute && ((HbAnnotatedEAttribute) pef).getGenerated() != null + if (pef instanceof HbAnnotatedEAttribute + && ((HbAnnotatedEAttribute) pef).getGenerated() != null && ((HbAnnotatedEAttribute) pef).getGenerated().getValue() != null && ((HbAnnotatedEAttribute) pef).getGenerated().getValue() != GenerationTime.NEVER) { col.setInsertable(false); @@ -481,46 +552,55 @@ public abstract class AbstractMapper { columns.add(col); } // if only one id column then it is not nullable! - if (isIdProperty && columns.size() == 1 && pef.getPaEClass().getIdClass() == null) { + if (isIdProperty && columns.size() == 1 + && pef.getPaEClass().getIdClass() == null) { columns.get(0).setNullable(false); // set to false, see bugzilla 280169 columns.get(0).setUnique(false); } for (Column column : columns) { - addColumn(propertyElement, pef, column, isNullable, setColumnAttributesInProperty); + addColumn(propertyElement, pef, column, isNullable, + setColumnAttributesInProperty); } // do the formula part if (hbFeature.getFormula() != null) { - propertyElement.addElement("formula").addText(hbFeature.getFormula().getValue()); + propertyElement.addElement("formula").addText( + hbFeature.getFormula().getValue()); } } protected String getColumnName(PAnnotatedEStructuralFeature pef) { if (getHbmContext().getEmbeddingFeature() != null) { // embedded // TODO: check illegal, embedded component can not really have an id - final PAnnotatedEStructuralFeature embeddingFeature = getHbmContext().getEmbeddingFeature(); + final PAnnotatedEStructuralFeature embeddingFeature = getHbmContext() + .getEmbeddingFeature(); return getHbmContext().getSqlNameStrategy().getColumnName(pef, embeddingFeature.getModelEStructuralFeature().getName()); } else { - return getHbmContext().getSqlNameStrategy().getColumnName(pef, null); + return getHbmContext().getSqlNameStrategy() + .getColumnName(pef, null); } } /** - * Add a comment element, if the eModelElement has documentation, returns the comment element + * Add a comment element, if the eModelElement has documentation, returns + * the comment element */ - protected Element addCommentElement(EModelElement eModelElement, Element hbmElement) { + protected Element addCommentElement(EModelElement eModelElement, + Element hbmElement) { if (hbmContext.getMaximumCommentLength() == 0) { return null; } final String commentData = EcoreUtil.getDocumentation(eModelElement); if (commentData != null) { final Element comment = hbmElement.addElement("comment"); - String commentText = commentData.replace('\'', ' ').replace('"', ' '); + String commentText = commentData.replace('\'', ' ').replace('"', + ' '); if (commentText.length() > hbmContext.getMaximumCommentLength()) { - commentText = commentText.substring(0, hbmContext.getMaximumCommentLength()); + commentText = commentText.substring(0, + hbmContext.getMaximumCommentLength()); } comment.addText(commentText); return comment; @@ -529,21 +609,23 @@ public abstract class AbstractMapper { } /** Adds anytype columns */ - protected List<Column> getAnyTypeColumns(String featureName, boolean isNullable) { + protected List<Column> getAnyTypeColumns(String featureName, + boolean isNullable) { final ArrayList<Column> result = new ArrayList<Column>(); final Column typeColumn = PannotationFactory.eINSTANCE.createColumn(); - typeColumn.setName(hbmContext.trunc(featureName + "_type")); + typeColumn.setName(hbmContext.trunc(null, featureName + "_type")); typeColumn.setNullable(isNullable); result.add(typeColumn); final Column idColumn = PannotationFactory.eINSTANCE.createColumn(); - idColumn.setName(hbmContext.trunc(featureName + "_id")); + idColumn.setName(hbmContext.trunc(null, featureName + "_id")); idColumn.setNullable(isNullable); result.add(idColumn); return result; } /** - * Returns the (possibly overridden) columns annotation for the given attribute. + * Returns the (possibly overridden) columns annotation for the given + * attribute. */ protected List<Column> getColumns(PAnnotatedEStructuralFeature paFeature) { final Column defaultColumn = paFeature.getColumn(); @@ -567,15 +649,18 @@ public abstract class AbstractMapper { } /** Sets property attributes on the basis of the column */ - private void addColumn(Element propertyElement, PAnnotatedEStructuralFeature pef, Column column, + private void addColumn(Element propertyElement, + PAnnotatedEStructuralFeature pef, Column column, boolean isNullable, boolean setColumnAttributesInProperty) { if (column != null) { if (setColumnAttributesInProperty) { // this is not the nicest place to do this if (propertyElement.getName().compareTo("property") == 0 || propertyElement.getName().compareTo("many-to-one") == 0) { - propertyElement.addAttribute("insert", column.isInsertable() ? "true" : "false"); - propertyElement.addAttribute("update", column.isUpdatable() ? "true" : "false"); + propertyElement.addAttribute("insert", + column.isInsertable() ? "true" : "false"); + propertyElement.addAttribute("update", + column.isUpdatable() ? "true" : "false"); } // MT: I think that the column nullability should not be used // for setting not-null @@ -593,9 +678,11 @@ public abstract class AbstractMapper { // can be used in case of // single table inheritance mapping if (!propertyElement.getName().equals("any")) { - propertyElement.addAttribute("not-null", isNullable || column.isNullable() ? "false" : "true"); + propertyElement.addAttribute("not-null", isNullable + || column.isNullable() ? "false" : "true"); } - propertyElement.addAttribute("unique", column.isUnique() ? "true" : "false"); + propertyElement.addAttribute("unique", + column.isUnique() ? "true" : "false"); } addColumnElement(propertyElement, pef, column, isNullable); } @@ -612,7 +699,8 @@ public abstract class AbstractMapper { } /** - * Creates cascades for onetoone/manytoone, they differ from many relations because no delete-orphan is supported. + * Creates cascades for onetoone/manytoone, they differ from many relations + * because no delete-orphan is supported. * * @param associationElement * : the element to which the cascades are added. @@ -621,7 +709,8 @@ public abstract class AbstractMapper { * @param addDeleteOrphan * : if true then delete-orphan is added in case of cascade all */ - protected void addCascades(Element associationElement, List<HbCascadeType> cascades, boolean addDeleteOrphan) { + protected void addCascades(Element associationElement, + List<HbCascadeType> cascades, boolean addDeleteOrphan) { if (!cascades.isEmpty()) { StringBuffer sb = new StringBuffer(); for (HbCascadeType cascade : cascades) { @@ -653,18 +742,21 @@ public abstract class AbstractMapper { } else if (cascade == HbCascadeType.SAVE_UPDATE) { sb.append("save-update,"); } else { - throw new MappingException("Cascade " + cascade.getName() + " not supported"); + throw new MappingException("Cascade " + cascade.getName() + + " not supported"); } } - associationElement.addAttribute("cascade", sb.substring(0, sb.length() - 1)); + associationElement.addAttribute("cascade", + sb.substring(0, sb.length() - 1)); } } /** * Create an any Element */ - public Element createAny(String name, PAnnotatedEStructuralFeature paFeature, Any any, AnyMetaDef anyMetaDef, - boolean isMany) { + public Element createAny(String name, + PAnnotatedEStructuralFeature paFeature, Any any, + AnyMetaDef anyMetaDef, boolean isMany) { final AnyMetaDef localAnyMetaDef; if (anyMetaDef == null) { @@ -688,8 +780,10 @@ public abstract class AbstractMapper { } // don't know how to create an element, so add an remove it... - final Element anyElement = getHbmContext().getCurrent().addElement(tagName).addAttribute("id-type", - localAnyMetaDef.getIdType()).addAttribute("meta-type", localAnyMetaDef.getMetaType()); + final Element anyElement = getHbmContext().getCurrent() + .addElement(tagName) + .addAttribute("id-type", localAnyMetaDef.getIdType()) + .addAttribute("meta-type", localAnyMetaDef.getMetaType()); getHbmContext().getCurrent().remove(anyElement); if (!isMany) { @@ -699,7 +793,8 @@ public abstract class AbstractMapper { if (((HbAnnotatedEReference) paFeature).getHbCascade() == null) { cascades.add(HbCascadeType.ALL); } else { - cascades.addAll(((HbAnnotatedEReference) paFeature).getHbCascade().getValue()); + cascades.addAll(((HbAnnotatedEReference) paFeature) + .getHbCascade().getValue()); } } else { cascades.add(HbCascadeType.ALL); @@ -711,10 +806,13 @@ public abstract class AbstractMapper { if (localAny.getMetaColumn() != null) { columns.add(localAny.getMetaColumn()); } else { - final Column typeColumn = PannotationFactory.eINSTANCE.createColumn(); + final Column typeColumn = PannotationFactory.eINSTANCE + .createColumn(); // bugzilla 294201, trunc is also done later in addColumn - // typeColumn.setName(hbmContext.trunc(paFeature.getModelEStructuralFeature().getName() + "_type")); - typeColumn.setName(paFeature.getModelEStructuralFeature().getName() + "_type"); + // typeColumn.setName(hbmContext.trunc(paFeature.getModelEStructuralFeature().getName() + // + "_type")); + typeColumn.setName(paFeature.getModelEStructuralFeature().getName() + + "_type"); typeColumn.setNullable(localAny.isOptional()); columns.add(typeColumn); } @@ -724,25 +822,34 @@ public abstract class AbstractMapper { } else { final Column idColumn = PannotationFactory.eINSTANCE.createColumn(); // bugzilla 294201, trunc is also done later in addColumn - // idColumn.setName(hbmContext.trunc(paFeature.getModelEStructuralFeature().getName() + "_id")); - idColumn.setName(paFeature.getModelEStructuralFeature().getName() + "_id"); + // idColumn.setName(hbmContext.trunc(paFeature.getModelEStructuralFeature().getName() + // + "_id")); + idColumn.setName(paFeature.getModelEStructuralFeature().getName() + + "_id"); idColumn.setNullable(localAny.isOptional()); columns.add(idColumn); } - addColumnsAndFormula(anyElement, paFeature, columns, paFeature.getModelEStructuralFeature().isRequired(), false); + addColumnsAndFormula(anyElement, paFeature, columns, paFeature + .getModelEStructuralFeature().isRequired(), false); return anyElement; } /** - * Add a columnelement to the property, takes into account length, precision etc. forceNullable is set when the - * feature belongs to a featuremap + * Add a columnelement to the property, takes into account length, precision + * etc. forceNullable is set when the feature belongs to a featuremap */ - private void addColumnElement(Element propertyElement, PAnnotatedEStructuralFeature pef, Column column, + private void addColumnElement(Element propertyElement, + PAnnotatedEStructuralFeature pef, Column column, boolean forceNullable) { if (column != null) { - Element columnElement = propertyElement.addElement("column").addAttribute("not-null", - column.isNullable() || forceNullable ? "false" : "true").addAttribute("unique", - column.isUnique() ? "true" : "false"); + Element columnElement = propertyElement + .addElement("column") + .addAttribute( + "not-null", + column.isNullable() || forceNullable ? "false" + : "true") + .addAttribute("unique", + column.isUnique() ? "true" : "false"); final String name; if (column.getName() != null) { name = column.getName(); @@ -750,35 +857,46 @@ public abstract class AbstractMapper { if (getHbmContext().getEmbeddingFeature() != null) { // embedded // TODO: check illegal, embedded component can not really // have an id - final PAnnotatedEStructuralFeature embeddingFeature = getHbmContext().getEmbeddingFeature(); - name = getHbmContext().getSqlNameStrategy().getColumnName(pef, - embeddingFeature.getModelEStructuralFeature().getName()); + final PAnnotatedEStructuralFeature embeddingFeature = getHbmContext() + .getEmbeddingFeature(); + name = getHbmContext().getSqlNameStrategy().getColumnName( + pef, + embeddingFeature.getModelEStructuralFeature() + .getName()); } else { - name = getHbmContext().getSqlNameStrategy().getColumnName(pef, null); + name = getHbmContext().getSqlNameStrategy().getColumnName( + pef, null); } } if (column.getUniqueKey() != null) { columnElement.addAttribute("unique-key", column.getUniqueKey()); } - columnElement.addAttribute("name", getHbmContext().trunc(name)); + columnElement.addAttribute("name", + getHbmContext().trunc(column, name)); if (column.isSetLength()) { - columnElement.addAttribute("length", Integer.toString(column.getLength())); + columnElement.addAttribute("length", + Integer.toString(column.getLength())); } if (column.isSetPrecision()) { - columnElement.addAttribute("precision", Integer.toString(column.getPrecision())); + columnElement.addAttribute("precision", + Integer.toString(column.getPrecision())); } if (column.isSetScale()) { - columnElement.addAttribute("scale", Integer.toString(column.getScale())); + columnElement.addAttribute("scale", + Integer.toString(column.getScale())); } if (column.getColumnDefinition() != null) { - columnElement.addAttribute("sql-type", column.getColumnDefinition()); + columnElement.addAttribute("sql-type", + column.getColumnDefinition()); } final String uc = getHbmContext().getUniqueConstraintKey(name); if (uc != null) { columnElement.addAttribute("unique-key", uc); } - if (pef instanceof HbAnnotatedETypeElement && ((HbAnnotatedETypeElement) pef).getHbIndex() != null) { - final Index index = ((HbAnnotatedETypeElement) pef).getHbIndex(); + if (pef instanceof HbAnnotatedETypeElement + && ((HbAnnotatedETypeElement) pef).getHbIndex() != null) { + final Index index = ((HbAnnotatedETypeElement) pef) + .getHbIndex(); columnElement.addAttribute("index", index.getName()); } @@ -790,15 +908,15 @@ public abstract class AbstractMapper { /** Add the ondelete mapping to the key */ protected void handleOndelete(Element keyElement, OnDelete onDelete) { -// if (true || onDelete == null) { -// return; -// } -// -// if (onDelete.getAction().equals(OnDeleteAction.CASCADE)) { -// keyElement.addAttribute("on-delete", "cascade"); -// } else { -// keyElement.addAttribute("on-delete", "noaction"); -// } + // if (true || onDelete == null) { + // return; + // } + // + // if (onDelete.getAction().equals(OnDeleteAction.CASCADE)) { + // keyElement.addAttribute("on-delete", "cascade"); + // } else { + // keyElement.addAttribute("on-delete", "noaction"); + // } } /** Returns true if the target is the general EObject type */ diff --git a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/EntityMapper.java b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/EntityMapper.java index 37a45b3b7..1180fc073 100644 --- a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/EntityMapper.java +++ b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/EntityMapper.java @@ -3,7 +3,7 @@ * reserved. This program and the accompanying materials are made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html Contributors: Martin Taal - * </copyright> $Id: EntityMapper.java,v 1.53 2010/08/18 12:21:13 mtaal Exp $ + * </copyright> $Id: EntityMapper.java,v 1.54 2011/02/21 06:39:57 mtaal Exp $ */ package org.eclipse.emf.teneo.hibernate.mapper; @@ -41,7 +41,8 @@ import org.eclipse.emf.teneo.simpledom.DocumentHelper; import org.eclipse.emf.teneo.simpledom.Element; /** - * Maps the entity and its features, also takes care of embedded superclass mapping. + * Maps the entity and its features, also takes care of embedded superclass + * mapping. * * @author <a href="mailto:mtaal at elver.org">Martin Taal</a> */ @@ -72,33 +73,37 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { private final String PRIMARY_TABLE_KEY = null; /** - * @return Return the name used by hibernate to denote the given inheritance type. If the argument is null, the - * default inheritance type (SINGLE_TABLE) is used instead. + * @return Return the name used by hibernate to denote the given inheritance + * type. If the argument is null, the default inheritance type + * (SINGLE_TABLE) is used instead. */ private static String hbInheritanceName(InheritanceType inheritanceType) { - return EntityMapper.INHERITANCE_STRATEGY_NAMES[inheritanceType != null ? inheritanceType.getValue() - : InheritanceType.SINGLE_TABLE.getValue()]; + return EntityMapper.INHERITANCE_STRATEGY_NAMES[inheritanceType != null ? inheritanceType + .getValue() : InheritanceType.SINGLE_TABLE.getValue()]; } /** * @return Return the name used by hibernate for the given dicriminator type */ private static String hbDiscriminatorType(DiscriminatorType dType) { - return EntityMapper.DISCRIMINATOR_TYPE_NAMES[dType == null ? DiscriminatorType.STRING.getValue() : dType - .getValue()]; + return EntityMapper.DISCRIMINATOR_TYPE_NAMES[dType == null ? DiscriminatorType.STRING + .getValue() : dType.getValue()]; } /** * @return Returns a new hibernate element for the given entity. */ - private Element createEntity(PAnnotatedEClass entity, InheritanceType inhStrategy, PAnnotatedEClass superEntity, + private Element createEntity(PAnnotatedEClass entity, + InheritanceType inhStrategy, PAnnotatedEClass superEntity, DiscriminatorValue dValue, Table table) { // determine what type of hibernate tag should be used - final InheritanceType inheritanceStrategy = inhStrategy != null ? inhStrategy : InheritanceType.SINGLE_TABLE; + final InheritanceType inheritanceStrategy = inhStrategy != null ? inhStrategy + : InheritanceType.SINGLE_TABLE; final EClass eclass = entity.getModelEClass(); final String hbClassName; if (entity.getEavMapping() != null) { - hbClassName = EntityMapper.hbInheritanceName(InheritanceType.SINGLE_TABLE); + hbClassName = EntityMapper + .hbInheritanceName(InheritanceType.SINGLE_TABLE); } else if (superEntity == null) { hbClassName = "class"; } else { @@ -114,8 +119,10 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { final boolean doProxy = ((HbAnnotatedEClass) entity).getHbProxy() != null && ((HbAnnotatedEClass) entity).getHbProxy().isLazy(); if (entity.isOnlyMapAsEntity()) { - target = getHbmContext().getCurrent().addElement(hbClassName).addAttribute("entity-name", entityName) - .addAttribute("abstract", isAbstractStr).addAttribute("lazy", "false"); + target = getHbmContext().getCurrent().addElement(hbClassName) + .addAttribute("entity-name", entityName) + .addAttribute("abstract", isAbstractStr) + .addAttribute("lazy", "false"); // note for composite ids the name must be set always! // entity.getAnnotatedEClass().getInstanceClass() != null) { // @@ -124,29 +131,45 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { if (hasCompositeID(entity)) { // only for this specific case // it is // required to have the impl.name - target.addAttribute("name", hbmContext.getInstanceClassName(entity.getModelEClass())); + target.addAttribute("name", hbmContext + .getInstanceClassName(entity.getModelEClass())); } } else if (getHbmContext().forceUseOfInstance(entity)) { final Element importElement = new Element("import"); - importElement.addAttribute("class", hbmContext.getInstanceClassName(entity.getModelEClass())).addAttribute( - "rename", entityName); + importElement.addAttribute("class", + hbmContext.getInstanceClassName(entity.getModelEClass())) + .addAttribute("rename", entityName); getHbmContext().getCurrent().add(getImportIndex(), importElement); - target = getHbmContext().getCurrent().addElement(hbClassName).addAttribute("name", - hbmContext.getInstanceClassName(entity.getModelEClass())).addAttribute("abstract", isAbstractStr) + target = getHbmContext() + .getCurrent() + .addElement(hbClassName) + .addAttribute( + "name", + hbmContext.getInstanceClassName(entity + .getModelEClass())) + .addAttribute("abstract", isAbstractStr) .addAttribute("lazy", doProxy ? "true" : "false"); } else { // don't specify the name as it is a dynamic eobject if (getHbmContext().getInstanceClass(entity.getModelEClass()) == null) { - target = getHbmContext().getCurrent().addElement(hbClassName).addAttribute("entity-name", entityName) - .addAttribute("abstract", isAbstractStr).addAttribute("lazy", doProxy ? "true" : "false"); + target = getHbmContext().getCurrent().addElement(hbClassName) + .addAttribute("entity-name", entityName) + .addAttribute("abstract", isAbstractStr) + .addAttribute("lazy", doProxy ? "true" : "false"); } else { - target = getHbmContext().getCurrent().addElement(hbClassName).addAttribute("name", - hbmContext.getInstanceClassName(entity.getModelEClass())).addAttribute("entity-name", - entityName).addAttribute("abstract", isAbstractStr).addAttribute("lazy", - doProxy ? "true" : "false"); + target = getHbmContext() + .getCurrent() + .addElement(hbClassName) + .addAttribute( + "name", + hbmContext.getInstanceClassName(entity + .getModelEClass())) + .addAttribute("entity-name", entityName) + .addAttribute("abstract", isAbstractStr) + .addAttribute("lazy", doProxy ? "true" : "false"); } } @@ -158,20 +181,24 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } if (hbEntity.getBatchSize() != null) { - target.addAttribute("batch-size", "" + hbEntity.getBatchSize().getSize()); + target.addAttribute("batch-size", "" + + hbEntity.getBatchSize().getSize()); } if (hbEntity.getHbEntity() != null) { final HbEntity hbEntityAnnon = hbEntity.getHbEntity(); if (superEntity == null) { - if (!hbEntityAnnon.isMutable() && target.getAttributeValue("mutable") == null) { + if (!hbEntityAnnon.isMutable() + && target.getAttributeValue("mutable") == null) { target.addAttribute("mutable", "false"); } if (hbEntityAnnon.getPolymorphism() != null) { - target.addAttribute("polymorphism", hbEntityAnnon.getPolymorphism().getName().toLowerCase()); + target.addAttribute("polymorphism", hbEntityAnnon + .getPolymorphism().getName().toLowerCase()); } if (hbEntityAnnon.getOptimisticLock() != null) { - target.addAttribute("optimistic-lock", hbEntityAnnon.getOptimisticLock().getName().toLowerCase()); + target.addAttribute("optimistic-lock", hbEntityAnnon + .getOptimisticLock().getName().toLowerCase()); } } if (hbEntityAnnon.isDynamicInsert()) { @@ -191,10 +218,13 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { if (superEntity != null) { final String extendsEntity; - if (superEntity.isOnlyMapAsEntity() || !getHbmContext().forceUseOfInstance(superEntity)) { - extendsEntity = getHbmContext().getEntityName(superEntity.getModelEClass()); + if (superEntity.isOnlyMapAsEntity() + || !getHbmContext().forceUseOfInstance(superEntity)) { + extendsEntity = getHbmContext().getEntityName( + superEntity.getModelEClass()); } else { - extendsEntity = getHbmContext().getInstanceClassName(superEntity.getModelEClass()); + extendsEntity = getHbmContext().getInstanceClassName( + superEntity.getModelEClass()); } target.addAttribute("extends", extendsEntity); log.debug("Extends " + extendsEntity); @@ -202,54 +232,67 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { if (entity.getEavMapping() != null) { if (superEntity == null) { - target.addAttribute("extends", Constants.EAV_EOBJECT_ENTITY_NAME); + target.addAttribute("extends", + Constants.EAV_EOBJECT_ENTITY_NAME); } target.addAttribute("discriminator-value", dValue.getValue()); if (((HbAnnotatedEClass) entity).getHbProxy() != null - && ((HbAnnotatedEClass) entity).getHbProxy().getProxyClass() != null) { - final String proxyInterfaceName = ((HbAnnotatedEClass) entity).getHbProxy().getProxyClass(); + && ((HbAnnotatedEClass) entity).getHbProxy() + .getProxyClass() != null) { + final String proxyInterfaceName = ((HbAnnotatedEClass) entity) + .getHbProxy().getProxyClass(); target.addAttribute("proxy", proxyInterfaceName); } // stop here for eav return target; } - if (dValue != null && !target.getName().equals(INHERITANCE_STRATEGY_NAMES[InheritanceType.JOINED.getValue()]) - && !target.getName().equals(InheritanceType.TABLE_PER_CLASS.getValue())) { + if (dValue != null + && !target.getName().equals( + INHERITANCE_STRATEGY_NAMES[InheritanceType.JOINED + .getValue()]) + && !target.getName().equals( + InheritanceType.TABLE_PER_CLASS.getValue())) { target.addAttribute("discriminator-value", dValue.getValue()); log.debug("DValue " + dValue.getValue()); } if (table != null - && hbClassName.compareTo(INHERITANCE_STRATEGY_NAMES[InheritanceType.SINGLE_TABLE.getValue()]) == 0) { - log - .warn("EClass/Entity (" - + entityName - + ") is mapped as subclass in a single table with its superclass but it also has a table annotation. This table annotation is ignored."); + && hbClassName + .compareTo(INHERITANCE_STRATEGY_NAMES[InheritanceType.SINGLE_TABLE + .getValue()]) == 0) { + log.warn("EClass/Entity (" + + entityName + + ") is mapped as subclass in a single table with its superclass but it also has a table annotation. This table annotation is ignored."); } else if (table != null) { if (table.getName() != null) { - target.addAttribute("table", getHbmContext().trunc(table.getName(), false)); + target.addAttribute("table", + getHbmContext().trunc(table, table.getName(), false)); log.debug("Table " + table.getName()); } if (table.getSchema() != null) { - target.addAttribute("schema", getHbmContext().trunc(table.getSchema(), false)); + target.addAttribute("schema", + getHbmContext().trunc(table.getSchema(), false)); log.debug("Schema " + table.getSchema()); } if (table.getCatalog() != null) { - target.addAttribute("catalog", getHbmContext().trunc(table.getCatalog(), false)); + target.addAttribute("catalog", + getHbmContext().trunc(table.getCatalog(), false)); log.debug("Catalog " + table.getCatalog()); } } if (((HbAnnotatedEClass) entity).getHbWhere() != null) { - target.addAttribute("where", ((HbAnnotatedEClass) entity).getHbWhere().getClause()); + target.addAttribute("where", ((HbAnnotatedEClass) entity) + .getHbWhere().getClause()); } if (((HbAnnotatedEClass) entity).getHbProxy() != null && ((HbAnnotatedEClass) entity).getHbProxy().getProxyClass() != null) { - final String proxyInterfaceName = ((HbAnnotatedEClass) entity).getHbProxy().getProxyClass(); + final String proxyInterfaceName = ((HbAnnotatedEClass) entity) + .getHbProxy().getProxyClass(); target.addAttribute("proxy", proxyInterfaceName); } @@ -264,7 +307,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { final Element e = (Element) children.get(i); final String name = e.getName(); if (name.compareTo("class") == 0 || name.compareTo("subclass") == 0 - || name.compareTo("joined-subclass") == 0 || name.compareTo("union-subclass") == 0) { + || name.compareTo("joined-subclass") == 0 + || name.compareTo("union-subclass") == 0) { return i; } if (name.compareTo("typedef") == 0 || name.compareTo("meta") == 0) { @@ -288,9 +332,11 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } } { - List<PAnnotatedEStructuralFeature> features = aClass.getPaEStructuralFeatures(); + List<PAnnotatedEStructuralFeature> features = aClass + .getPaEStructuralFeatures(); for (PAnnotatedEStructuralFeature feature : features) { - if (feature instanceof PAnnotatedEReference && ((PAnnotatedEReference) feature).getEmbeddedId() != null) { + if (feature instanceof PAnnotatedEReference + && ((PAnnotatedEReference) feature).getEmbeddedId() != null) { return true; } } @@ -307,8 +353,9 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } final HbAnnotatedEClass hbEntity = (HbAnnotatedEClass) entity; - Element entityElement = createEntity(entity, entity.getInheritanceStrategy(), entity.getPaSuperEntity(), entity - .getDiscriminatorValue(), entity.getTable()); + Element entityElement = createEntity(entity, + entity.getInheritanceStrategy(), entity.getPaSuperEntity(), + entity.getDiscriminatorValue(), entity.getTable()); if (entity.getEavMapping() != null) { return; @@ -322,45 +369,58 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { // the id // element has been placed - if (entity.getPaSuperEntity() == null && entity.getPrimaryKeyJoinColumns() != null + if (entity.getPaSuperEntity() == null + && entity.getPrimaryKeyJoinColumns() != null && entity.getPrimaryKeyJoinColumns().size() > 0) { - log.warn("This entity (" + entity.getEntity().getName() + " is the root in the class hierarchy and " + log.warn("This entity (" + + entity.getEntity().getName() + + " is the root in the class hierarchy and " + "has a pk joincolum annotation, this is not correct, ignoring pk joincolumn annotation"); - } else if (InheritanceType.SINGLE_TABLE.equals(entity.getInheritanceStrategy()) - && entity.getPrimaryKeyJoinColumns() != null && entity.getPrimaryKeyJoinColumns().size() > 0) { - log.warn("Single table inheritance strategy (entity " + entity.getEntity().getName() + } else if (InheritanceType.SINGLE_TABLE.equals(entity + .getInheritanceStrategy()) + && entity.getPrimaryKeyJoinColumns() != null + && entity.getPrimaryKeyJoinColumns().size() > 0) { + log.warn("Single table inheritance strategy (entity " + + entity.getEntity().getName() + ", primary key join column annotation is ignored for subclass"); - } else if (entity.getPrimaryKeyJoinColumns() != null && entity.getPrimaryKeyJoinColumns().size() > 0) { + } else if (entity.getPrimaryKeyJoinColumns() != null + && entity.getPrimaryKeyJoinColumns().size() > 0) { addPrimaryKeyJoinColumn(entity.getPrimaryKeyJoinColumns(), entity); - } else if (entity.getPaSuperEntity() != null && InheritanceType.JOINED.equals(entity.getInheritanceStrategy())) { + } else if (entity.getPaSuperEntity() != null + && InheritanceType.JOINED.equals(entity + .getInheritanceStrategy())) { final ArrayList<PrimaryKeyJoinColumn> list = new ArrayList<PrimaryKeyJoinColumn>(); - final PrimaryKeyJoinColumn pkjc = PannotationFactory.eINSTANCE.createPrimaryKeyJoinColumn(); + final PrimaryKeyJoinColumn pkjc = PannotationFactory.eINSTANCE + .createPrimaryKeyJoinColumn(); final String entityName = mc.getEntityName(entity.getModelEClass()); - mc.trunc(entityName + "id"); // TODO improve name + mc.trunc(pkjc, entityName + "id"); // TODO improve name // creation here list.add(pkjc); addPrimaryKeyJoinColumn(list, entity); } try { - final List<PAnnotatedEStructuralFeature> inheritedFeatures = mc.getInheritedFeatures(entity); + final List<PAnnotatedEStructuralFeature> inheritedFeatures = mc + .getInheritedFeatures(entity); mc.setForceOptional(entity.getPaSuperEntity() != null - && (entity.getInheritanceStrategy() == null || InheritanceType.SINGLE_TABLE.equals(entity - .getInheritanceStrategy()))); + && (entity.getInheritanceStrategy() == null || InheritanceType.SINGLE_TABLE + .equals(entity.getInheritanceStrategy()))); mc.pushOverrideOnStack(); mc.addAttributeOverrides(entity.getAttributeOverrides()); mc.addAssociationOverrides(entity.getAssociationOverrides()); if (inheritedFeatures.size() > 0) { - log.debug("There are " + inheritedFeatures.size() + " inherited features "); + log.debug("There are " + inheritedFeatures.size() + + " inherited features "); } try { processFeatures(inheritedFeatures); - final EList<SecondaryTable> secondaryTables = entity.getSecondaryTables(); + final EList<SecondaryTable> secondaryTables = entity + .getSecondaryTables(); if (secondaryTables == null || secondaryTables.isEmpty()) { // Process features normally. processFeatures(entity.getPaEStructuralFeatures()); @@ -380,7 +440,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } // create a synthetic id for roots - if (idElement == null && entity.getPaSuperEntity() == null && entity.getPaMappedSupers().size() == 0) { + if (idElement == null && entity.getPaSuperEntity() == null + && entity.getPaMappedSupers().size() == 0) { idElement = IdMapper.addSyntheticId(hbmContext, entityElement); } else if (mc.mustAddSyntheticID(entity)) { idElement = IdMapper.addSyntheticId(hbmContext, entityElement); @@ -392,12 +453,19 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { int index = entityElement.indexOf(idElement) + 1; if (((HbAnnotatedEClass) entity).getDiscriminatorFormula() != null) { - entityElement.add(index++, createDiscriminatorElement(hbEntity.getDiscriminatorFormula(), entity - .getDiscriminatorColumn(), hbEntity.getForceDiscriminator() != null)); + entityElement.add( + index++, + createDiscriminatorElement( + hbEntity.getDiscriminatorFormula(), + entity.getDiscriminatorColumn(), + hbEntity.getForceDiscriminator() != null)); } else if (entity.getDiscriminatorColumn() != null) { // add discriminator element immediately after id element - entityElement.add(index++, createDiscriminatorElement(entity.getDiscriminatorColumn(), hbEntity - .getForceDiscriminator() != null)); + entityElement.add( + index++, + createDiscriminatorElement( + entity.getDiscriminatorColumn(), + hbEntity.getForceDiscriminator() != null)); } // create and/or move version element (if present) immediately @@ -430,7 +498,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } // initially the comment is placed at the back, move it to the front - final Element comment = addCommentElement(entity.getModelEClass(), entityElement); + final Element comment = addCommentElement(entity.getModelEClass(), + entityElement); if (comment != null) { entityElement.remove(comment); entityElement.add(0, comment); @@ -439,19 +508,22 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { // place the tuplizer at the front mc.addTuplizerElement(entityElement, entity); - if ((entity.getPaSuperEntity() == null || entity.getPaSuperEntity().getMappedSuperclass() != null) + if ((entity.getPaSuperEntity() == null || entity.getPaSuperEntity() + .getMappedSuperclass() != null) && ((HbAnnotatedEClass) entity).getHbCache() != null) { // then add the cache at the front - addCacheElement(entityElement, ((HbAnnotatedEClass) entity).getHbCache()); + addCacheElement(entityElement, + ((HbAnnotatedEClass) entity).getHbCache()); } // and add the metas at the front final Element meta1 = new Element("meta"); - meta1.addAttribute("attribute", HbMapperConstants.ECLASS_NAME_META).addText(entity.getModelEClass().getName()); + meta1.addAttribute("attribute", HbMapperConstants.ECLASS_NAME_META) + .addText(entity.getModelEClass().getName()); meta1.addAttribute("inherit", "false"); final Element meta2 = new Element("meta"); - meta2.addAttribute("attribute", HbMapperConstants.EPACKAGE_META).addText( - entity.getModelEClass().getEPackage().getNsURI()); + meta2.addAttribute("attribute", HbMapperConstants.EPACKAGE_META) + .addText(entity.getModelEClass().getEPackage().getNsURI()); meta2.addAttribute("inherit", "false"); entityElement.add(0, meta1); @@ -476,7 +548,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { boolean mutable = false; for (Element element : parentElement.getChildren()) { String value; - if ((value = element.getAttributeValue(HbMapperConstants.NATURAL_ID_ATTR)) != null) { + if ((value = element + .getAttributeValue(HbMapperConstants.NATURAL_ID_ATTR)) != null) { if (naturalIdElements.isEmpty()) { log.debug("Found natural-id, mutable: " + mutable); mutable = Boolean.parseBoolean(value); @@ -486,7 +559,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } } if (!naturalIdElements.isEmpty()) { - log.debug("Found " + naturalIdElements.size() + log.debug("Found " + + naturalIdElements.size() + " naturalid properies, putting them inside a natural-id tag"); parentElement.getChildren().removeAll(naturalIdElements); final Element naturalId = parentElement.addElement("natural-id"); @@ -510,7 +584,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { // added // to the featuremapmappers in the context, therefore this process walks // on - List<FeatureMapMapping> featureMapMappers = getHbmContext().getClearFeatureMapMappers(); + List<FeatureMapMapping> featureMapMappers = getHbmContext() + .getClearFeatureMapMappers(); while (featureMapMappers.size() > 0) { for (FeatureMapMapping fmm : featureMapMappers) { @@ -524,7 +599,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } /** - * Process the features of the mapped superclass so that they are added to the current mapping + * Process the features of the mapped superclass so that they are added to + * the current mapping */ protected void processMappedSuper(PAnnotatedEClass paMappedSuper) { processFeatures(paMappedSuper.getPaEStructuralFeatures()); @@ -538,7 +614,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } /** Process secondary tables. */ - protected void processSecondaryTables(List<SecondaryTable> secondaryTables, PAnnotatedEClass entity) { + protected void processSecondaryTables(List<SecondaryTable> secondaryTables, + PAnnotatedEClass entity) { // Group features by primary and secondary tables. // For the primary table we use null as the surrogate key. (HashMap // allows null keys.) @@ -548,17 +625,22 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { tableNames.add(secondaryTable.getName()); } final Map<String, List<PAnnotatedEStructuralFeature>> featuresByTable = new HashMap<String, List<PAnnotatedEStructuralFeature>>(); - for (PAnnotatedEStructuralFeature feature : entity.getPaEStructuralFeatures()) { + for (PAnnotatedEStructuralFeature feature : entity + .getPaEStructuralFeatures()) { // find the table for the feature - final String tableName = getHbmContext().getSecondaryTableName(feature); + final String tableName = getHbmContext().getSecondaryTableName( + feature); // if there put it in the correct list if (!tableNames.contains(tableName)) { - final String message = "Feature \"" + feature.getModelElement().getName() - + "\" was mapped to undeclared secondary table \"" + tableName + "\"."; + final String message = "Feature \"" + + feature.getModelElement().getName() + + "\" was mapped to undeclared secondary table \"" + + tableName + "\"."; log.error(message); throw new MappingException(message); } - List<PAnnotatedEStructuralFeature> featuresInTable = featuresByTable.get(tableName); + List<PAnnotatedEStructuralFeature> featuresInTable = featuresByTable + .get(tableName); if (featuresInTable == null) { featuresInTable = new ArrayList<PAnnotatedEStructuralFeature>(); featuresByTable.put(tableName, featuresInTable); @@ -568,7 +650,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { // Process features in primary table. { - final List<PAnnotatedEStructuralFeature> featuresInTable = featuresByTable.get(PRIMARY_TABLE_KEY); + final List<PAnnotatedEStructuralFeature> featuresInTable = featuresByTable + .get(PRIMARY_TABLE_KEY); // can happen if there are only secondarytable features and no // primary table // features see bugzilla 235949 @@ -584,31 +667,44 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { // The table was listed in the SecondaryTable annotation, but no // features were actually mapped to this // table. - log.warn("No features mapped to secondary table " + secondaryTable.getName()); + log.warn("No features mapped to secondary table " + + secondaryTable.getName()); continue; } - final List<PAnnotatedEStructuralFeature> featuresInTable = featuresByTable.get(secondaryTable.getName()); + final List<PAnnotatedEStructuralFeature> featuresInTable = featuresByTable + .get(secondaryTable.getName()); // Create <join> element // See // http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration // -join - final Element joinElement = getHbmContext().getCurrent().addElement("join"); - joinElement.addAttribute("table", getHbmContext().trunc(secondaryTable.getName().toUpperCase(), false)); - log.debug("Mapping features to secondary table \"" + secondaryTable.getName() + "\""); + final Element joinElement = getHbmContext().getCurrent() + .addElement("join"); + joinElement.addAttribute( + "table", + getHbmContext().trunc( + secondaryTable.getName().toUpperCase(), false)); + log.debug("Mapping features to secondary table \"" + + secondaryTable.getName() + "\""); if (secondaryTable.getCatalog() != null) { - joinElement.addAttribute("catalog", secondaryTable.getCatalog()); + joinElement + .addAttribute("catalog", secondaryTable.getCatalog()); } if (secondaryTable.getSchema() != null) { joinElement.addAttribute("schema", secondaryTable.getSchema()); } entity.getModelEClass().getEIDAttribute(); - final List<PrimaryKeyJoinColumn> pkJoinColumns = secondaryTable.getPkJoinColumns(); + final List<PrimaryKeyJoinColumn> pkJoinColumns = secondaryTable + .getPkJoinColumns(); final Element keyElement = joinElement.addElement("key"); for (PrimaryKeyJoinColumn pkJoinColumn : pkJoinColumns) { - keyElement.addElement("column").addAttribute("name", getHbmContext().trunc(pkJoinColumn.getName())); + keyElement.addElement("column").addAttribute( + "name", + getHbmContext().trunc(pkJoinColumn, + pkJoinColumn.getName())); } - handleOndelete(keyElement, ((HbAnnotatedEClass) entity).getHbOnDelete()); + handleOndelete(keyElement, + ((HbAnnotatedEClass) entity).getHbOnDelete()); // Process features in this secondary table. try { @@ -630,35 +726,45 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } /** - * Adds a key element to the current entity mapping, is for example used to join to the super class table. + * Adds a key element to the current entity mapping, is for example used to + * join to the super class table. */ - private void addPrimaryKeyJoinColumn(List<PrimaryKeyJoinColumn> pkJCs, PAnnotatedEClass entity) { + private void addPrimaryKeyJoinColumn(List<PrimaryKeyJoinColumn> pkJCs, + PAnnotatedEClass entity) { log.debug("Adding primary key join column"); - final Element jcElement = getHbmContext().getCurrent().addElement("key"); + final Element jcElement = getHbmContext().getCurrent() + .addElement("key"); handleOndelete(jcElement, ((HbAnnotatedEClass) entity).getHbOnDelete()); for (PrimaryKeyJoinColumn pkJC : pkJCs) { final Element columnElement = jcElement.addElement("column"); if (pkJC.getColumnDefinition() != null) { - log.error("Unsupported column definition in primary key join column " + pkJC); - throw new MappingException("Unsupported column definition in primary key join column", pkJC); + log.error("Unsupported column definition in primary key join column " + + pkJC); + throw new MappingException( + "Unsupported column definition in primary key join column", + pkJC); } if (pkJC.getName() != null) { - columnElement.addAttribute("name", getHbmContext().trunc(pkJC.getName())); + columnElement.addAttribute("name", + getHbmContext().trunc(pkJC, pkJC.getName())); } if (pkJC.getReferencedColumnName() != null) { - columnElement.addAttribute("property-ref", pkJC.getReferencedColumnName()); + columnElement.addAttribute("property-ref", + pkJC.getReferencedColumnName()); } } } /** - * Creates a discriminator element and returns it, so the caller should add it to the parent element + * Creates a discriminator element and returns it, so the caller should add + * it to the parent element */ - private Element createDiscriminatorElement(DiscriminatorColumn dColumn, boolean force) { + private Element createDiscriminatorElement(DiscriminatorColumn dColumn, + boolean force) { Element dcElement = DocumentHelper.createElement("discriminator"); if (dColumn.getColumn() != null) { final Column col = dColumn.getColumn(); @@ -667,16 +773,21 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { if (col.getIndex() != null) { colElement.addAttribute("index", col.getIndex()); } - if (hbDiscriminatorType(dColumn.getDiscriminatorType()).equals("string") && col.getLength() > 0) { - colElement.addAttribute("length", Integer.toString(col.getLength())); + if (hbDiscriminatorType(dColumn.getDiscriminatorType()).equals( + "string") + && col.getLength() > 0) { + colElement.addAttribute("length", + Integer.toString(col.getLength())); } if (!col.isNullable()) { colElement.addAttribute("not-null", "true"); } } else if (dColumn.getName() != null) { - dcElement.addAttribute("column", getHbmContext().trunc(dColumn.getName())); + dcElement.addAttribute("column", + getHbmContext().trunc(dColumn, dColumn.getName())); } - dcElement.addAttribute("type", hbDiscriminatorType(dColumn.getDiscriminatorType())); + dcElement.addAttribute("type", + hbDiscriminatorType(dColumn.getDiscriminatorType())); if (force) { dcElement.addAttribute("force", "true"); @@ -687,14 +798,18 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } if (dColumn.getColumnDefinition() != null) { - log.error("Unsupported column definition in discriminator column " + dColumn); - throw new MappingException("Unsupported column definition in discriminator column", dColumn); + log.error("Unsupported column definition in discriminator column " + + dColumn); + throw new MappingException( + "Unsupported column definition in discriminator column", + dColumn); } return dcElement; } // note dc can be null - private Element createDiscriminatorElement(DiscriminatorFormula formula, DiscriminatorColumn dc, boolean force) { + private Element createDiscriminatorElement(DiscriminatorFormula formula, + DiscriminatorColumn dc, boolean force) { Element dcElement = DocumentHelper.createElement("discriminator"); dcElement.addAttribute("formula", formula.getValue()); @@ -703,7 +818,8 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { } if (dc != null && dc.getDiscriminatorType() != null) { - dcElement.addAttribute("type", hbDiscriminatorType(dc.getDiscriminatorType())); + dcElement.addAttribute("type", + hbDiscriminatorType(dc.getDiscriminatorType())); } return dcElement; } @@ -717,13 +833,17 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { if (!getHbmContext().alwaysVersion()) { return null; } - + // if none optimistic lock then return - if (aClass.getHbEntity() != null && aClass.getHbEntity().getOptimisticLock() != null && aClass.getHbEntity().getOptimisticLock().equals(OptimisticLockType.NONE)) { + if (aClass.getHbEntity() != null + && aClass.getHbEntity().getOptimisticLock() != null + && aClass.getHbEntity().getOptimisticLock() + .equals(OptimisticLockType.NONE)) { return null; } - - final boolean skipVersionOnImmutable = !getHbmContext().getPersistenceOptions() + + final boolean skipVersionOnImmutable = !getHbmContext() + .getPersistenceOptions() .isDiscriminatorVersionOnImmutableEClass(); if (aClass.getImmutable() != null && skipVersionOnImmutable) { return null; @@ -732,14 +852,18 @@ public class EntityMapper extends AbstractMapper implements ExtensionPoint { // note specific accessor is required because version accessor is not // retrieved through // emf tuplizer - final Element versionElement = getHbmContext().getCurrent().addElement("version").addAttribute("name", - getHbmContext().getVersionColumnName()).addAttribute("column", getHbmContext().getVersionColumnName()); + final Element versionElement = getHbmContext().getCurrent() + .addElement("version") + .addAttribute("name", getHbmContext().getVersionColumnName()) + .addAttribute("column", getHbmContext().getVersionColumnName()); final Element meta = new Element("meta"); - meta.addAttribute("attribute", HbMapperConstants.VERSION_META).addText("true"); + meta.addAttribute("attribute", HbMapperConstants.VERSION_META).addText( + "true"); meta.addAttribute("inherit", "false"); versionElement.add(0, meta); - versionElement.addAttribute("access", getHbmContext().getSyntheticVersionPropertyHandlerName()); + versionElement.addAttribute("access", getHbmContext() + .getSyntheticVersionPropertyHandlerName()); return versionElement; } diff --git a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/ManyToManyMapper.java b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/ManyToManyMapper.java index cbdc69bd9..aab8029df 100644 --- a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/ManyToManyMapper.java +++ b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/ManyToManyMapper.java @@ -3,7 +3,7 @@ * reserved. This program and the accompanying materials are made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html Contributors: Martin Taal - * </copyright> $Id: ManyToManyMapper.java,v 1.32 2009/03/07 21:15:20 mtaal Exp $ + * </copyright> $Id: ManyToManyMapper.java,v 1.33 2011/02/21 06:39:57 mtaal Exp $ */ package org.eclipse.emf.teneo.hibernate.mapper; @@ -56,8 +56,8 @@ public class ManyToManyMapper extends AbstractAssociationMapper implements } if (((HbAnnotatedEReference) paReference).getHbCache() != null) { - addCacheElement(collElement, ((HbAnnotatedEReference) paReference) - .getHbCache()); + addCacheElement(collElement, + ((HbAnnotatedEReference) paReference).getHbCache()); } final Element keyElement = collElement.addElement("key"); @@ -79,8 +79,9 @@ public class ManyToManyMapper extends AbstractAssociationMapper implements } addFetchType(collElement, mtm.getFetch()); - addCascades(collElement, getCascades(hbReference.getHbCascade(), mtm - .getCascade()), false); + addCascades(collElement, + getCascades(hbReference.getHbCascade(), mtm.getCascade()), + false); final PAnnotatedEClass referedToAClass = hbReference .getAReferenceType(); @@ -94,14 +95,17 @@ public class ManyToManyMapper extends AbstractAssociationMapper implements final Element mtmElement; if (referedToAClass.isOnlyMapAsEntity() || !getHbmContext().forceUseOfInstance(referedToAClass)) { - mtmElement = collElement.addElement("many-to-many").addAttribute( - "entity-name", targetName).addAttribute("unique", "false"); + mtmElement = collElement.addElement("many-to-many") + .addAttribute("entity-name", targetName) + .addAttribute("unique", "false"); } else { - mtmElement = collElement.addElement("many-to-many").addAttribute( - "class", - getHbmContext().getInstanceClassName( - hbReference.getEReferenceType())).addAttribute( - "unique", "false"); + mtmElement = collElement + .addElement("many-to-many") + .addAttribute( + "class", + getHbmContext().getInstanceClassName( + hbReference.getEReferenceType())) + .addAttribute("unique", "false"); } if (hbReference.getNotFound() != null) { @@ -119,8 +123,7 @@ public class ManyToManyMapper extends AbstractAssociationMapper implements // both sides of the relation twice. collElement.addAttribute("inverse", "true"); } else if (mtm.getMappedBy() != null && mtm.isIndexed()) { - log - .warn("Indexed is true but indexed is not supported for inverse=true and many-to-many, not setting inverse=true"); + log.warn("Indexed is true but indexed is not supported for inverse=true and many-to-many, not setting inverse=true"); } // check for a special case that mapped by is set on both sides @@ -132,10 +135,9 @@ public class ManyToManyMapper extends AbstractAssociationMapper implements if (aOpposite.getManyToMany() != null && aOpposite.getManyToMany().getMappedBy() != null && mtm.getMappedBy() != null) { - log - .error("Mappedby is set on both sides of the many-to-many relation, this does not work, see the efeature: " - + hbReference.getModelElement().toString() - + ". Ignoring the mappedby in this efeature"); + log.error("Mappedby is set on both sides of the many-to-many relation, this does not work, see the efeature: " + + hbReference.getModelElement().toString() + + ". Ignoring the mappedby in this efeature"); mtm.setMappedBy(null); } } @@ -143,8 +145,12 @@ public class ManyToManyMapper extends AbstractAssociationMapper implements addJoinTable(hbReference, collElement, keyElement, jt); if (jt.getInverseJoinColumns() != null) { for (JoinColumn joinColumn : jt.getInverseJoinColumns()) { - mtmElement.addElement("column").addAttribute("name", - getHbmContext().trunc(joinColumn.getName())) + mtmElement + .addElement("column") + .addAttribute( + "name", + getHbmContext().trunc(joinColumn, + joinColumn.getName())) .addAttribute("not-null", joinColumn.isNullable() ? "false" : "true") .addAttribute("unique", @@ -154,7 +160,7 @@ public class ManyToManyMapper extends AbstractAssociationMapper implements addAccessor(collElement); - mapFilter(collElement, ((HbAnnotatedETypeElement) paReference) - .getFilter()); + mapFilter(collElement, + ((HbAnnotatedETypeElement) paReference).getFilter()); } } diff --git a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/ManyToOneMapper.java b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/ManyToOneMapper.java index 3179ca041..4d2c7a082 100644 --- a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/ManyToOneMapper.java +++ b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/ManyToOneMapper.java @@ -3,7 +3,7 @@ * reserved. This program and the accompanying materials are made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html Contributors: Martin Taal - * </copyright> $Id: ManyToOneMapper.java,v 1.37 2010/03/27 21:13:39 mtaal Exp $ + * </copyright> $Id: ManyToOneMapper.java,v 1.38 2011/02/21 06:39:57 mtaal Exp $ */ package org.eclipse.emf.teneo.hibernate.mapper; @@ -91,12 +91,17 @@ public class ManyToOneMapper extends AbstractAssociationMapper implements final boolean addInverse = aOpposite != null && aOpposite.getOneToMany() != null && aOpposite.getOneToMany().getMappedBy() == null - && !aOpposite.getOneToMany().getFetch().equals( - FetchType.EXTRA); - - currentElement = getHbmContext().getCurrent().addElement("join") - .addAttribute("table", getHbmContext().trunc(joinTable.getName())).addAttribute( - "inverse", Boolean.toString(addInverse)) + && !aOpposite.getOneToMany().getFetch() + .equals(FetchType.EXTRA); + + currentElement = getHbmContext() + .getCurrent() + .addElement("join") + .addAttribute( + "table", + getHbmContext().trunc(joinTable, + joinTable.getName())) + .addAttribute("inverse", Boolean.toString(addInverse)) .addAttribute("optional", Boolean.toString(mto.isOptional())); @@ -122,15 +127,15 @@ public class ManyToOneMapper extends AbstractAssociationMapper implements } if (!isProperty && joinTable != null) { - addJoinColumns(paReference, associationElement, joinTable - .getJoinColumns(), mto.isOptional() - || getHbmContext().isDoForceOptional(paReference) - || getHbmContext().isCurrentElementFeatureMap()); + addJoinColumns(paReference, associationElement, + joinTable.getJoinColumns(), mto.isOptional() + || getHbmContext().isDoForceOptional(paReference) + || getHbmContext().isCurrentElementFeatureMap()); } if (!isProperty) { - addCascadesForSingle(associationElement, getCascades(hbReference - .getHbCascade(), mto.getCascade())); + addCascadesForSingle(associationElement, + getCascades(hbReference.getHbCascade(), mto.getCascade())); } if (hbReference.getHbFetch() != null) { diff --git a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/MappingContext.java b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/MappingContext.java index 17420d531..676f1c173 100644 --- a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/MappingContext.java +++ b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/MappingContext.java @@ -3,7 +3,7 @@ * reserved. This program and the accompanying materials are made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html Contributors: Martin Taal - * </copyright> $Id: MappingContext.java,v 1.36 2009/11/02 18:14:17 mtaal Exp $ + * </copyright> $Id: MappingContext.java,v 1.37 2011/02/21 06:39:57 mtaal Exp $ */ package org.eclipse.emf.teneo.hibernate.mapper; @@ -28,6 +28,7 @@ import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEStructuralFeature; import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedModel; import org.eclipse.emf.teneo.annotations.pannotation.Column; import org.eclipse.emf.teneo.annotations.pannotation.JoinColumn; +import org.eclipse.emf.teneo.annotations.pannotation.PAnnotation; import org.eclipse.emf.teneo.annotations.pannotation.SecondaryTable; import org.eclipse.emf.teneo.annotations.pannotation.Table; import org.eclipse.emf.teneo.annotations.pannotation.UniqueConstraint; @@ -45,10 +46,10 @@ import org.eclipse.emf.teneo.simpledom.Element; * Maps a basic attribute with many=true, e.g. list of simpletypes. * * @author <a href="mailto:mtaal@elver.org">Martin Taal</a> - * @version $Revision: 1.36 $ + * @version $Revision: 1.37 $ */ -public class MappingContext extends AbstractProcessingContext implements ExtensionPoint, ExtensionInitializable, - ExtensionManagerAware { +public class MappingContext extends AbstractProcessingContext implements + ExtensionPoint, ExtensionInitializable, ExtensionManagerAware { /** The xml document to which all elements are added */ private Document mappingDoc; @@ -60,7 +61,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi private Map<EClass, String> entityNames = null; /** - * Keeps track of the list of featuremapmappers created for the current entity + * Keeps track of the list of featuremapmappers created for the current + * entity */ private final List<FeatureMapMapping> featureMapMappers = new ArrayList<FeatureMapMapping>(); @@ -71,8 +73,9 @@ public class MappingContext extends AbstractProcessingContext implements Extensi private FeatureMapper featureMapper; /** - * Is the current element a mixed or a feature map, in this case all features should be not required. TODO: check, - * does this work with embedded components in a feature map? + * Is the current element a mixed or a feature map, in this case all + * features should be not required. TODO: check, does this work with + * embedded components in a feature map? */ private boolean currentElementFeatureMap = false; @@ -80,7 +83,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi private Table currentTable = null; /** - * The current column prefix, is used in case of multiple mixed types in one eclass. + * The current column prefix, is used in case of multiple mixed types in one + * eclass. */ private String namePrefix = ""; @@ -118,9 +122,12 @@ public class MappingContext extends AbstractProcessingContext implements Extensi /** The escape character string used for escaping sql names */ protected String escapeCharacter; + private boolean adaptManualNames = false; + /** - * Set force optional, force optional is used in case a subclass is stored in the same table as its superclass, in - * this case all properties of the subclass are denoted as optional. + * Set force optional, force optional is used in case a subclass is stored + * in the same table as its superclass, in this case all properties of the + * subclass are denoted as optional. */ private boolean forceOptional = false; @@ -147,6 +154,7 @@ public class MappingContext extends AbstractProcessingContext implements Extensi versionColumnName = po.getVersionColumnName(); idColumnName = po.getIdColumnName(); maximumSqlNameLength = po.getMaximumSqlNameLength(); + adaptManualNames = po.isAutoAdaptManualSQLNames(); alwaysVersion = po.getAlwaysVersion(); isMapEMapAsTrueMap = po.isMapEMapAsTrueMap(); idbagIDColumnName = po.getIDBagIDColumnName(); @@ -189,7 +197,9 @@ public class MappingContext extends AbstractProcessingContext implements Extensi } } if (throwCheckException && name == null) { - throw new IllegalStateException("An entity name has not been registered for " + entityEClass); + throw new IllegalStateException( + "An entity name has not been registered for " + + entityEClass); } return name; } @@ -235,7 +245,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi * @return the featureMapMappers gathered during the entity processing */ public List<FeatureMapMapping> getClearFeatureMapMappers() { - final ArrayList<FeatureMapMapping> result = new ArrayList<FeatureMapMapping>(featureMapMappers); // clone + final ArrayList<FeatureMapMapping> result = new ArrayList<FeatureMapMapping>( + featureMapMappers); // clone // the // list! featureMapMappers.clear(); @@ -247,7 +258,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi * a featureMapMapper to the featuremapp mapper list */ public void addFeatureMapMapper(FeatureMapMapping featureMapMapper) { - if (!handledFeatureMapEAttributes.contains(featureMapMapper.getEAttribute())) { + if (!handledFeatureMapEAttributes.contains(featureMapMapper + .getEAttribute())) { featureMapMappers.add(featureMapMapper); handledFeatureMapEAttributes.add(featureMapMapper.getEAttribute()); } @@ -256,7 +268,9 @@ public class MappingContext extends AbstractProcessingContext implements Extensi /* * (non-Javadoc) * - * @see org.eclipse.emf.teneo.extension.ExtensionInitializable#initializeExtension () + * @see + * org.eclipse.emf.teneo.extension.ExtensionInitializable#initializeExtension + * () */ public void initializeExtension() { featureMapper = createFeatureMapper(); @@ -268,18 +282,21 @@ public class MappingContext extends AbstractProcessingContext implements Extensi * @return The builder used by entity mapped that maps features to hbm. */ private FeatureMapper createFeatureMapper() { - final FeatureMapper featureMapper = getExtensionManager().getExtension(FeatureMapper.class); + final FeatureMapper featureMapper = getExtensionManager().getExtension( + FeatureMapper.class); featureMapper.setHbmContext(this); featureMapper.setBasicMapper(createMapper(BasicMapper.class)); - featureMapper.setManyAttributeMapper(createMapper(ManyAttributeMapper.class)); + featureMapper + .setManyAttributeMapper(createMapper(ManyAttributeMapper.class)); featureMapper.setEmbeddedMapper(createMapper(EmbeddedMapper.class)); featureMapper.setIdMapper(createMapper(IdMapper.class)); featureMapper.setManyToManyMapper(createMapper(ManyToManyMapper.class)); featureMapper.setManyToOneMapper(createMapper(ManyToOneMapper.class)); featureMapper.setOneToManyMapper(createMapper(OneToManyMapper.class)); featureMapper.setOneToOneMapper(createMapper(OneToOneMapper.class)); - featureMapper.setManyExternalReferenceMapper(createMapper(ManyExternalReferenceMapper.class)); + featureMapper + .setManyExternalReferenceMapper(createMapper(ManyExternalReferenceMapper.class)); return featureMapper; } @@ -391,25 +408,61 @@ public class MappingContext extends AbstractProcessingContext implements Extensi return maximumSqlNameLength; } - /** Default is trunc */ + /** + * @return true if {@link PersistenceOptions#isAutoAdaptManualSQLNames()} is + * true or if the {@link PAnnotation#isGenerated()} is true. + */ + protected boolean doTrunc(PAnnotation pAnnotation) { + if (adaptManualNames) { + return true; + } + return pAnnotation.isGenerated(); + } + + /** + * @deprecated use {@link #trunc(PAnnotation, String)} + */ protected String trunc(String name) { - return trunc(name, true); + return trunc(null, name, true); + } + + /** + * @deprecated use {@link #trunc(PAnnotation, String, boolean)} + */ + protected String trunc(String truncName, boolean truncPrefix) { + return trunc(null, truncName, truncPrefix); + } + + /** + * Calls {@link #trunc(PAnnotation, String, boolean)} with the last + * parameter set to true. + */ + protected String trunc(PAnnotation pAnnotation, String name) { + return trunc(pAnnotation, name, true); } /** - * Utility method to truncate a column/table name. This method also repairs the name if an efeature was inherited - * and really belongs to another eclass. In this case jointables and join keys must be renamed to the new eclass. - * TODO: handle the case that the jointable/columns were set manually. This procedure will override them (only - * applies in case of multiple inheritance/mappedsuperclass). This renaming is required for the case that an - * ereference is inherited from a mapped superclass, in this case the join-column of the e-reference will be placed - * in another table. If one ereference is inherited by multiple subtypes then this goes wrong because they then all - * share the same join column with foreign keys relating it to different tables, and multiple foreign keys on one - * column can not point to different directions. + * Utility method to truncate a column/table name. This method also repairs + * the name if an efeature was inherited and really belongs to another + * eclass. In this case jointables and join keys must be renamed to the new + * eclass. TODO: handle the case that the jointable/columns were set + * manually. This procedure will override them (only applies in case of + * multiple inheritance/mappedsuperclass). This renaming is required for the + * case that an ereference is inherited from a mapped superclass, in this + * case the join-column of the e-reference will be placed in another table. + * If one ereference is inherited by multiple subtypes then this goes wrong + * because they then all share the same join column with foreign keys + * relating it to different tables, and multiple foreign keys on one column + * can not point to different directions. * * This method is also called for table names. * */ - protected String trunc(String truncName, boolean truncPrefix) { + protected String trunc(PAnnotation pAnnotation, String truncName, + boolean truncPrefix) { + if (pAnnotation != null && !doTrunc(pAnnotation)) { + return truncName; + } // see bugzilla 225818 // in case of attributeoverride then do not repair the name @@ -423,20 +476,27 @@ public class MappingContext extends AbstractProcessingContext implements Extensi override = getAssociationOverrides((PAnnotatedEReference) currentAFeature) != null; } - final String otherEntityName = getEntityName(currentEFeature.getEContainingClass(), false); + final String otherEntityName = getEntityName( + currentEFeature.getEContainingClass(), false); // if the current name starts with the name of the mapped superclass // then // change it back to the current eclass, do not do this in case of // override - if (!override && currentEFeature.getEContainingClass() != currentEClass && otherEntityName != null - && truncName.toUpperCase().startsWith(otherEntityName.toUpperCase())) { + if (!override + && currentEFeature.getEContainingClass() != currentEClass + && otherEntityName != null + && truncName.toUpperCase().startsWith( + otherEntityName.toUpperCase())) { log.debug("Replacing name of table/joincolumn " + truncName); // get rid of the first part - useName = getNamePrefix() + getEntityName(currentEClass) - + truncName.substring(getEntityName(currentEFeature.getEContainingClass()).length()); + useName = getNamePrefix() + + getEntityName(currentEClass) + + truncName + .substring(getEntityName( + currentEFeature.getEContainingClass()) + .length()); log.debug("with " + useName + " because efeature is inherited"); - log - .debug("This renaming does not work in case of manually specified joincolumn/table names and mappedsuperclass or multiple inheritance!"); + log.debug("This renaming does not work in case of manually specified joincolumn/table names and mappedsuperclass or multiple inheritance!"); } else { useName = getNamePrefix() + truncName; } @@ -444,11 +504,14 @@ public class MappingContext extends AbstractProcessingContext implements Extensi useName = getNamePrefix() + truncName; } - if (escapeCharacter.length() > 0 && useName.indexOf(escapeCharacter) == 0) { + if (escapeCharacter.length() > 0 + && useName.indexOf(escapeCharacter) == 0) { return getSqlNameStrategy().convert(useName, false); } - return escapeCharacter + getSqlNameStrategy().convert(useName, false) + escapeCharacter; + return escapeCharacter + getSqlNameStrategy().convert(useName, false) + + escapeCharacter; + } /** @@ -509,26 +572,31 @@ public class MappingContext extends AbstractProcessingContext implements Extensi } /** - * Use the implementation name as the mapping and never use entity-mapping, always false in this implementation + * Use the implementation name as the mapping and never use entity-mapping, + * always false in this implementation */ public boolean forceUseOfInstance(PAnnotatedEClass aclass) { return false; } /** - * Returns true if the instance classes have been generated by emf. For Teneo this is always the case. Overriders - * can support a different generation strategy. + * Returns true if the instance classes have been generated by emf. For + * Teneo this is always the case. Overriders can support a different + * generation strategy. */ public boolean isGeneratedByEMF() { return true; } /** - * There are four cases: EMF generated, EMF Dynamic, Easy EMF Generated, Easy EMF Dynamic public boolean - * isEasyEMFGenerated(EClassifier eclassifier) { return - * EModelResolver.instance().hasImplementationClass(eclassifier); } public boolean isEasyEMFDynamic(EClassifier - * eclassifier) { return !isEasyEMFGenerated(eclassifier) && EModelResolver.instance().isRegistered( - * eclassifier.getEPackage()); } public boolean isEMFGenerated(EClassifier eclassifier) { return + * There are four cases: EMF generated, EMF Dynamic, Easy EMF Generated, + * Easy EMF Dynamic public boolean isEasyEMFGenerated(EClassifier + * eclassifier) { return + * EModelResolver.instance().hasImplementationClass(eclassifier); } public + * boolean isEasyEMFDynamic(EClassifier eclassifier) { return + * !isEasyEMFGenerated(eclassifier) && + * EModelResolver.instance().isRegistered( eclassifier.getEPackage()); } + * public boolean isEMFGenerated(EClassifier eclassifier) { return * eclassifier.getInstanceClass() != null; } */ // @@ -547,7 +615,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi // af.getAnnotatedEStructuralFeature().getEContainingClass()); // } /** Add a tuplizer element or not */ - public void addTuplizerElement(Element entityElement, PAnnotatedEClass aclass) { + public void addTuplizerElement(Element entityElement, + PAnnotatedEClass aclass) { } /** Returns the enumusertype class name */ @@ -611,7 +680,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi */ public EntityNameStrategy getEntityNameStrategy() { if (entityNameStrategy == null) { - entityNameStrategy = getExtensionManager().getExtension(EntityNameStrategy.class); + entityNameStrategy = getExtensionManager().getExtension( + EntityNameStrategy.class); entityNameStrategy.setPaModel(getPaModel()); // this call is not // really required // but @@ -655,7 +725,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi */ public void setCurrentFeature(PAnnotatedEStructuralFeature currentFeature) { this.currentAFeature = currentFeature; - this.currentEFeature = currentFeature == null ? null : currentFeature.getModelEStructuralFeature(); + this.currentEFeature = currentFeature == null ? null : currentFeature + .getModelEStructuralFeature(); } /** @@ -716,7 +787,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi */ public SQLNameStrategy getSqlNameStrategy() { if (sqlNameStrategy == null) { - sqlNameStrategy = getExtensionManager().getExtension(SQLNameStrategy.class); + sqlNameStrategy = getExtensionManager().getExtension( + SQLNameStrategy.class); } return sqlNameStrategy; } @@ -766,8 +838,9 @@ public class MappingContext extends AbstractProcessingContext implements Extensi } /** - * Returns the table name from the column annotation or the joincolumn annotation Also takes associationoverride or - * attributeoverride into account + * Returns the table name from the column annotation or the joincolumn + * annotation Also takes associationoverride or attributeoverride into + * account */ public String getSecondaryTableName(PAnnotatedEStructuralFeature pef) { String tableName = null; @@ -792,7 +865,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi // check the embedded efeatures // take the first feature of the target type // assume that they are all handled in the same table - final EClass eClass = per.getModelEReference().getEReferenceType(); + final EClass eClass = per.getModelEReference() + .getEReferenceType(); for (EAttribute ea : eClass.getEAllAttributes()) { final Column c = getAttributeOverride(ea.getName()); if (c != null && c.getTable() != null) { @@ -800,7 +874,8 @@ public class MappingContext extends AbstractProcessingContext implements Extensi } } for (EReference er : eClass.getEAllReferences()) { - final List<JoinColumn> jcs = getAssociationOverrides(er.getName()); + final List<JoinColumn> jcs = getAssociationOverrides(er + .getName()); if (jcs != null && jcs.size() > 0) { return jcs.get(0).getTable(); } diff --git a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/classic/ClassicMappingContext.java b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/classic/ClassicMappingContext.java index 9b69dc560..de6d09c4a 100644 --- a/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/classic/ClassicMappingContext.java +++ b/plugins/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/mapper/classic/ClassicMappingContext.java @@ -3,40 +3,49 @@ * reserved. This program and the accompanying materials are made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html Contributors: Martin Taal - * </copyright> $Id: ClassicMappingContext.java,v 1.6 2008/07/13 13:12:31 mtaal Exp $ + * </copyright> $Id: ClassicMappingContext.java,v 1.7 2011/02/21 06:39:57 mtaal Exp $ */ package org.eclipse.emf.teneo.hibernate.mapper.classic; import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEAttribute; import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEReference; +import org.eclipse.emf.teneo.annotations.pannotation.PAnnotation; import org.eclipse.emf.teneo.hibernate.mapper.MappingContext; /** * Maps a basic attribute with many=true, e.g. list of simpletypes. * * @author <a href="mailto:mtaal@elver.org">Martin Taal</a> - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ */ public class ClassicMappingContext extends MappingContext { /** - * Utilit method to truncate a column/table name. This method also repairs the name if an - * efeature was inherited and really belongs to another eclass. In this case jointables and join - * keys must be renamed to the new eclass. TODO: handle the case that the jointable/columns were - * set manually. This procedure will override them (only applies in case of multiple - * inheritance/mappedsuperclass). + * Utilit method to truncate a column/table name. This method also repairs + * the name if an efeature was inherited and really belongs to another + * eclass. In this case jointables and join keys must be renamed to the new + * eclass. TODO: handle the case that the jointable/columns were set + * manually. This procedure will override them (only applies in case of + * multiple inheritance/mappedsuperclass). * - * This renaming is required for the case that an ereference is inherited from a mapped - * superclass, in this case the join-column of the e-reference will be placed in another table. - * If one ereference is inherited by multiple subtypes then this goes wrong because they then - * all share the same join column with foreign keys relating it to different tables, and - * multiple foreign keys on one column can not point to different directions. + * This renaming is required for the case that an ereference is inherited + * from a mapped superclass, in this case the join-column of the e-reference + * will be placed in another table. If one ereference is inherited by + * multiple subtypes then this goes wrong because they then all share the + * same join column with foreign keys relating it to different tables, and + * multiple foreign keys on one column can not point to different + * directions. * - * TODO: is really only required for eattributes TODO: it ignores attributeoverrides + * TODO: is really only required for eattributes TODO: it ignores + * attributeoverrides */ @Override - protected String trunc(String truncName, boolean truncSuffix) { + protected String trunc(PAnnotation pAnnotation, String truncName, + boolean truncSuffix) { + if (pAnnotation != null && !doTrunc(pAnnotation)) { + return truncName; + } final String useName; // currentEFeature is null in the beginning if (currentAFeature != null) { @@ -47,19 +56,26 @@ public class ClassicMappingContext extends MappingContext { override = getAssociationOverrides((PAnnotatedEReference) currentAFeature) != null; } - final String otherEntityName = getEntityName(currentEFeature.getEContainingClass(), false); - // if the current name starts with the name of the mapped superclass then - // change it back to the current eclass, do not do this in case of override - if (!override && currentEFeature.getEContainingClass() != currentEClass && otherEntityName != null && - truncName.toUpperCase().startsWith(otherEntityName.toUpperCase())) { + final String otherEntityName = getEntityName( + currentEFeature.getEContainingClass(), false); + // if the current name starts with the name of the mapped superclass + // then + // change it back to the current eclass, do not do this in case of + // override + if (!override + && currentEFeature.getEContainingClass() != currentEClass + && otherEntityName != null + && truncName.toUpperCase().startsWith( + otherEntityName.toUpperCase())) { log.debug("Replacing name of table/joincolumn " + truncName); // get rid of the first part - useName = - getEntityName(currentEClass) + - truncName.substring(getEntityName(currentEFeature.getEContainingClass()).length()); + useName = getEntityName(currentEClass) + + truncName + .substring(getEntityName( + currentEFeature.getEContainingClass()) + .length()); log.debug("with " + useName + " because efeature is inherited"); - log - .debug("This renaming does not work in case of manually specified joincolumn/table names and mappedsuperclass or multiple inheritance!"); + log.debug("This renaming does not work in case of manually specified joincolumn/table names and mappedsuperclass or multiple inheritance!"); } else { useName = truncName; } @@ -81,7 +97,9 @@ public class ClassicMappingContext extends MappingContext { if ((maximumSqlNameLength - usStr.length()) < 0) { return escape(useName); } - return escape(useName.substring(0, maximumSqlNameLength - usStr.length()) + usStr); + return escape(useName.substring(0, + maximumSqlNameLength - usStr.length()) + + usStr); } return escape(useName.substring(0, maximumSqlNameLength)); @@ -89,11 +107,21 @@ public class ClassicMappingContext extends MappingContext { /** Escape the column name */ protected String escape(String name) { - if (getEscapeCharacter().length() == 0 || name.indexOf(getEscapeCharacter()) == 0) { + if (getEscapeCharacter().length() == 0 + || name.indexOf(getEscapeCharacter()) == 0) { return getSqlNameStrategy().convert(name); } - return getEscapeCharacter() + getSqlNameStrategy().convert(name) + getEscapeCharacter(); + return getEscapeCharacter() + getSqlNameStrategy().convert(name) + + getEscapeCharacter(); + } + + /** + * @deprecated use {@link #trunc(PAnnotation, String, boolean)} + */ + @Deprecated + protected String trunc(String truncName, boolean truncSuffix) { + return trunc(null, truncName, truncSuffix); } }
\ No newline at end of file |