Author: adamw
Date: 2008-09-22 05:12:36 -0400 (Mon, 22 Sep 2008)
New Revision: 157
Removed:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java
Log:
ENVERS-26: intial support for @OneToMany+@JoinTable
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21
22:00:50 UTC (rev 156)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22
09:12:36 UTC (rev 157)
@@ -40,7 +40,7 @@
Type type = value.getType();
if ((type instanceof BagType || type instanceof SetType) &&
- (value.getElement() instanceof OneToMany)) {
+ (value.getElement() instanceof OneToMany) && (value.isInverse()))
{
addOneToManyAttached(name, value, currentMapper, entityName);
} else {
addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData);
@@ -110,9 +110,11 @@
name));
}
- private String getReferencedEntityName(Collection value) {
- if (value.getElement() instanceof ToOne) {
- return ((ToOne) value.getElement()).getReferencedEntityName();
+ private String getReferencedEntityName(Value value) {
+ if (value instanceof ToOne) {
+ return ((ToOne) value).getReferencedEntityName();
+ } if (value instanceof OneToMany) {
+ return ((OneToMany) value).getReferencedEntityName();
} else {
return null;
}
@@ -127,15 +129,27 @@
}
}
+ private String getMiddleTableName(Collection value, String entityName) {
+ // We check how Hibernate maps the collection.
+ if (value.getKey().getTable().equals(value.getCollectionTable())) {
+ // The table of the element is the same as the collection table - this must
be a @JoinColumn+@OneToMany
+ // mapping. Generating the table name ourselves, as Hibernate doesn't use
a middle table for mapping
+ // this relation.
+ return StringTools.getLastComponent(entityName) + "_" +
StringTools.getLastComponent(getReferencedEntityName(value.getElement()));
+ } else {
+ // Hibernate uses a middle table for mapping this relation, so we get
it's name directly.
+ return value.getCollectionTable().getName();
+ }
+ }
+
@SuppressWarnings({"unchecked"})
private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder
currentMapper,
String entityName, EntityXmlMappingData
xmlMappingData) {
// Generating the name of the middle table, its schema and catalog
// TODO: add support for @VersionsJoinTable
- String middleTableName = value.getCollectionTable().getName();
- String middleEntityName = middleTableName;
+ String middleTableName = getMiddleTableName(value,
entityName);//value.getCollectionTable().getName();
String versionsMiddleTableName =
mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName);
- String versionsMiddleEntityName =
mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName);
+ String versionsMiddleEntityName =
mainGenerator.getVerEntCfg().getVersionsEntityName(middleTableName);
String schema = value.getCollectionTable().getSchema();
String catalog = value.getCollectionTable().getCatalog();
@@ -165,7 +179,7 @@
IdMappingData referencingIdMapping =
referencingConfiguration.getIdMappingData();
// Null if this collection doesn't reference an entity.
- String referencedEntityName = getReferencedEntityName(value);
+ String referencedEntityName = getReferencedEntityName(value.getElement());
// Only valid for an inverse relation; null otherwise.
String mappedBy;
@@ -305,10 +319,9 @@
if (type instanceof ManyToOneType) {
String prefixRelated = prefix + "_";
- ToOne toOneValue = (ToOne) value;
- String referencedEntityName = toOneValue.getReferencedEntityName();
+ String referencedEntityName = getReferencedEntityName(value);
IdMappingData referencedIdMapping =
mainGenerator.getEntitiesConfigurations().get(
- toOneValue.getReferencedEntityName()).getIdMappingData();
+ referencedEntityName).getIdMappingData();
// Adding related-entity (in this case: the referenced entities id) id
mapping to the xml only if the
// relation isn't inverse (so when
<code>middleEntityXml</code> is not null).
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-21
22:00:50 UTC (rev 156)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-22
09:12:36 UTC (rev 157)
@@ -8,9 +8,21 @@
* @author Adam Warski (adam at warski dot org)
*/
public final class MiddleIdData {
+ /**
+ * Original id mapper of the related entity.
+ */
private final IdMapper originalMapper;
+ /**
+ * Prefixed id mapper (with the names for the id fields that are used in the middle
table) of the related entity.
+ */
private final IdMapper prefixedMapper;
+ /**
+ * Name of the related entity.
+ */
private final String entityName;
+ /**
+ * Versions name of the related entity.
+ */
private final String versionsEntityName;
public MiddleIdData(IdMapper originalMapper, IdMapper prefixedMapper, String
entityName, String versionsEntityName) {
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-21
22:00:50 UTC (rev 156)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-22
09:12:36 UTC (rev 157)
@@ -20,7 +20,7 @@
Number revision);
/**
- * Maps from an object to the object's representation (for an entity - only its
id).
+ * Maps from an object to the object's map representation (for an entity - only
its id).
* @param data Map to which data should be added.
* @param obj Object to map from.
*/
Deleted:
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-21
22:00:50 UTC (rev 156)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-22
09:12:36 UTC (rev 157)
@@ -1,78 +0,0 @@
-/*
- * 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;
-
-import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
-import org.jboss.envers.entities.EntityInstantiator;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.exception.VersionsException;
-import org.jboss.envers.configuration.VersionsConfiguration;
-
-import java.util.Collection;
-import java.util.List;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class DetachedRelationInitializor<T> implements Initializor<T> {
- private final VersionsConfiguration verCfg;
- private final String entityName;
- //private final DetachedRelationQueryGenerator queryGenerator;
- private final VersionsReaderImplementor versionsReader;
- private final Object primaryKey;
- private final Number revision;
- private final Class<T> collectionClass;
-
- public DetachedRelationInitializor(VersionsConfiguration verCfg, String entityName,
- //DetachedRelationQueryGenerator
queryGenerator,
- VersionsReaderImplementor versionsReader,
Object primaryKey,
- Number revision, Class<T>
collectionClass) {
- this.verCfg = verCfg;
- this.entityName = entityName;
- //this.queryGenerator = queryGenerator;
- this.versionsReader = versionsReader;
- this.primaryKey = primaryKey;
- this.revision = revision;
- this.collectionClass = collectionClass;
- }
-
- @SuppressWarnings({"unchecked"})
- public T initialize() {
- EntityInstantiator entityInstantiator = new EntityInstantiator(verCfg,
versionsReader);
-
- List queryResult = null;//queryGenerator.getQuery(versionsReader, primaryKey,
revision).list();
-
- T result;
- try {
- result = collectionClass.newInstance();
- } catch (Exception e) {
- throw new VersionsException(e);
- }
-
- // TODO
- entityInstantiator.addInstancesFromVersionsEntities(entityName, (Collection)
result, queryResult, revision);
-
- return result;
- }
-}
Deleted:
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java 2008-09-21
22:00:50 UTC (rev 156)
+++
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java 2008-09-22
09:12:36 UTC (rev 157)
@@ -1,81 +0,0 @@
-package org.jboss.envers.test.entities.manytomany.unidirectional;
-
-import org.jboss.envers.Versioned;
-
-import javax.persistence.*;
-import java.util.List;
-
-/**
- * Entity owning the many-to-many relation
- * @author Adam Warski (adam at warski dot org)
- */
-@Entity
-public class ListOwningUniEntity {
- @Id
- private Integer id;
-
- @Versioned
- private String data;
-
- @Versioned
- @ManyToMany
- private List<ListOwnedUniEntity> references;
-
- public ListOwningUniEntity() { }
-
- public ListOwningUniEntity(Integer id, String data) {
- this.id = id;
- this.data = data;
- }
-
- public ListOwningUniEntity(String data) {
- this.data = data;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getData() {
- return data;
- }
-
- public void setData(String data) {
- this.data = data;
- }
-
- public List<ListOwnedUniEntity> getReferences() {
- return references;
- }
-
- public void setReferences(List<ListOwnedUniEntity> references) {
- this.references = references;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof ListOwningUniEntity)) return false;
-
- ListOwningUniEntity that = (ListOwningUniEntity) o;
-
- if (data != null ? !data.equals(that.data) : that.data != null) return false;
- if (id != null ? !id.equals(that.id) : that.id != null) return false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = (id != null ? id.hashCode() : 0);
- result = 31 * result + (data != null ? data.hashCode() : 0);
- return result;
- }
-
- public String toString() {
- return "SetOwningEntity(id = " + id + ", data = " + data +
")";
- }
-}
\ No newline at end of file
Deleted:
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java 2008-09-21
22:00:50 UTC (rev 156)
+++
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java 2008-09-22
09:12:36 UTC (rev 157)
@@ -1,81 +0,0 @@
-package org.jboss.envers.test.entities.manytomany.unidirectional;
-
-import org.jboss.envers.Versioned;
-
-import javax.persistence.*;
-import java.util.Set;
-
-/**
- * Entity owning the many-to-many relation
- * @author Adam Warski (adam at warski dot org)
- */
-@Entity
-public class SetOwningUniEntity {
- @Id
- private Integer id;
-
- @Versioned
- private String data;
-
- @Versioned
- @ManyToMany
- private Set<SetOwnedUniEntity> references;
-
- public SetOwningUniEntity() { }
-
- public SetOwningUniEntity(Integer id, String data) {
- this.id = id;
- this.data = data;
- }
-
- public SetOwningUniEntity(String data) {
- this.data = data;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getData() {
- return data;
- }
-
- public void setData(String data) {
- this.data = data;
- }
-
- public Set<SetOwnedUniEntity> getReferences() {
- return references;
- }
-
- public void setReferences(Set<SetOwnedUniEntity> references) {
- this.references = references;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof SetOwningUniEntity)) return false;
-
- SetOwningUniEntity that = (SetOwningUniEntity) o;
-
- if (data != null ? !data.equals(that.data) : that.data != null) return false;
- if (id != null ? !id.equals(that.id) : that.id != null) return false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = (id != null ? id.hashCode() : 0);
- result = 31 * result + (data != null ? data.hashCode() : 0);
- return result;
- }
-
- public String toString() {
- return "SetOwningUniEntity(id = " + id + ", data = " + data +
")";
- }
-}
\ No newline at end of file