Author: adamw
Date: 2008-09-15 11:32:02 -0400 (Mon, 15 Sep 2008)
New Revision: 149
Added:
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/EntityXmlMappingData.java
trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java
Removed:
trunk/src/main/org/jboss/envers/entities/mapper/CompositePropertyMapperBuilder.java
Modified:
trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java
trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java
trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java
trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java
trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java
trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java
trunk/src/main/org/jboss/envers/query/criteria/CriteriaTools.java
trunk/src/main/org/jboss/envers/tools/reflection/ReflectionTools.java
Log:
ENVERS-41: breaking down VersionsMetadaGenerator to smaller classes
Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-03
09:54:06 UTC (rev 148)
+++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -25,7 +25,7 @@
import org.jboss.envers.configuration.metadata.VersionsMetadataGenerator;
import org.jboss.envers.configuration.metadata.PersistentClassVersioningData;
import org.jboss.envers.configuration.metadata.AnnotationsMetadataReader;
-import org.jboss.envers.configuration.metadata.EntityMappingData;
+import org.jboss.envers.configuration.metadata.EntityXmlMappingData;
import org.jboss.envers.tools.graph.GraphTopologicalSort;
import org.jboss.envers.tools.reflection.YReflectionManager;
import org.dom4j.io.DOMWriter;
@@ -62,7 +62,7 @@
Map<PersistentClass, PersistentClassVersioningData> pcDatas =
new HashMap<PersistentClass, PersistentClassVersioningData>();
- Map<PersistentClass, EntityMappingData> xmlMappings = new
HashMap<PersistentClass, EntityMappingData>();
+ Map<PersistentClass, EntityXmlMappingData> xmlMappings = new
HashMap<PersistentClass, EntityXmlMappingData>();
// First pass
AnnotationsMetadataReader annotationsMetadataReader = new
AnnotationsMetadataReader();
@@ -79,25 +79,24 @@
verEntCfg.addCustomVersionsTableName(pc.getEntityName(),
versioningData.versionsTableName);
}
- EntityMappingData mappingData = new EntityMappingData();
- versionsMetaGen.generateFirstPass(pc, versioningData, mappingData);
- xmlMappings.put(pc, mappingData);
+ EntityXmlMappingData xmlMappingData = new EntityXmlMappingData();
+ versionsMetaGen.generateFirstPass(pc, versioningData, xmlMappingData);
+ xmlMappings.put(pc, xmlMappingData);
}
}
// Second pass
for (PersistentClass pc : pcDatas.keySet()) {
- EntityMappingData mappingData = xmlMappings.get(pc);
+ EntityXmlMappingData xmlMappingData = xmlMappings.get(pc);
- versionsMetaGen.generateSecondPass(pc, pcDatas.get(pc), mappingData);
+ versionsMetaGen.generateSecondPass(pc, pcDatas.get(pc), xmlMappingData);
-
try {
- cfg.addDocument(writer.write(mappingData.getMainMapping()));
+ cfg.addDocument(writer.write(xmlMappingData.getMainXmlMapping()));
// TODO
- //writeDocument(mappingData.getMainMapping());
+ //writeDocument(xmlMappingData.getMainXmlMapping());
- for (Document additionalMapping : mappingData.getAdditionalMappings()) {
+ for (Document additionalMapping :
xmlMappingData.getAdditionalXmlMappings()) {
cfg.addDocument(writer.write(additionalMapping));
// TODO
//writeDocument(additionalMapping);
Modified: trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java 2008-09-03
09:54:06 UTC (rev 148)
+++ trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -64,5 +64,4 @@
public boolean isUnversionedOptimisticLockingField() {
return unversionedOptimisticLockingField;
}
-
}
Modified: trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-03
09:54:06 UTC (rev 148)
+++
trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -203,7 +203,7 @@
}
return new RevisionInfoConfigurationResult(
- revisionInfoGenerator, revisionPropType, revisionInfoXmlMapping,
+ revisionInfoGenerator, revisionInfoXmlMapping,
new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdName,
revisionInfoTimestampName),
generateRevisionInfoRelationMapping(),
new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdName),
revisionInfoEntityName);
@@ -212,19 +212,17 @@
class RevisionInfoConfigurationResult {
private final RevisionInfoGenerator revisionInfoGenerator;
- private final String revisionPropType;
private final Document revisionInfoXmlMapping;
private final RevisionInfoQueryCreator revisionInfoQueryCreator;
private final Element revisionInfoRelationMapping;
private final RevisionInfoNumberReader revisionInfoNumberReader;
private final String revisionInfoEntityName;
- RevisionInfoConfigurationResult(RevisionInfoGenerator revisionInfoGenerator, String
revisionPropType,
+ RevisionInfoConfigurationResult(RevisionInfoGenerator revisionInfoGenerator,
Document revisionInfoXmlMapping,
RevisionInfoQueryCreator revisionInfoQueryCreator,
Element revisionInfoRelationMapping,
RevisionInfoNumberReader revisionInfoNumberReader,
String revisionInfoEntityName) {
this.revisionInfoGenerator = revisionInfoGenerator;
- this.revisionPropType = revisionPropType;
this.revisionInfoXmlMapping = revisionInfoXmlMapping;
this.revisionInfoQueryCreator = revisionInfoQueryCreator;
this.revisionInfoRelationMapping = revisionInfoRelationMapping;
@@ -236,10 +234,6 @@
return revisionInfoGenerator;
}
- public String getRevisionPropType() {
- return revisionPropType;
- }
-
public Document getRevisionInfoXmlMapping() {
return revisionInfoXmlMapping;
}
Modified: trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-09-03
09:54:06 UTC (rev 148)
+++ trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -72,8 +72,7 @@
YReflectionManager reflectionManager = YReflectionManager.get(cfg);
RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration();
RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure(cfg,
reflectionManager);
- verEntCfg = new VersionsEntitiesConfiguration(properties,
revInfoCfgResult.getRevisionPropType(),
- revInfoCfgResult.getRevisionInfoEntityName());
+ verEntCfg = new VersionsEntitiesConfiguration(properties,
revInfoCfgResult.getRevisionInfoEntityName());
globalCfg = new GlobalConfiguration(properties);
versionsSyncManager = new
VersionsSyncManager(revInfoCfgResult.getRevisionInfoGenerator());
revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator();
Modified:
trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-03
09:54:06 UTC (rev 148)
+++
trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -34,11 +34,8 @@
private final String originalIdPropName;
private final String revisionPropName;
- private final String revisionPropType;
private final String revisionPropPath;
- private final String revisionEntityPath;
-
private final String revisionTypePropName;
private final String revisionTypePropType;
@@ -46,7 +43,7 @@
private final Map<String, String> customVersionsTablesNames;
- public VersionsEntitiesConfiguration(Properties properties, String revisionPropType,
String revisionInfoEntityName) {
+ public VersionsEntitiesConfiguration(Properties properties, String
revisionInfoEntityName) {
this.revisionInfoEntityName = revisionInfoEntityName;
versionsTablePrefix =
properties.getProperty("org.jboss.envers.versionsTablePrefix", "");
@@ -55,15 +52,13 @@
originalIdPropName = "originalId";
revisionPropName =
properties.getProperty("org.jboss.envers.revisionFieldName",
"_revision");
- this.revisionPropType = revisionPropType;
revisionTypePropName =
properties.getProperty("org.jboss.envers.revisionTypeFieldName",
"_revision_type");
revisionTypePropType = "byte";
customVersionsTablesNames = new HashMap<String, String>();
- revisionEntityPath = originalIdPropName + "." + revisionPropName;
- revisionPropPath = revisionEntityPath + ".id";
+ revisionPropPath = originalIdPropName + "." + revisionPropName +
".id";
}
public String getOriginalIdPropName() {
@@ -78,10 +73,6 @@
return revisionPropPath;
}
- public String getRevisionPropType() {
- return revisionPropType;
- }
-
public String getRevisionTypePropName() {
return revisionTypePropName;
}
@@ -90,10 +81,6 @@
return revisionTypePropType;
}
- public String getRevisionEntityPath() {
- return revisionEntityPath;
- }
-
public String getRevisionInfoEntityName() {
return revisionInfoEntityName;
}
Added: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -0,0 +1,108 @@
+package org.jboss.envers.configuration.metadata;
+
+import org.dom4j.Element;
+import org.hibernate.mapping.Value;
+import org.hibernate.mapping.Column;
+import org.hibernate.mapping.Component;
+import org.hibernate.mapping.Property;
+import org.hibernate.type.CustomType;
+import org.hibernate.util.StringHelper;
+import org.jboss.envers.entities.mapper.SimpleMapperBuilder;
+import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
+import org.jboss.envers.ModificationStore;
+import org.jboss.envers.exception.VersionsException;
+
+import java.util.Iterator;
+
+/**
+ * Generates metadata for basic properties: immutable types (including enums) and
components
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicMetadataGenerator {
+ private final VersionsMetadataGenerator mainGenerator;
+
+ BasicMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) {
+ mainGenerator = versionsMetadataGenerator;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder
mapper,
+ ModificationStore store, boolean key) {
+ Element prop_mapping = MetadataTools.addProperty(parent, name,
+ value.getType().getName(), key);
+ MetadataTools.addColumns(prop_mapping, (Iterator<Column>)
value.getColumnIterator());
+
+ // A null mapper means that we only want to add xml mappings (while building the
id mapping)
+ if (mapper != null) {
+ mapper.add(name, store);
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder
mapper,
+ ModificationStore store) {
+ Element prop_mapping = parent.addElement("property");
+ prop_mapping.addAttribute("name", name);
+
+ CustomType propertyType = (CustomType) value.getType();
+
+ Element type_mapping = prop_mapping.addElement("type");
+ type_mapping.addAttribute("name", propertyType.getName());
+
+ Element type_param1 = type_mapping.addElement("param");
+ type_param1.addAttribute("name", "enumClass");
+ type_param1.setText(propertyType.getReturnedClass().getName());
+
+ Element type_param2 = type_mapping.addElement("param");
+ type_param2.addAttribute("name", "type");
+ type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
+
+ MetadataTools.addColumns(prop_mapping, (Iterator<Column>)
value.getColumnIterator());
+
+ mapper.add(name, store);
+ }
+
+ private void addComponentClassName(Element any_mapping, Component comp) {
+ if (StringHelper.isNotEmpty(comp.getComponentClassName())) {
+ any_mapping.addAttribute("class", comp.getComponentClassName());
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ void addComponent(Element parent, String name, Value value, CompositeMapperBuilder
mapper,
+ String entityName, EntityXmlMappingData xmlMappingData,
+ boolean firstPass) {
+ Element component_mapping = null;
+ Component prop_component = (Component) value;
+
+ if (!firstPass) {
+ // The required element already exists.
+ Iterator<Element> iter =
parent.elementIterator("component");
+ while (iter.hasNext()) {
+ Element child = iter.next();
+ if (child.attribute("name").getText().equals(name)) {
+ component_mapping = child;
+ break;
+ }
+ }
+
+ if (component_mapping == null) {
+ throw new VersionsException("Element for component not found during
second pass!");
+ }
+ } else {
+ component_mapping = parent.addElement("component");
+ component_mapping.addAttribute("name", name);
+
+ addComponentClassName(component_mapping, prop_component);
+ }
+
+ CompositeMapperBuilder componentMapper = mapper.addComposite(name);
+
+ Iterator<Property> properties = (Iterator<Property>)
prop_component.getPropertyIterator();
+ while (properties.hasNext()) {
+ Property property = properties.next();
+ mainGenerator.addValue(component_mapping, property.getName(),
property.getValue(),
+ componentMapper, ModificationStore.FULL, entityName, xmlMappingData,
firstPass);
+ }
+ }
+}
Added:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -0,0 +1,204 @@
+package org.jboss.envers.configuration.metadata;
+
+import org.hibernate.mapping.*;
+import org.hibernate.mapping.Collection;
+import org.hibernate.MappingException;
+import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
+import org.jboss.envers.entities.mapper.relation.*;
+import org.jboss.envers.entities.mapper.id.IdMapper;
+import org.jboss.envers.entities.EntityConfiguration;
+import org.jboss.envers.entities.IdMappingData;
+import org.jboss.envers.tools.Tools;
+import org.jboss.envers.tools.StringTools;
+import org.dom4j.Element;
+
+import java.util.*;
+
+/**
+ * Generates metadata for collection-valued properties.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class CollectionMetadataGenerator {
+ private final VersionsMetadataGenerator mainGenerator;
+
+ CollectionMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) {
+ mainGenerator = versionsMetadataGenerator;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private String getMappedBy(Collection collectionValue) {
+ Iterator<Property> assocClassProps =
+ ((OneToMany)
collectionValue.getElement()).getAssociatedClass().getPropertyIterator();
+
+ while (assocClassProps.hasNext()) {
+ Property property = assocClassProps.next();
+
+ if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(),
+ collectionValue.getKey().getColumnIterator())) {
+ return property.getName();
+ }
+ }
+
+ return null;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ void addOneToManyAttached(String name, Value value, CompositeMapperBuilder mapper,
String entityName) {
+ Collection propertyValue = (Collection) value;
+
+ String owningReferencePropertyName = getMappedBy(propertyValue);
+ if (owningReferencePropertyName == null) {
+ throw new MappingException("Unable to read the mapped by attribute for
" + name);
+ }
+
+ EntityConfiguration configuration =
mainGenerator.getEntitiesConfigurations().get(entityName);
+ if (configuration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ }
+
+ IdMappingData referencingIdMapping = configuration.getIdMappingData();
+
+ String owningEntityName = ((OneToMany)
propertyValue.getElement()).getReferencedEntityName();
+ String lastPropertyPrefix = owningReferencePropertyName + "_";
+
+ // Generating the id mapper for the relation
+ IdMapper ownedIdMapper =
referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
+
+ // Storing information about this relation
+
mainGenerator.getEntitiesConfigurations().get(entityName).addOneToManyAttachedRelation(name,
owningReferencePropertyName,
+ owningEntityName, ownedIdMapper);
+
+ // Adding mapper for the id
+ mapper.addComposite(name, new
OneToManyAttachedMapper(owningReferencePropertyName, owningEntityName,
+ name));
+ }
+
+ private String getMiddleEntityName(String entityName, String referencedEntityName,
ManyToOne mto) {
+ return entityName + "_" + referencedEntityName + "_" +
mto.getTable().getName();
+ }
+
+ @SuppressWarnings({"unchecked"})
+ void addOneToManyDetached(String name, Value value, CompositeMapperBuilder mapper,
String entityName,
+ EntityXmlMappingData xmlMappingData) {
+ Collection propertyValue = (Collection) value;
+ ManyToOne mto = (ManyToOne) propertyValue.getElement();
+
+ String referencedEntityName = mto.getReferencedEntityName();
+
+ EntityConfiguration configuration =
mainGenerator.getEntitiesConfigurations().get(entityName);
+ if (configuration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ }
+
+ EntityConfiguration referencedConfiguration =
mainGenerator.getEntitiesConfigurations().get(referencedEntityName);
+ if (referencedConfiguration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
+ }
+
+ IdMappingData referencingIdMapping = configuration.getIdMappingData();
+ IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData();
+
+ String referencingPrefix = StringTools.getLastComponent(entityName) +
"_";
+ String referencedPrefix = name + "_";
+
+ // Name of the entity that will be used to store the relation between the two
entities.
+ String middleEntityName = getMiddleEntityName(entityName, referencedEntityName,
mto);
+ String versionsMiddleEntityName =
mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName);
+ String versionsMiddleTableName =
mainGenerator.getVerEntCfg().getVersionsTableName(middleEntityName,
mto.getTable().getName());
+
+ Element middleEntity =
MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(),
versionsMiddleEntityName,
+ versionsMiddleTableName, mto.getTable().getSchema(),
mto.getTable().getCatalog(), null);
+
+ Element middleEntityId = middleEntity.addElement("composite-id");
+ middleEntityId.addAttribute("name",
mainGenerator.getVerEntCfg().getOriginalIdPropName());
+
+ Iterator columnIterator = mto.getTable().getColumnIterator();
+
+ // Adding elements to the mapping corresponding to the referencing entity
id's
+ Element properties = (Element)
referencingIdMapping.getXmlRelationMapping().clone();
+ MetadataTools.prefixNamesInPropertyElement(properties, referencingPrefix,
columnIterator, true);
+ for (Element idProperty : (java.util.List<Element>) properties.elements())
{
+ middleEntityId.add((Element) idProperty.clone());
+ }
+
+ // Adding elements to the mapping corresponding to the referenced entity
id's
+ properties = (Element) referencedIdMapping.getXmlRelationMapping().clone();
+ MetadataTools.prefixNamesInPropertyElement(properties, referencedPrefix,
columnIterator, true);
+ for (Element idProperty : (java.util.List<Element>) properties.elements())
{
+ middleEntityId.add((Element) idProperty.clone());
+ }
+
+ mainGenerator.addRevisionInfoRelation(middleEntityId);
+ mainGenerator.addRevisionType(middleEntity);
+
+
mainGenerator.getEntitiesConfigurations().get(entityName).addOneToManyDetachedRelation(name,
referencedEntityName);
+
+ // Adding the property mapper
+ mapper.addComposite(name, new
OneToManyDetachedMapper(mainGenerator.getVerEntCfg(), entityName,
+ referencedEntityName, name,
mainGenerator.getVerEntCfg().getVersionsEntityName(referencedEntityName),
+ versionsMiddleEntityName,
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix),
+
referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix),
+
mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMapper()));
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private String getMappedBy(Table collectionTable, PersistentClass referencedClass) {
+ Iterator<Property> properties = referencedClass.getPropertyIterator();
+ while (properties.hasNext()) {
+ Property property = properties.next();
+ if (property.getValue() instanceof Collection) {
+ // The equality is intentional. We want to find a collection property
with the same collection table.
+ //noinspection ObjectEquality
+ if (((Collection) property.getValue()).getCollectionTable() ==
collectionTable) {
+ return property.getName();
+ }
+ }
+ }
+
+ return null;
+ }
+
+ void addManyToManyNotOwning(String name, Value value, CompositeMapperBuilder mapper,
String entityName) {
+ Collection propertyValue = (Collection) value;
+ ManyToOne mto = (ManyToOne) propertyValue.getElement();
+
+ String referencedEntityName = mto.getReferencedEntityName();
+
+ EntityConfiguration configuration =
mainGenerator.getEntitiesConfigurations().get(entityName);
+ if (configuration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ }
+
+ EntityConfiguration referencedConfiguration =
mainGenerator.getEntitiesConfigurations().get(referencedEntityName);
+ if (referencedConfiguration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
+ }
+
+ String mappedBy = getMappedBy(propertyValue.getCollectionTable(),
mainGenerator.getCfg().getClassMapping(referencedEntityName));
+ if (mappedBy == null) {
+ throw new MappingException("Unable to read the mapped by attribute for
" + name);
+ }
+
+ IdMappingData referencingIdMapping = configuration.getIdMappingData();
+ IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData();
+
+ String referencingPrefix = mappedBy + "_";
+ String referencedPrefix = StringTools.getLastComponent(referencedEntityName) +
"_";
+
+ // Name of the entity that will be used to store the relation between the two
entities.
+ // The order is inverse, as the referenced and referencing entity names are
swapped with respect to the
+ // name of the entity that was created on the owning side in
<code>addOneToManyDetached</code>.
+ String middleEntityName = getMiddleEntityName(referencedEntityName, entityName,
mto);
+ String versionsMiddleEntityName =
mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName);
+
+
mainGenerator.getEntitiesConfigurations().get(entityName).addManyToManyNotOwningRelation(name,
mappedBy,
+ referencedEntityName);
+
+ // Adding the property mapper
+ mapper.addComposite(name, new
ManyToManyNotOwningMapper(mainGenerator.getVerEntCfg(), entityName,
+ referencedEntityName, name,
mainGenerator.getVerEntCfg().getVersionsEntityName(referencedEntityName),
+ versionsMiddleEntityName,
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix),
+
referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix),
+
mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMapper()));
+ }
+}
Copied: trunk/src/main/org/jboss/envers/configuration/metadata/EntityXmlMappingData.java
(from rev 148,
trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java)
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/EntityXmlMappingData.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/EntityXmlMappingData.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -0,0 +1,58 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ *
+ * See the copyright.txt in the distribution for a full listing of individual
+ * contributors. This copyrighted material is made available to anyone wishing
+ * to use, modify, copy, or redistribute it subject to the terms and
+ * conditions of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT A WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.configuration.metadata;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class EntityXmlMappingData {
+ private Document mainXmlMapping;
+ private List<Document> additionalXmlMappings;
+
+ public EntityXmlMappingData() {
+ mainXmlMapping = DocumentHelper.createDocument();
+ additionalXmlMappings = new ArrayList<Document>();
+ }
+
+ public Document getMainXmlMapping() {
+ return mainXmlMapping;
+ }
+
+ public List<Document> getAdditionalXmlMappings() {
+ return additionalXmlMappings;
+ }
+
+ public Document newAdditionalMapping() {
+ Document additionalMapping = DocumentHelper.createDocument();
+ additionalXmlMappings.add(additionalMapping);
+
+ return additionalMapping;
+ }
+}
Added: trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -0,0 +1,91 @@
+package org.jboss.envers.configuration.metadata;
+
+import org.dom4j.Element;
+import org.dom4j.tree.DefaultElement;
+import org.hibernate.mapping.Property;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Component;
+import org.hibernate.type.Type;
+import org.hibernate.type.ImmutableType;
+import org.hibernate.MappingException;
+import org.jboss.envers.entities.mapper.SimpleMapperBuilder;
+import org.jboss.envers.entities.mapper.id.SimpleIdMapperBuilder;
+import org.jboss.envers.entities.mapper.id.MultipleIdMapper;
+import org.jboss.envers.entities.mapper.id.EmbeddedIdMapper;
+import org.jboss.envers.entities.mapper.id.SingleIdMapper;
+import org.jboss.envers.entities.IdMappingData;
+import org.jboss.envers.ModificationStore;
+
+import java.util.Iterator;
+
+/**
+ * Generates metadata for primary identifiers (ids) of versions entities.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class IdMetadataGenerator {
+ private final VersionsMetadataGenerator mainGenerator;
+
+ IdMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) {
+ mainGenerator = versionsMetadataGenerator;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addIdProperties(Element parent, Iterator<Property> properties,
SimpleMapperBuilder mapper, boolean key) {
+ while (properties.hasNext()) {
+ Property property = properties.next();
+ Type propertyType = property.getType();
+ if (!"_identifierMapper".equals(property.getName())) {
+ if (propertyType instanceof ImmutableType) {
+ mainGenerator.getBasicMetadataGenerator().addSimpleValue(parent,
property.getName(),
+ property.getValue(), mapper, ModificationStore.FULL, key);
+ } else {
+ throw new MappingException("Type not supported: " +
propertyType.getClass().getName());
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ IdMappingData addId(PersistentClass pc) {
+ // Xml mapping which will be used for relations
+ Element rel_id_mapping = new DefaultElement("properties");
+ // Xml mapping which will be used for the primary key of the versions table
+ Element orig_id_mapping = new DefaultElement("composite-id");
+
+ Property id_prop = pc.getIdentifierProperty();
+ Component id_mapper = pc.getIdentifierMapper();
+
+ SimpleIdMapperBuilder mapper;
+ if (id_mapper != null) {
+ mapper = new MultipleIdMapper(((Component)
pc.getIdentifier()).getComponentClassName());
+ addIdProperties(rel_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), mapper, false);
+
+ // null mapper - the mapping where already added the first time, now we only
want to generate the xml
+ addIdProperties(orig_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), null, true);
+ } else if (id_prop.isComposite()) {
+ Component id_component = (Component) id_prop.getValue();
+
+ mapper = new EmbeddedIdMapper(id_prop.getName(),
id_component.getComponentClassName());
+ addIdProperties(rel_id_mapping, (Iterator<Property>)
id_component.getPropertyIterator(), mapper, false);
+
+ // null mapper - the mapping where already added the first time, now we only
want to generate the xml
+ addIdProperties(orig_id_mapping, (Iterator<Property>)
id_component.getPropertyIterator(), null, true);
+ } else {
+ mapper = new SingleIdMapper();
+
+ mainGenerator.getBasicMetadataGenerator().addSimpleValue(rel_id_mapping,
id_prop.getName(),
+ id_prop.getValue(), mapper, ModificationStore.FULL, false);
+
+ // null mapper - the mapping where already added the first time, now we only
want to generate the xml
+ mainGenerator.getBasicMetadataGenerator().addSimpleValue(orig_id_mapping,
id_prop.getName(),
+ id_prop.getValue(), null, ModificationStore.FULL, true);
+ }
+
+ orig_id_mapping.addAttribute("name",
mainGenerator.getVerEntCfg().getOriginalIdPropName());
+
+ // Adding a relation to the revision entity (effectively: the "revision
number" property)
+ mainGenerator.addRevisionInfoRelation(orig_id_mapping);
+
+ return new IdMappingData(mapper, orig_id_mapping, rel_id_mapping);
+ }
+}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-03
09:54:06 UTC (rev 148)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -23,8 +23,12 @@
import org.dom4j.Element;
import org.dom4j.Document;
+import org.dom4j.Attribute;
import org.jboss.envers.tools.StringTools;
+import org.hibernate.mapping.Column;
+import java.util.Iterator;
+
/**
* @author Adam Warski (adam at warski dot org)
*/
@@ -126,4 +130,48 @@
return join_mapping;
}
+
+ public static void addColumns(Element any_mapping, Iterator<Column> columns) {
+ while (columns.hasNext()) {
+ Column column = columns.next();
+ addColumn(any_mapping, column.getName(), column.getLength());
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private static void changeNamesInColumnElement(Element element,
Iterator<Column> columnIterator) {
+ Iterator<Element> properties = element.elementIterator();
+ while (properties.hasNext()) {
+ Element property = properties.next();
+
+ if ("column".equals(property.getName())) {
+ Attribute nameAttr = property.attribute("name");
+ if (nameAttr != null) {
+ nameAttr.setText(columnIterator.next().getName());
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public static void prefixNamesInPropertyElement(Element element, String prefix,
Iterator<Column> columnIterator,
+ boolean changeToKey) {
+ Iterator<Element> properties = element.elementIterator();
+ while (properties.hasNext()) {
+ Element property = properties.next();
+
+ if ("property".equals(property.getName())) {
+ Attribute nameAttr = property.attribute("name");
+ if (nameAttr != null) {
+ nameAttr.setText(prefix + nameAttr.getText());
+ }
+
+ changeNamesInColumnElement(property, columnIterator);
+
+ if (changeToKey) {
+ property.setName("key-property");
+ }
+ }
+ }
+ }
}
Added:
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -0,0 +1,87 @@
+package org.jboss.envers.configuration.metadata;
+
+import org.dom4j.Element;
+import org.hibernate.mapping.Value;
+import org.hibernate.mapping.ToOne;
+import org.hibernate.mapping.OneToOne;
+import org.hibernate.MappingException;
+import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
+import org.jboss.envers.entities.mapper.relation.ToOneIdMapper;
+import org.jboss.envers.entities.mapper.relation.OneToOneNotOwningIdMapper;
+import org.jboss.envers.entities.mapper.id.IdMapper;
+import org.jboss.envers.entities.EntityConfiguration;
+import org.jboss.envers.entities.IdMappingData;
+
+/**
+ * Generates metadata for to-one relations (reference-valued properties).
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class ToOneRelationMetadataGenerator {
+ private final VersionsMetadataGenerator mainGenerator;
+
+ ToOneRelationMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator)
{
+ mainGenerator = versionsMetadataGenerator;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ void addToOne(Element parent, String name, Value value, CompositeMapperBuilder
mapper, String entityName) {
+ String referencedEntityName = ((ToOne) value).getReferencedEntityName();
+
+ EntityConfiguration configuration =
mainGenerator.getEntitiesConfigurations().get(referencedEntityName);
+ if (configuration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
+ }
+
+ IdMappingData idMapping = configuration.getIdMappingData();
+
+ String lastPropertyPrefix = name + "_";
+
+ // Generating the id mapper for the relation
+ IdMapper relMapper =
idMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
+
+ // Storing information about this relation
+ mainGenerator.getEntitiesConfigurations().get(entityName).addToOneRelation(name,
referencedEntityName, relMapper);
+
+ // Adding an element to the mapping corresponding to the references entity
id's
+ Element properties = (Element) idMapping.getXmlRelationMapping().clone();
+ properties.addAttribute("name", name);
+
+ MetadataTools.prefixNamesInPropertyElement(properties, lastPropertyPrefix,
value.getColumnIterator(), false);
+ parent.add(properties);
+
+ // Adding mapper for the id
+ mapper.addComposite(name, new ToOneIdMapper(relMapper, name,
referencedEntityName));
+ }
+
+ @SuppressWarnings({"unchecked"})
+ void addOneToOneNotOwning(String name, Value value, CompositeMapperBuilder mapper,
String entityName) {
+ OneToOne propertyValue = (OneToOne) value;
+
+ String owningReferencePropertyName = propertyValue.getReferencedPropertyName();
// mappedBy
+
+ EntityConfiguration configuration =
mainGenerator.getEntitiesConfigurations().get(entityName);
+ if (configuration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ }
+
+ IdMappingData ownedIdMapping = configuration.getIdMappingData();
+
+ if (ownedIdMapping == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ }
+
+ String lastPropertyPrefix = owningReferencePropertyName + "_";
+ String referencedEntityName = propertyValue.getReferencedEntityName();
+
+ // Generating the id mapper for the relation
+ IdMapper ownedIdMapper =
ownedIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
+
+ // Storing information about this relation
+
mainGenerator.getEntitiesConfigurations().get(entityName).addOneToOneNotOwningRelation(name,
owningReferencePropertyName,
+ referencedEntityName, ownedIdMapper);
+
+ // Adding mapper for the id
+ mapper.addComposite(name, new
OneToOneNotOwningIdMapper(owningReferencePropertyName,
+ referencedEntityName, name));
+ }
+}
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-03
09:54:06 UTC (rev 148)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -22,28 +22,21 @@
package org.jboss.envers.configuration.metadata;
import org.hibernate.type.*;
-import org.hibernate.util.StringHelper;
import org.hibernate.MappingException;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.*;
import org.hibernate.mapping.Collection;
import org.dom4j.Element;
-import org.dom4j.Attribute;
-import org.dom4j.tree.DefaultElement;
import org.jboss.envers.entities.EntityConfiguration;
import org.jboss.envers.entities.IdMappingData;
import org.jboss.envers.configuration.GlobalConfiguration;
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
import org.jboss.envers.entities.mapper.*;
-import org.jboss.envers.entities.mapper.id.*;
-import org.jboss.envers.entities.mapper.relation.*;
import org.jboss.envers.tools.StringTools;
-import org.jboss.envers.tools.Tools;
import org.jboss.envers.tools.HibernateVersion;
import org.jboss.envers.ModificationStore;
import org.jboss.envers.tools.log.YLog;
import org.jboss.envers.tools.log.YLogManager;
-import org.jboss.envers.exception.VersionsException;
import java.util.*;
import java.util.List;
@@ -54,13 +47,16 @@
* @author Sebastian Komander
*/
public class VersionsMetadataGenerator {
- private final static Map<String, ModificationStore> EMPTY_STORE =
Collections.emptyMap();
-
private final Configuration cfg;
private final GlobalConfiguration globalCfg;
private final VersionsEntitiesConfiguration verEntCfg;
private final Element revisionInfoRelationMapping;
+ private final BasicMetadataGenerator basicMetadataGenerator;
+ private final IdMetadataGenerator idMetadataGenerator;
+ private final CollectionMetadataGenerator collectionMetadataGenerator;
+ private final ToOneRelationMetadataGenerator toOneRelationMetadataGenerator;
+
private Map<String, EntityConfiguration> entitiesConfigurations;
// Map entity name -> (join descriptor -> element describing the
"versioned" join)
@@ -76,24 +72,16 @@
this.verEntCfg = verEntCfg;
this.revisionInfoRelationMapping = revisionInfoRelationMapping;
+ this.basicMetadataGenerator = new BasicMetadataGenerator(this);
+ this.idMetadataGenerator = new IdMetadataGenerator(this);
+ this.collectionMetadataGenerator = new CollectionMetadataGenerator(this);
+ this.toOneRelationMetadataGenerator = new ToOneRelationMetadataGenerator(this);
+
entitiesConfigurations = new HashMap<String, EntityConfiguration>();
entitiesJoins = new HashMap<String, Map<Join, Element>>();
}
- private void addComponentClassName(Element any_mapping, Component comp) {
- if (StringHelper.isNotEmpty(comp.getComponentClassName())) {
- any_mapping.addAttribute("class", comp.getComponentClassName());
- }
- }
-
- private void addColumns(Element any_mapping, Iterator<Column> columns) {
- while (columns.hasNext()) {
- Column column = columns.next();
- MetadataTools.addColumn(any_mapping, column.getName(), column.getLength());
- }
- }
-
- private void addRevisionInfoRelation(Element any_mapping) {
+ void addRevisionInfoRelation(Element any_mapping) {
Element rev_mapping = (Element) revisionInfoRelationMapping.clone();
rev_mapping.addAttribute("name", verEntCfg.getRevisionPropName());
MetadataTools.addColumn(rev_mapping, verEntCfg.getRevisionPropName(), null);
@@ -101,361 +89,12 @@
any_mapping.add(rev_mapping);
}
- private void addRevisionType(Element any_mapping) {
+ void addRevisionType(Element any_mapping) {
Element revTypeProperty = MetadataTools.addProperty(any_mapping,
verEntCfg.getRevisionTypePropName(),
verEntCfg.getRevisionTypePropType(), false);
revTypeProperty.addAttribute("type",
"org.jboss.envers.entities.RevisionTypeType");
}
- @SuppressWarnings({"unchecked"})
- private void addSimpleProperty(Element parent, Property property, SimpleMapperBuilder
mapper,
- ModificationStore store, boolean key) {
- Element prop_mapping = MetadataTools.addProperty(parent, property.getName(),
- property.getType().getName(), key);
- addColumns(prop_mapping, (Iterator<Column>) property.getColumnIterator());
-
- // A null mapper means that we only want to add xml mappings (while building the
id mapping)
- if (mapper != null) {
- mapper.add(property.getName(), store);
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void addEnumProperty(Element parent, Property property, SimpleMapperBuilder
mapper,
- ModificationStore store) {
- Element prop_mapping = parent.addElement("property");
- prop_mapping.addAttribute("name", property.getName());
-
- CustomType propertyType = (CustomType) property.getType();
-
- Element type_mapping = prop_mapping.addElement("type");
- type_mapping.addAttribute("name", propertyType.getName());
-
- Element type_param1 = type_mapping.addElement("param");
- type_param1.addAttribute("name", "enumClass");
- type_param1.setText(propertyType.getReturnedClass().getName());
-
- Element type_param2 = type_mapping.addElement("param");
- type_param2.addAttribute("name", "type");
- type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
-
- addColumns(prop_mapping, (Iterator<Column>) property.getColumnIterator());
-
- mapper.add(property.getName(), store);
- }
-
- @SuppressWarnings({"unchecked"})
- private void addComponent(Element parent, Property property, CompositeMapperBuilder
mapper, ModificationStore store,
- String entityName, EntityMappingData mappingData,
List<String> unversionedProperties,
- boolean firstPass) {
- Element component_mapping = null;
- Component prop_component = (Component) property.getValue();
-
- if (!firstPass) {
- // The required element already exists.
- Iterator<Element> iter =
parent.elementIterator("component");
- while (iter.hasNext()) {
- Element child = iter.next();
- if
(child.attribute("name").getText().equals(property.getName())) {
- component_mapping = child;
- break;
- }
- }
-
- if (component_mapping == null) {
- throw new VersionsException("Element for component not found during
second pass!");
- }
- } else {
- component_mapping = parent.addElement("component");
- component_mapping.addAttribute("name", property.getName());
-
- addComponentClassName(component_mapping, prop_component);
- }
-
- addProperties(component_mapping, (Iterator<Property>)
prop_component.getPropertyIterator(),
- mapper.addComposite(property.getName()), new PropertyStoreInfo(store,
EMPTY_STORE), entityName,
- mappingData, unversionedProperties, firstPass);
- }
-
- @SuppressWarnings({"unchecked"})
- private void changeNamesInColumnElement(Element element, Iterator<Column>
columnIterator) {
- Iterator<Element> properties = element.elementIterator();
- while (properties.hasNext()) {
- Element property = properties.next();
-
- if ("column".equals(property.getName())) {
- Attribute nameAttr = property.attribute("name");
- if (nameAttr != null) {
- nameAttr.setText(columnIterator.next().getName());
- }
- }
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void prefixNamesInPropertyElement(Element element, String prefix,
Iterator<Column> columnIterator,
- boolean changeToKey) {
- Iterator<Element> properties = element.elementIterator();
- while (properties.hasNext()) {
- Element property = properties.next();
-
- if ("property".equals(property.getName())) {
- Attribute nameAttr = property.attribute("name");
- if (nameAttr != null) {
- nameAttr.setText(prefix + nameAttr.getText());
- }
-
- changeNamesInColumnElement(property, columnIterator);
-
- if (changeToKey) {
- property.setName("key-property");
- }
- }
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void addToOne(Element parent, Property property, CompositeMapperBuilder
mapper, String entityName) {
- String referencedEntityName = ((ToOne)
property.getValue()).getReferencedEntityName();
-
- EntityConfiguration configuration =
entitiesConfigurations.get(referencedEntityName);
- if (configuration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
- }
-
- IdMappingData idMapping = configuration.getIdMappingData();
-
- String propertyName = property.getName();
- String lastPropertyPrefix = propertyName + "_";
-
- // Generating the id mapper for the relation
- IdMapper relMapper =
idMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
-
- // Storing information about this relation
- entitiesConfigurations.get(entityName).addToOneRelation(propertyName,
referencedEntityName, relMapper);
-
- // Adding an element to the mapping corresponding to the references entity
id's
- Element properties = (Element) idMapping.getXmlRelationMapping().clone();
- properties.addAttribute("name", propertyName);
-
- prefixNamesInPropertyElement(properties, lastPropertyPrefix,
property.getValue().getColumnIterator(), false);
- parent.add(properties);
-
- // Adding mapper for the id
- mapper.addComposite(propertyName, new ToOneIdMapper(relMapper, propertyName,
referencedEntityName));
- }
-
- @SuppressWarnings({"unchecked"})
- private void addOneToOneNotOwning(Property property, CompositeMapperBuilder mapper,
String entityName) {
- OneToOne propertyValue = (OneToOne) property.getValue();
-
- String owningReferencePropertyName = propertyValue.getReferencedPropertyName();
// mappedBy
-
- EntityConfiguration configuration = entitiesConfigurations.get(entityName);
- if (configuration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
- }
-
- IdMappingData ownedIdMapping = configuration.getIdMappingData();
-
- if (ownedIdMapping == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
- }
-
- String propertyName = property.getName();
- String lastPropertyPrefix = owningReferencePropertyName + "_";
- String referencedEntityName = propertyValue.getReferencedEntityName();
-
- // Generating the id mapper for the relation
- IdMapper ownedIdMapper =
ownedIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
-
- // Storing information about this relation
- entitiesConfigurations.get(entityName).addOneToOneNotOwningRelation(propertyName,
owningReferencePropertyName,
- referencedEntityName, ownedIdMapper);
-
- // Adding mapper for the id
- mapper.addComposite(propertyName, new
OneToOneNotOwningIdMapper(owningReferencePropertyName,
- referencedEntityName, propertyName));
- }
-
- @SuppressWarnings({"unchecked"})
- private String getMappedBy(Collection collectionValue) {
- Iterator<Property> assocClassProps =
- ((OneToMany)
collectionValue.getElement()).getAssociatedClass().getPropertyIterator();
-
- while (assocClassProps.hasNext()) {
- Property property = assocClassProps.next();
-
- if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(),
- collectionValue.getKey().getColumnIterator())) {
- return property.getName();
- }
- }
-
- return null;
- }
-
- @SuppressWarnings({"unchecked"})
- private void addOneToManyAttached(Property property, CompositeMapperBuilder mapper,
String entityName) {
- Collection propertyValue = (Collection) property.getValue();
-
- String owningReferencePropertyName = getMappedBy(propertyValue);
- if (owningReferencePropertyName == null) {
- throw new MappingException("Unable to read the mapped by attribute for
" + property.getName());
- }
-
- EntityConfiguration configuration = entitiesConfigurations.get(entityName);
- if (configuration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
- }
-
- IdMappingData referencingIdMapping = configuration.getIdMappingData();
-
- String owningEntityName = ((OneToMany)
propertyValue.getElement()).getReferencedEntityName();
- String propertyName = property.getName();
- String lastPropertyPrefix = owningReferencePropertyName + "_";
-
- // Generating the id mapper for the relation
- IdMapper ownedIdMapper =
referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
-
- // Storing information about this relation
- entitiesConfigurations.get(entityName).addOneToManyAttachedRelation(propertyName,
owningReferencePropertyName,
- owningEntityName, ownedIdMapper);
-
- // Adding mapper for the id
- mapper.addComposite(propertyName, new
OneToManyAttachedMapper(owningReferencePropertyName, owningEntityName,
- propertyName));
- }
-
- private String getMiddleEntityName(String entityName, String referencedEntityName,
ManyToOne mto) {
- return entityName + "_" + referencedEntityName + "_" +
mto.getTable().getName();
- }
-
- @SuppressWarnings({"unchecked"})
- private void addOneToManyDetached(Property property, CompositeMapperBuilder mapper,
String entityName,
- EntityMappingData mappingData) {
- Collection propertyValue = (Collection) property.getValue();
- ManyToOne mto = (ManyToOne) propertyValue.getElement();
-
- String referencedEntityName = mto.getReferencedEntityName();
-
- EntityConfiguration configuration = entitiesConfigurations.get(entityName);
- if (configuration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
- }
-
- EntityConfiguration referencedConfiguration =
entitiesConfigurations.get(referencedEntityName);
- if (referencedConfiguration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
- }
-
- IdMappingData referencingIdMapping = configuration.getIdMappingData();
- IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData();
-
- String referencingPrefix = StringTools.getLastComponent(entityName) +
"_";
- String referencedPrefix = property.getName() + "_";
-
- // Name of the entity that will be used to store the relation between the two
entities.
- String middleEntityName = getMiddleEntityName(entityName, referencedEntityName,
mto);
- String versionsMiddleEntityName =
verEntCfg.getVersionsEntityName(middleEntityName);
- String versionsMiddleTableName = verEntCfg.getVersionsTableName(middleEntityName,
mto.getTable().getName());
-
- Element middleEntity =
MetadataTools.createEntity(mappingData.newAdditionalMapping(), versionsMiddleEntityName,
- versionsMiddleTableName, mto.getTable().getSchema(),
mto.getTable().getCatalog(), null);
-
- Element middleEntityId = middleEntity.addElement("composite-id");
- middleEntityId.addAttribute("name",
verEntCfg.getOriginalIdPropName());
-
- Iterator columnIterator = mto.getTable().getColumnIterator();
-
- // Adding elements to the mapping corresponding to the referencing entity
id's
- Element properties = (Element)
referencingIdMapping.getXmlRelationMapping().clone();
- prefixNamesInPropertyElement(properties, referencingPrefix, columnIterator,
true);
- for (Element idProperty : (List<Element>) properties.elements()) {
- middleEntityId.add((Element) idProperty.clone());
- }
-
- // Adding elements to the mapping corresponding to the referenced entity
id's
- properties = (Element) referencedIdMapping.getXmlRelationMapping().clone();
- prefixNamesInPropertyElement(properties, referencedPrefix, columnIterator,
true);
- for (Element idProperty : (List<Element>) properties.elements()) {
- middleEntityId.add((Element) idProperty.clone());
- }
-
- addRevisionInfoRelation(middleEntityId);
- addRevisionType(middleEntity);
-
-
entitiesConfigurations.get(entityName).addOneToManyDetachedRelation(property.getName(),
referencedEntityName);
-
- // Adding the property mapper
- mapper.addComposite(property.getName(), new OneToManyDetachedMapper(verEntCfg,
entityName,
- referencedEntityName, property.getName(),
verEntCfg.getVersionsEntityName(referencedEntityName),
- versionsMiddleEntityName,
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix),
-
referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix),
- entitiesConfigurations.get(referencedEntityName).getIdMapper()));
- }
-
- @SuppressWarnings({"unchecked"})
- private String getMappedBy(Table collectionTable, PersistentClass referencedClass) {
- Iterator<Property> properties = referencedClass.getPropertyIterator();
- while (properties.hasNext()) {
- Property property = properties.next();
- if (property.getValue() instanceof Collection) {
- // The equality is intentional. We want to find a collection property
with the same collection table.
- //noinspection ObjectEquality
- if (((Collection) property.getValue()).getCollectionTable() ==
collectionTable) {
- return property.getName();
- }
- }
- }
-
- return null;
- }
-
- private void addManyToManyNotOwning(Property property, CompositeMapperBuilder mapper,
String entityName) {
- Collection propertyValue = (Collection) property.getValue();
- ManyToOne mto = (ManyToOne) propertyValue.getElement();
-
- String referencedEntityName = mto.getReferencedEntityName();
-
- EntityConfiguration configuration = entitiesConfigurations.get(entityName);
- if (configuration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
- }
-
- EntityConfiguration referencedConfiguration =
entitiesConfigurations.get(referencedEntityName);
- if (referencedConfiguration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
- }
-
- String mappedBy = getMappedBy(propertyValue.getCollectionTable(),
cfg.getClassMapping(referencedEntityName));
- if (mappedBy == null) {
- throw new MappingException("Unable to read the mapped by attribute for
" + property.getName());
- }
-
- IdMappingData referencingIdMapping = configuration.getIdMappingData();
- IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData();
-
- String referencingPrefix = mappedBy + "_";
- String referencedPrefix = StringTools.getLastComponent(referencedEntityName) +
"_";
-
- // Name of the entity that will be used to store the relation between the two
entities.
- // The order is inverse, as the referenced and referencing entity names are
swapped with respect to the
- // name of the entity that was created on the owning side in
<code>addOneToManyDetached</code>.
- String middleEntityName = getMiddleEntityName(referencedEntityName, entityName,
mto);
- String versionsMiddleEntityName =
verEntCfg.getVersionsEntityName(middleEntityName);
-
-
entitiesConfigurations.get(entityName).addManyToManyNotOwningRelation(property.getName(),
mappedBy,
- referencedEntityName);
-
- // Adding the property mapper
- mapper.addComposite(property.getName(), new ManyToManyNotOwningMapper(verEntCfg,
entityName,
- referencedEntityName, property.getName(),
verEntCfg.getVersionsEntityName(referencedEntityName),
- versionsMiddleEntityName,
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix),
-
referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix),
- entitiesConfigurations.get(referencedEntityName).getIdMapper()));
- }
-
private ModificationStore getStoreForProperty(Property property, PropertyStoreInfo
propertyStoreInfo,
List<String>
unversionedProperties) {
/*
@@ -479,100 +118,93 @@
}
@SuppressWarnings({"unchecked"})
- private void addIdProperties(Element parent, Iterator<Property> properties,
SimpleMapperBuilder mapper, boolean key) {
- while (properties.hasNext()) {
- Property property = properties.next();
- Type propertyType = property.getType();
- if (!"_identifierMapper".equals(property.getName())) {
- if (propertyType instanceof ImmutableType) {
- addSimpleProperty(parent, property, mapper, ModificationStore.FULL,
key);
- } else {
- throw new MappingException("Type not supported: " +
propertyType.getClass().getName());
- }
+ void addValue(Element parent, String name, Value value, CompositeMapperBuilder
currentMapper,
+ ModificationStore store, String entityName,
EntityXmlMappingData xmlMappingData,
+ boolean firstPass) {
+ Type type = value.getType();
+
+ if (type instanceof ComponentType) {
+ // only first pass
+ if (firstPass) {
+ basicMetadataGenerator.addComponent(parent, name, value, currentMapper,
entityName, xmlMappingData, firstPass);
}
+ } else if (type instanceof ImmutableType || type instanceof MutableType) {
+ // only first pass
+ if (firstPass) {
+ basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper,
store, false);
+ }
+ } else if (type instanceof CustomType &&
+ "org.hibernate.type.EnumType".equals(type.getName())) {
+ // only first pass
+ if (firstPass) {
+ basicMetadataGenerator.addEnumValue(parent, name, value, currentMapper,
store);
+ }
+ } else if (type instanceof ManyToOneType) {
+ // only second pass
+ if (!firstPass) {
+ toOneRelationMetadataGenerator.addToOne(parent, name, value,
currentMapper, entityName);
+ }
+ } else if (type instanceof OneToOneType) {
+ // only second pass
+ if (!firstPass) {
+ toOneRelationMetadataGenerator.addOneToOneNotOwning(name, value,
currentMapper, entityName);
+ }
+ } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(
+ type.getClass().getName())) {
+ // only first pass
+ if (firstPass) {
+ basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper,
store, false);
+ }
+ } else if (type instanceof CustomType &&
+
("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) ||
+
"org.hibernate.type.StringClobType".equals(type.getName()))) {
+ // only first pass
+ if (firstPass) {
+ basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper,
store, false);
+ }
+ } else if ((type instanceof BagType || type instanceof SetType) &&
+ ((((Collection) value).getElement() instanceof OneToMany))) {
+ // only second pass
+ if (!firstPass) {
+ collectionMetadataGenerator.addOneToManyAttached(name, value,
currentMapper, entityName);
+ }
+ } else if ((type instanceof BagType || type instanceof SetType) &&
+ ((((Collection) value).getElement() instanceof ManyToOne)) &&
+ !((Collection) value).isInverse()) {
+ // only second pass
+ if (!firstPass) {
+ collectionMetadataGenerator.addOneToManyDetached(name, value,
currentMapper, entityName, xmlMappingData);
+ }
+ } else if ((type instanceof BagType || type instanceof SetType) &&
+ ((((Collection) value).getElement() instanceof ManyToOne)) &&
+ ((Collection) value).isInverse()) {
+ // only second pass
+ if (!firstPass) {
+ collectionMetadataGenerator.addManyToManyNotOwning(name, value,
currentMapper, entityName);
+ }
+ } else {
+ String message = "Type not supported for versioning: " +
type.getClass().getName() +
+ ", on entity " + entityName + ", property '"
+ name + "'.";
+ if (globalCfg.isWarnOnUnsupportedTypes()) {
+ log.warn(message);
+ } else {
+ throw new MappingException(message);
+ }
}
}
@SuppressWarnings({"unchecked"})
private void addProperties(Element parent, Iterator<Property> properties,
CompositeMapperBuilder currentMapper,
- PropertyStoreInfo propertyStoreInfo, String entityName,
EntityMappingData mappingData,
+ PropertyStoreInfo propertyStoreInfo, String entityName,
EntityXmlMappingData xmlMappingData,
List<String> unversionedProperties, boolean
firstPass) {
while (properties.hasNext()) {
Property property = properties.next();
- Type propertyType = property.getType();
if (!"_identifierMapper".equals(property.getName())) {
ModificationStore store = getStoreForProperty(property,
propertyStoreInfo, unversionedProperties);
- if (store != null) {
- if (propertyType instanceof ComponentType) {
- // only first pass
- if (firstPass) {
- addComponent(parent, property, currentMapper, store,
entityName, mappingData,
- unversionedProperties, firstPass);
- }
- } else if (propertyType instanceof ImmutableType || propertyType
instanceof MutableType) {
- // only first pass
- if (firstPass) {
- addSimpleProperty(parent, property, currentMapper, store,
false);
- }
- } else if (propertyType instanceof CustomType &&
-
"org.hibernate.type.EnumType".equals(propertyType.getName())) {
- // only first pass
- if (firstPass) {
- addEnumProperty(parent, property, currentMapper, store);
- }
- } else if (propertyType instanceof ManyToOneType) {
- // only second pass
- if (!firstPass) {
- addToOne(parent, property, currentMapper, entityName);
- }
- } else if (propertyType instanceof OneToOneType) {
- // only second pass
- if (!firstPass) {
- addOneToOneNotOwning(property, currentMapper, entityName);
- }
- } else if
("org.hibernate.type.PrimitiveByteArrayBlobType".equals(
- propertyType.getClass().getName())) {
- // only first pass
- if (firstPass) {
- addSimpleProperty(parent, property, currentMapper, store,
false);
- }
- } else if (propertyType instanceof CustomType &&
-
("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(propertyType.getName())
||
-
"org.hibernate.type.StringClobType".equals(propertyType.getName()))) {
- // only first pass
- if (firstPass) {
- addSimpleProperty(parent, property, currentMapper, store,
false);
- }
- } else if ((propertyType instanceof BagType || propertyType
instanceof SetType) &&
- ((((Collection) property.getValue()).getElement() instanceof
OneToMany))) {
- // only second pass
- if (!firstPass) {
- addOneToManyAttached(property, currentMapper, entityName);
- }
- } else if ((propertyType instanceof BagType || propertyType
instanceof SetType) &&
- ((((Collection) property.getValue()).getElement() instanceof
ManyToOne)) &&
- !((Collection) property.getValue()).isInverse()) {
- // only second pass
- if (!firstPass) {
- addOneToManyDetached(property, currentMapper, entityName,
mappingData);
- }
- } else if ((propertyType instanceof BagType || propertyType
instanceof SetType) &&
- ((((Collection) property.getValue()).getElement() instanceof
ManyToOne)) &&
- ((Collection) property.getValue()).isInverse()) {
- // only second pass
- if (!firstPass) {
- addManyToManyNotOwning(property, currentMapper, entityName);
- }
- } else {
- String message = "Type not supported for versioning: "
+ propertyType.getClass().getName() +
- ", on entity " + entityName + ", property
'" + property.getName() + "'.";
- if (globalCfg.isWarnOnUnsupportedTypes()) {
- log.warn(message);
- } else {
- throw new MappingException(message);
- }
- }
+ if (store != null) {
+ addValue(parent, property.getName(), property.getValue(),
currentMapper, store, entityName,
+ xmlMappingData, firstPass);
}
}
}
@@ -610,14 +242,14 @@
joinElements.put(join, joinElement);
Element joinKey = joinElement.addElement("key");
- addColumns(joinKey, join.getKey().getColumnIterator());
+ MetadataTools.addColumns(joinKey, join.getKey().getColumnIterator());
MetadataTools.addColumn(joinKey, verEntCfg.getRevisionPropName(), null);
}
}
@SuppressWarnings({"unchecked"})
private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper,
PropertyStoreInfo propertyStoreInfo,
- String entityName, EntityMappingData mappingData,
List<String> unversionedProperties,
+ String entityName, EntityXmlMappingData xmlMappingData,
List<String> unversionedProperties,
boolean firstPass) {
Iterator<Join> joins = pc.getJoinIterator();
@@ -626,52 +258,12 @@
Element joinElement = entitiesJoins.get(entityName).get(join);
addProperties(joinElement, join.getPropertyIterator(), currentMapper,
propertyStoreInfo, entityName,
- mappingData, unversionedProperties, firstPass);
+ xmlMappingData, unversionedProperties, firstPass);
}
}
- @SuppressWarnings({"unchecked"})
- private IdMappingData addId(PersistentClass pc) {
- // Xml mapping which will be used for relations
- Element rel_id_mapping = new DefaultElement("properties");
- // Xml mapping which will be used for the primary key of the versions table
- Element orig_id_mapping = new DefaultElement("composite-id");
- Property id_prop = pc.getIdentifierProperty();
- Component id_mapper = pc.getIdentifierMapper();
- SimpleIdMapperBuilder mapper;
- if (id_mapper != null) {
- mapper = new MultipleIdMapper(((Component)
pc.getIdentifier()).getComponentClassName());
- addIdProperties(rel_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), mapper, false);
-
- // null mapper - the mapping where already added the first time, now we only
want to generate the xml
- addIdProperties(orig_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), null, true);
- } else if (id_prop.isComposite()) {
- Component id_component = (Component) id_prop.getValue();
-
- mapper = new EmbeddedIdMapper(id_prop.getName(),
id_component.getComponentClassName());
- addIdProperties(rel_id_mapping, (Iterator<Property>)
id_component.getPropertyIterator(), mapper, false);
-
- // null mapper - the mapping where already added the first time, now we only
want to generate the xml
- addIdProperties(orig_id_mapping, (Iterator<Property>)
id_component.getPropertyIterator(), null, true);
- } else {
- mapper = new SingleIdMapper();
-
- addSimpleProperty(rel_id_mapping, id_prop, mapper, ModificationStore.FULL,
false);
-
- // null mapper - the mapping where already added the first time, now we only
want to generate the xml
- addSimpleProperty(orig_id_mapping, id_prop, null, ModificationStore.FULL,
true);
- }
-
- orig_id_mapping.addAttribute("name",
verEntCfg.getOriginalIdPropName());
-
- // Adding a relation to the revision entity (effectively: the "revision
number" property)
- addRevisionInfoRelation(orig_id_mapping);
-
- return new IdMappingData(mapper, orig_id_mapping, rel_id_mapping);
- }
-
private void addPersisterHack(Element class_mapping) {
String persisterClassName;
if (HibernateVersion.get().startsWith("3.3")) {
@@ -685,7 +277,7 @@
@SuppressWarnings({"unchecked"})
public void generateFirstPass(PersistentClass pc, PersistentClassVersioningData
versioningData,
- EntityMappingData mappingData) {
+ EntityXmlMappingData xmlMappingData) {
String schema = versioningData.schema;
if (StringTools.isEmpty(schema)) {
schema = pc.getTable().getSchema();
@@ -701,7 +293,7 @@
String versionsTableName = verEntCfg.getVersionsTableName(entityName,
pc.getTable().getName());
// Generating a mapping for the id
- IdMappingData idMapper = addId(pc);
+ IdMappingData idMapper = idMetadataGenerator.addId(pc);
Element class_mapping;
ExtendedPropertyMapper propertyMapper;
@@ -711,14 +303,14 @@
switch (inheritanceType) {
case NONE:
- class_mapping = MetadataTools.createEntity(mappingData.getMainMapping(),
versionsEntityName, versionsTableName,
+ class_mapping =
MetadataTools.createEntity(xmlMappingData.getMainXmlMapping(), versionsEntityName,
versionsTableName,
schema, catalog, pc.getDiscriminatorValue());
propertyMapper = new MultiPropertyMapper();
// Checking if there is a discriminator column
if (pc.getDiscriminator() != null) {
Element discriminator_element =
class_mapping.addElement("discriminator");
- addColumns(discriminator_element,
pc.getDiscriminator().getColumnIterator());
+ MetadataTools.addColumns(discriminator_element,
pc.getDiscriminator().getColumnIterator());
discriminator_element.addAttribute("type",
pc.getDiscriminator().getType().getName());
// If so, there is some inheritance scheme -> using the persister
hack.
@@ -734,7 +326,7 @@
break;
case SINGLE:
String extendsEntityName =
verEntCfg.getVersionsEntityName(pc.getSuperclass().getEntityName());
- class_mapping =
MetadataTools.createSubclassEntity(mappingData.getMainMapping(), versionsEntityName,
+ class_mapping =
MetadataTools.createSubclassEntity(xmlMappingData.getMainXmlMapping(),
versionsEntityName,
versionsTableName, schema, catalog, extendsEntityName,
pc.getDiscriminatorValue());
addPersisterHack(class_mapping);
@@ -757,12 +349,12 @@
// Mapping unjoined properties
addProperties(class_mapping, (Iterator<Property>)
pc.getUnjoinedPropertyIterator(), propertyMapper,
- versioningData.propertyStoreInfo, pc.getEntityName(), mappingData,
versioningData.unversionedProperties,
+ versioningData.propertyStoreInfo, pc.getEntityName(), xmlMappingData,
versioningData.unversionedProperties,
true);
// Creating and mapping joins (first pass)
createJoins(pc, class_mapping, versioningData);
- addJoins(pc, propertyMapper, versioningData.propertyStoreInfo,
pc.getEntityName(), mappingData,
+ addJoins(pc, propertyMapper, versioningData.propertyStoreInfo,
pc.getEntityName(), xmlMappingData,
versioningData.unversionedProperties, true);
// Storing the generated configuration
@@ -773,27 +365,57 @@
@SuppressWarnings({"unchecked"})
public void generateSecondPass(PersistentClass pc, PersistentClassVersioningData
versioningData,
- EntityMappingData mappingData) {
+ EntityXmlMappingData xmlMappingData) {
String entityName = pc.getEntityName();
CompositeMapperBuilder propertyMapper =
entitiesConfigurations.get(entityName).getPropertyMapper();
// Mapping unjoined properties
- Element parent =
mappingData.getMainMapping().getRootElement().element("class");
+ Element parent =
xmlMappingData.getMainXmlMapping().getRootElement().element("class");
if (parent == null) {
- parent =
mappingData.getMainMapping().getRootElement().element("subclass");
+ parent =
xmlMappingData.getMainXmlMapping().getRootElement().element("subclass");
}
addProperties(parent, (Iterator<Property>)
pc.getUnjoinedPropertyIterator(),
- propertyMapper, versioningData.propertyStoreInfo, entityName,
mappingData,
+ propertyMapper, versioningData.propertyStoreInfo, entityName,
xmlMappingData,
versioningData.unversionedProperties, false);
// Mapping joins (second pass)
- addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName,
mappingData,
+ addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName,
xmlMappingData,
versioningData.unversionedProperties, false);
}
public Map<String, EntityConfiguration> getEntitiesConfigurations() {
return entitiesConfigurations;
}
+
+ // Getters for generators and configuration
+
+ BasicMetadataGenerator getBasicMetadataGenerator() {
+ return basicMetadataGenerator;
+ }
+
+ IdMetadataGenerator getIdMetadataGenerator() {
+ return idMetadataGenerator;
+ }
+
+ CollectionMetadataGenerator getCollectionMetadataGenerator() {
+ return collectionMetadataGenerator;
+ }
+
+ ToOneRelationMetadataGenerator getToOneRelationMetadataGenerator() {
+ return toOneRelationMetadataGenerator;
+ }
+
+ Configuration getCfg() {
+ return cfg;
+ }
+
+ GlobalConfiguration getGlobalCfg() {
+ return globalCfg;
+ }
+
+ VersionsEntitiesConfiguration getVerEntCfg() {
+ return verEntCfg;
+ }
}
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/CompositePropertyMapperBuilder.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/CompositePropertyMapperBuilder.java 2008-09-03
09:54:06 UTC (rev 148)
+++
trunk/src/main/org/jboss/envers/entities/mapper/CompositePropertyMapperBuilder.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -1,28 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.entities.mapper;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public interface CompositePropertyMapperBuilder extends PropertyMapper,
CompositeMapperBuilder {
-}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java 2008-09-03
09:54:06 UTC (rev 148)
+++ trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -39,7 +39,7 @@
/**
* @author Adam Warski (adam at warski dot org)
*/
-public class MapPropertyMapper implements CompositePropertyMapperBuilder {
+public class MapPropertyMapper implements PropertyMapper, CompositeMapperBuilder {
private String propertyName;
private ExtendedPropertyMapper delegate;
Modified: trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java 2008-09-03
09:54:06 UTC (rev 148)
+++ trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -55,7 +55,7 @@
throw new MappingException("Mapping for " + propertyName + "
already added!");
}
- CompositePropertyMapperBuilder mapperBuilder = new
MapPropertyMapper(propertyName);
+ MapPropertyMapper mapperBuilder = new MapPropertyMapper(propertyName);
properties.put(propertyName, mapperBuilder);
return mapperBuilder;
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-03
09:54:06 UTC (rev 148)
+++
trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -26,6 +26,8 @@
import java.util.Map;
/**
+ * Data describing the change of a single object in a persisten collection (when the
object was added, removed or
+ * modified in the collection).
* @author Adam Warski (adam at warski dot org)
*/
public class PersistentCollectionChangeData {
@@ -39,6 +41,10 @@
this.obj = obj;
}
+ /**
+ *
+ * @return Name of the (middle) entity that holds the collection data.
+ */
public String getEntityName() {
return entityName;
}
@@ -47,6 +53,10 @@
return data;
}
+ /**
+ *
+ * @return The affected object, which was changed (added, removed, modified) in the
collection.
+ */
public Object getObj() {
return obj;
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-09-03
09:54:06 UTC (rev 148)
+++ trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -36,9 +36,8 @@
/**
* Maps properties to the given map, basing on differences between properties of new
and old objects.
* @param data Data to map to.
- * @param newObj If the modification store for a property is DIFF, diff-s are created
from differences between
- * newObj and oldObj.
- * @param oldObj Data to map from.
+ * @param newObj New state of the entity.
+ * @param oldObj Old state of the entity.
* @return True if there are any differences between the states represented by newObj
and oldObj.
*/
public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj);
@@ -50,7 +49,7 @@
* @param data Data to map from.
* @param primaryKey Primary key of the object to which we map (for relations)
* @param versionsReader VersionsReader for reading relations
- * @param revision Revision at which the obejct is read, for reading relations
+ * @param revision Revision at which the object is read, for reading relations
*/
public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey,
VersionsReaderImplementor versionsReader, Number
revision);
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-03
09:54:06 UTC (rev 148)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -28,6 +28,7 @@
import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy;
import org.jboss.envers.entities.mapper.relation.lazy.proxy.SetProxy;
import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
+import org.jboss.envers.entities.mapper.relation.lazy.proxy.MapProxy;
import org.jboss.envers.entities.mapper.PropertyMapper;
import org.jboss.envers.exception.VersionsException;
import org.jboss.envers.configuration.VersionsConfiguration;
@@ -48,10 +49,10 @@
this.propertyName = propertyName;
}
- protected abstract <T extends Collection> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
-
VersionsReaderImplementor versionsReader,
-
Class<?> entityClass, Object primaryKey,
- Number
revision, Class<T> collectionClass);
+ protected abstract <T> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
+ VersionsReaderImplementor
versionsReader,
+ Class<?> entityClass,
Object primaryKey,
+ Number revision,
Class<T> collectionClass);
@SuppressWarnings({"unchecked"})
public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey,
@@ -71,6 +72,8 @@
value = new ListProxy(getInitializator(verCfg, versionsReader, entityClass,
primaryKey, revision, ArrayList.class));
} else if (Set.class.isAssignableFrom(collectionClass) ||
Collection.class.isAssignableFrom(collectionClass)) {
value = new SetProxy(getInitializator(verCfg, versionsReader, entityClass,
primaryKey, revision, HashSet.class));
+ } else if (Map.class.isAssignableFrom(collectionClass)) {
+ value = new MapProxy(getInitializator(verCfg, versionsReader, entityClass,
primaryKey, revision, HashMap.class));
} else {
throw new VersionsException("Unsupported versioned collection type:
" + collectionClass.getName());
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-03
09:54:06 UTC (rev 148)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -134,7 +134,7 @@
return false;
}
- protected <T extends Collection> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
+ protected <T> Initializor<T> getInitializator(VersionsConfiguration
verCfg,
VersionsReaderImplementor versionsReader,
Class<?>
entityClass, Object primaryKey,
Number revision,
Class<T> collectionClass) {
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-03
09:54:06 UTC (rev 148)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -36,7 +36,7 @@
/**
* @author Adam Warski (adam at warski dot org)
*/
-public class DetachedRelationInitializor<T extends Collection> implements
Initializor<T> {
+public class DetachedRelationInitializor<T> implements Initializor<T> {
private final VersionsConfiguration verCfg;
private final String entityName;
private final DetachedRelationQueryGenerator queryGenerator;
@@ -71,7 +71,8 @@
throw new VersionsException(e);
}
- entityInstantiator.addInstancesFromVersionsEntities(entityName, result,
queryResult, revision);
+ // TODO
+ entityInstantiator.addInstancesFromVersionsEntities(entityName, (Collection)
result, queryResult, revision);
return result;
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java 2008-09-03
09:54:06 UTC (rev 148)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -26,6 +26,6 @@
/**
* @author Adam Warski (adam at warski dot org)
*/
-public interface Initializor<T extends Collection> {
+public interface Initializor<T> {
T initialize();
}
Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -0,0 +1,117 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ *
+ * See the copyright.txt in the distribution for a full listing of individual
+ * contributors. This copyrighted material is made available to anyone wishing
+ * to use, modify, copy, or redistribute it subject to the terms and
+ * conditions of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT A WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.entities.mapper.relation.lazy.proxy;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.Collection;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class MapProxy<K, V> implements Map<K, V> {
+ private Initializor<Map<K, V>> initializor;
+ protected Map<K, V> delegate;
+
+ public MapProxy(Initializor<Map<K, V>> initializor) {
+ this.initializor = initializor;
+ }
+
+ private void checkInit() {
+ if (delegate == null) {
+ delegate = initializor.initialize();
+ }
+ }
+
+ public int size() {
+ checkInit();
+ return delegate.size();
+ }
+
+ public boolean isEmpty() {
+ checkInit();
+ return delegate.isEmpty();
+ }
+
+ public boolean containsKey(Object o) {
+ checkInit();
+ return delegate.containsKey(o);
+ }
+
+ public boolean containsValue(Object o) {
+ checkInit();
+ return delegate.containsValue(o);
+ }
+
+ public V get(Object o) {
+ checkInit();
+ return delegate.get(o);
+ }
+
+ public V put(K k, V v) {
+ checkInit();
+ return delegate.put(k, v);
+ }
+
+ public V remove(Object o) {
+ checkInit();
+ return delegate.remove(o);
+ }
+
+ public void putAll(Map<? extends K, ? extends V> map) {
+ checkInit();
+ delegate.putAll(map);
+ }
+
+ public void clear() {
+ checkInit();
+ delegate.clear();
+ }
+
+ public Set<K> keySet() {
+ checkInit();
+ return delegate.keySet();
+ }
+
+ public Collection<V> values() {
+ checkInit();
+ return delegate.values();
+ }
+
+ public Set<Entry<K, V>> entrySet() {
+ checkInit();
+ return delegate.entrySet();
+ }
+
+ @SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"})
+ public boolean equals(Object o) {
+ checkInit();
+ return delegate.equals(o);
+ }
+
+ public int hashCode() {
+ checkInit();
+ return delegate.hashCode();
+ }
+}
Modified: trunk/src/main/org/jboss/envers/query/criteria/CriteriaTools.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/criteria/CriteriaTools.java 2008-09-03 09:54:06
UTC (rev 148)
+++ trunk/src/main/org/jboss/envers/query/criteria/CriteriaTools.java 2008-09-15 15:32:02
UTC (rev 149)
@@ -42,7 +42,7 @@
public static RelationDescription getRelatedEntity(VersionsConfiguration verCfg,
String entityName,
String propertyName) throws
VersionsException {
- RelationDescription relationDesc =
verCfg.getEntCfg().get(entityName).getRelationDescription(propertyName);
+ RelationDescription relationDesc =
verCfg.getEntCfg().getRelationDescription(entityName, propertyName);
if (relationDesc == null) {
return null;
Modified: trunk/src/main/org/jboss/envers/tools/reflection/ReflectionTools.java
===================================================================
--- trunk/src/main/org/jboss/envers/tools/reflection/ReflectionTools.java 2008-09-03
09:54:06 UTC (rev 148)
+++ trunk/src/main/org/jboss/envers/tools/reflection/ReflectionTools.java 2008-09-15
15:32:02 UTC (rev 149)
@@ -62,7 +62,7 @@
Getter value = getterCache.get(key);
if (value == null) {
value = ReflectHelper.getGetter(cls, propertyName);
- // It's ok if two getter are generated concurrently
+ // It's ok if two getters are generated concurrently
getterCache.put(key, value);
}