You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2006/04/30 16:33:05 UTC

svn commit: r398351 - in /incubator/cayenne/jpa/trunk/cayenne-jpa/src: main/java/org/apache/cayenne/jpa/bridge/ main/java/org/apache/cayenne/jpa/conf/ main/java/org/apache/cayenne/jpa/map/ test/java/org/apache/cayenne/jpa/bridge/ test/java/org/apache/c...

Author: aadamchik
Date: Sun Apr 30 07:33:02 2006
New Revision: 398351

URL: http://svn.apache.org/viewcvs?rev=398351&view=rev
Log:
adding incomplete DataMap relationship converter

Added:
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractRelationship.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity1.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity2.java
Modified:
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/AnnotationPrototypes.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractEntity.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaJoinColumn.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToMany.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToOne.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToMany.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToOne.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java Sun Apr 30 07:33:02 2006
@@ -21,11 +21,15 @@
 import org.apache.cayenne.jpa.JpaProviderException;
 import org.apache.cayenne.jpa.conf.JpaLoaderContext;
 import org.apache.cayenne.jpa.map.AccessType;
+import org.apache.cayenne.jpa.map.JpaAbstractRelationship;
 import org.apache.cayenne.jpa.map.JpaAttribute;
 import org.apache.cayenne.jpa.map.JpaColumn;
 import org.apache.cayenne.jpa.map.JpaEntity;
 import org.apache.cayenne.jpa.map.JpaEntityMap;
+import org.apache.cayenne.jpa.map.JpaManyToOne;
 import org.apache.cayenne.jpa.map.JpaNamedQuery;
+import org.apache.cayenne.jpa.map.JpaOneToMany;
+import org.apache.cayenne.jpa.map.JpaOneToOne;
 import org.apache.cayenne.jpa.map.JpaQueryHint;
 import org.apache.cayenne.jpa.map.JpaTable;
 import org.apache.cayenne.util.BaseTreeVisitor;
@@ -35,6 +39,7 @@
 import org.objectstyle.cayenne.map.DataMap;
 import org.objectstyle.cayenne.map.DbAttribute;
 import org.objectstyle.cayenne.map.DbEntity;
+import org.objectstyle.cayenne.map.DbRelationship;
 import org.objectstyle.cayenne.map.ObjAttribute;
 import org.objectstyle.cayenne.map.ObjEntity;
 import org.objectstyle.cayenne.map.ObjRelationship;
@@ -95,6 +100,9 @@
 
         JpaAttributeVisitor() {
             addChildVisitor(JpaColumn.class, new JpaColumnVisitor());
+            addChildVisitor(JpaManyToOne.class, new JpaRelationshipVisitor());
+            addChildVisitor(JpaOneToOne.class, new JpaRelationshipVisitor());
+            addChildVisitor(JpaOneToMany.class, new JpaRelationshipVisitor());
         }
 
         @Override
@@ -227,6 +235,41 @@
         }
     }
 
+    class JpaRelationshipVisitor extends BaseTreeVisitor {
+
+        @Override
+        public boolean onStartNode(ProjectPath path) {
+
+            JpaAbstractRelationship relationship = (JpaAbstractRelationship) path
+                    .getObject();
+
+            JpaEntity jpaTargetEntity = ((JpaEntityMap) path.getRoot())
+                    .entityForClass(relationship.getTargetEntityName());
+
+            ObjRelationship cayenneRelationship = (ObjRelationship) targetPath
+                    .getObject();
+
+            ObjEntity cayenneSrcEntity = (ObjEntity) cayenneRelationship
+                    .getSourceEntity();
+            DbEntity cayenneSrcDbEntity = cayenneSrcEntity.getDbEntity();
+
+            DbEntity cayenneTargetDbEntity = cayenneSrcEntity.getDataMap().getDbEntity(
+                    jpaTargetEntity.getTable().getName());
+            if (cayenneTargetDbEntity == null) {
+                cayenneTargetDbEntity = new DbEntity(jpaTargetEntity.getTable().getName());
+                cayenneSrcEntity.getDataMap().addDbEntity(cayenneTargetDbEntity);
+            }
+
+            DbRelationship dbRelationship = new DbRelationship(cayenneRelationship
+                    .getName());
+            dbRelationship.setTargetEntity(cayenneTargetDbEntity);
+            cayenneSrcDbEntity.addRelationship(dbRelationship);
+            cayenneRelationship.addDbRelationship(dbRelationship);
+
+            return true;
+        }
+    }
+
     class JpaNamedQueryVisitor extends NestedVisitor {
 
         @Override
@@ -288,15 +331,21 @@
 
         @Override
         Object createObject(ProjectPath path) {
+
             JpaTable jpaTable = (JpaTable) path.getObject();
-            DbEntity cayenneEntity = new DbEntity(jpaTable.getName());
+            ObjEntity parentCayenneEntity = (ObjEntity) targetPath.getObject();
+
+            DbEntity cayenneEntity = parentCayenneEntity.getDataMap().getDbEntity(
+                    jpaTable.getName());
+            if (cayenneEntity == null) {
+                cayenneEntity = new DbEntity(jpaTable.getName());
+                parentCayenneEntity.getDataMap().addDbEntity(cayenneEntity);
+            }
+
             cayenneEntity.setCatalog(jpaTable.getCatalog());
             cayenneEntity.setSchema(jpaTable.getSchema());
 
-            ObjEntity parentCayenneEntity = (ObjEntity) targetPath.getObject();
-            parentCayenneEntity.getDataMap().addDbEntity(cayenneEntity);
             parentCayenneEntity.setDbEntity(cayenneEntity);
-
             return cayenneEntity;
         }
     }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/AnnotationPrototypes.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/AnnotationPrototypes.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/AnnotationPrototypes.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/AnnotationPrototypes.java Sun Apr 30 07:33:02 2006
@@ -16,6 +16,7 @@
 package org.apache.cayenne.jpa.conf;
 
 import javax.persistence.Column;
+import javax.persistence.JoinColumn;
 import javax.persistence.Table;
 
 import org.apache.cayenne.jpa.JpaProviderException;
@@ -29,6 +30,7 @@
 abstract class AnnotationPrototypes {
 
     static final Column column;
+    static final JoinColumn joinColumn;
     static final Table table;
 
     static {
@@ -40,6 +42,8 @@
                     .getDeclaredField("annotatedColumn")
                     .getAnnotation(Column.class);
 
+            joinColumn = AnnotationPrototypes.class.getDeclaredField(
+                    "annotatedJoinColumn").getAnnotation(JoinColumn.class);
         }
         catch (NoSuchFieldException e) {
             throw new JpaProviderException("No annotated field found", e);
@@ -49,8 +53,15 @@
     @Column
     Object annotatedColumn;
 
+    @JoinColumn
+    Object annotatedJoinColumn;
+
     public static Column getColumn() {
         return column;
+    }
+
+    public static JoinColumn getJoinColumn() {
+        return joinColumn;
     }
 
     public static Table getTable() {

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java Sun Apr 30 07:33:02 2006
@@ -17,12 +17,17 @@
 
 import org.apache.cayenne.jpa.JpaProviderException;
 import org.apache.cayenne.jpa.map.AccessType;
+import org.apache.cayenne.jpa.map.JpaAbstractRelationship;
 import org.apache.cayenne.jpa.map.JpaAttribute;
 import org.apache.cayenne.jpa.map.JpaBasic;
 import org.apache.cayenne.jpa.map.JpaColumn;
 import org.apache.cayenne.jpa.map.JpaEntity;
 import org.apache.cayenne.jpa.map.JpaEntityMap;
+import org.apache.cayenne.jpa.map.JpaId;
+import org.apache.cayenne.jpa.map.JpaJoinColumn;
+import org.apache.cayenne.jpa.map.JpaManyToOne;
 import org.apache.cayenne.jpa.map.JpaMappedSuperclass;
+import org.apache.cayenne.jpa.map.JpaOneToOne;
 import org.apache.cayenne.jpa.map.JpaTable;
 import org.apache.cayenne.util.BaseTreeVisitor;
 import org.apache.cayenne.util.HierarchicalTreeVisitor;
@@ -65,6 +70,7 @@
 
         AbstractEntityVisitor() {
             addChildVisitor(JpaAttribute.class, new AttributeVisitor());
+            addChildVisitor(JpaId.class, new IdVisitor());
         }
 
         @Override
@@ -108,6 +114,11 @@
             // * default table (see @Table annotation defaults, JPA spec 9.1.1)
             if (entity.getTable() == null) {
                 JpaTable table = new JpaTable(AnnotationPrototypes.getTable());
+
+                // unclear whether we need to apply any other name transformations ... or
+                // even if we need to upperclas the name. Per default examples looks like
+                // we need.
+                // table.setName(entity.getName().toUpperCase());
                 table.setName(entity.getName());
                 entity.setTable(table);
             }
@@ -120,7 +131,8 @@
                 JpaClassDescriptor descriptor,
                 JpaPropertyDescriptor property) {
 
-            if (entity.attributeForName(property.getName()) != null) {
+            if (entity.attributeForName(property.getName()) != null
+                    || entity.idForName(property.getName()) != null) {
                 return;
             }
 
@@ -146,6 +158,8 @@
 
         AttributeVisitor() {
             addChildVisitor(JpaColumn.class, new ColumnVisitor());
+            addChildVisitor(JpaManyToOne.class, new FKVisitor());
+            addChildVisitor(JpaOneToOne.class, new FKVisitor());
         }
 
         @Override
@@ -153,7 +167,7 @@
             JpaAttribute attribute = (JpaAttribute) path.getObject();
 
             if (attribute.isRelationship()) {
-
+                return true;
             }
             else if (attribute.isTransient()) {
                 return false;
@@ -164,9 +178,9 @@
                     column.setName(attribute.getName());
                     attribute.setColumn(column);
                 }
-            }
 
-            return true;
+                return true;
+            }
         }
     }
 
@@ -215,7 +229,85 @@
 
     }
 
+    final class IdVisitor extends BaseTreeVisitor {
+
+        @Override
+        public boolean onStartNode(ProjectPath path) {
+            JpaId id = (JpaId) path.getObject();
+
+            if (id.getColumn() == null) {
+                JpaColumn column = new JpaColumn(AnnotationPrototypes.getColumn());
+                column.setName(id.getName());
+                id.setColumn(column);
+            }
+
+            return true;
+        }
+    }
+
     final class MappedSuperclassVisitor extends AbstractEntityVisitor {
 
+    }
+
+    final class FKVisitor extends BaseTreeVisitor {
+
+        @Override
+        public boolean onStartNode(ProjectPath path) {
+
+            JpaAttribute attribute = (JpaAttribute) path.getObjectParent();
+            if (attribute.getJoinColumns().isEmpty()) {
+
+                JpaAbstractRelationship relationship = (JpaAbstractRelationship) path
+                        .getObject();
+                if (relationship.isOwner()) {
+
+                    // JPA Spec, 2.1.8.2 (same for all relationship owners):
+                    // The following mapping defaults apply: [...]
+                    // Table A contains a foreign key to table B. The foreign key column
+                    // name is formed as the concatenation of the following: the name of
+                    // the relationship property or field of entityA; "_" ; the name of
+                    // the primary key column in table B. The foreign key column has the
+                    // same type as the primary key of table B.
+
+                    JpaEntityMap map = (JpaEntityMap) path
+                            .firstInstanceOf(JpaEntityMap.class);
+                    JpaEntity target = map.entityForClass(relationship
+                            .getTargetEntityName());
+
+                    if (target == null) {
+                        context.recordConflict(new SimpleValidationFailure(
+                                relationship,
+                                "Invalid relationship target "
+                                        + relationship.getTargetEntityName()));
+                    }
+                    else if (target.getIds().isEmpty()) {
+                        context.recordConflict(new SimpleValidationFailure(
+                                target,
+                                "Relationship target has no PK defined: "
+                                        + relationship.getTargetEntityName()));
+                    }
+                    else if (target.getIds().size() > 1) {
+                        // TODO: andrus, 4/30/2006 implement this; note that instead of
+                        // checking for "attribute.getJoinColumns().isEmpty()" above,
+                        // we'll have to match individual columns
+                        context.recordConflict(new SimpleValidationFailure(
+                                relationship,
+                                "Defaults for compound FK are not implemented."));
+                    }
+                    else {
+                        JpaId id = target.getIds().iterator().next();
+                        JpaJoinColumn joinColumn = new JpaJoinColumn(AnnotationPrototypes
+                                .getJoinColumn());
+
+                        String pkName = id.getColumn() != null
+                                ? id.getColumn().getName()
+                                : id.getName();
+                        joinColumn.setName(attribute.getName() + '_' + pkName);
+                    }
+                }
+            }
+
+            return true;
+        }
     }
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractEntity.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractEntity.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractEntity.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractEntity.java Sun Apr 30 07:33:02 2006
@@ -40,6 +40,22 @@
     protected Collection<JpaId> ids;
     protected Collection<JpaAttribute> attributes;
 
+    public JpaId idForName(String idName) {
+        if (idName == null) {
+            throw new IllegalArgumentException("Null id name");
+        }
+
+        if (ids != null) {
+            for (JpaId id : ids) {
+                if (idName.equals(id.getName())) {
+                    return id;
+                }
+            }
+        }
+
+        return null;
+    }
+
     /**
      * Returns a JpaAttribute for a given property name
      */

Added: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractRelationship.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractRelationship.java?rev=398351&view=auto
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractRelationship.java (added)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAbstractRelationship.java Sun Apr 30 07:33:02 2006
@@ -0,0 +1,67 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.cayenne.jpa.map;
+
+import java.util.Collection;
+
+import javax.persistence.CascadeType;
+import javax.persistence.FetchType;
+
+public abstract class JpaAbstractRelationship {
+
+    protected String targetEntityName;
+    protected FetchType fetch;
+    protected String mappedBy;
+    protected Collection<CascadeType> cascades;
+
+    public boolean isOwner() {
+        return getMappedBy() == null;
+    }
+
+    public Collection<CascadeType> getCascades() {
+        if (cascades == null) {
+            // TODO: andrus, 4/20/2006 - replace with
+            // ArrayList<CascadeType>() once CAY-520 gets implemented in Cayenne > 1.2
+            cascades = new EnumList(CascadeType.class, CascadeType.values().length);
+        }
+
+        return cascades;
+    }
+
+    public FetchType getFetch() {
+        return fetch;
+    }
+
+    public void setFetch(FetchType fetch) {
+        this.fetch = fetch;
+    }
+
+    public String getMappedBy() {
+        return mappedBy;
+    }
+
+    public void setMappedBy(String mappedBy) {
+        this.mappedBy = mappedBy;
+    }
+
+    public String getTargetEntityName() {
+        return targetEntityName;
+    }
+
+    public void setTargetEntityName(String targetEntityName) {
+        this.targetEntityName = targetEntityName;
+    }
+}

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaJoinColumn.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaJoinColumn.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaJoinColumn.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaJoinColumn.java Sun Apr 30 07:33:02 2006
@@ -39,14 +39,26 @@
     }
 
     public JpaJoinColumn(JoinColumn annotation) {
-        name = annotation.name();
-        referencedColumnName = annotation.referencedColumnName();
+        if (!"".equals(annotation.name())) {
+            name = annotation.name();
+        }
+
+        if (!"".equals(annotation.referencedColumnName())) {
+            referencedColumnName = annotation.referencedColumnName();
+        }
+
+        if (!"".equals(annotation.columnDefinition())) {
+            columnDefinition = annotation.columnDefinition();
+        }
+
+        if (!"".equals(annotation.table())) {
+            table = annotation.table();
+        }
+
         unique = annotation.unique();
         nullable = annotation.nullable();
         insertable = annotation.insertable();
         updatable = annotation.updatable();
-        columnDefinition = annotation.columnDefinition();
-        table = annotation.table();
     }
 
     public String getColumnDefinition() {

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToMany.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToMany.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToMany.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToMany.java Sun Apr 30 07:33:02 2006
@@ -15,18 +15,9 @@
  */
 package org.apache.cayenne.jpa.map;
 
-import java.util.Collection;
-
-import javax.persistence.CascadeType;
-import javax.persistence.FetchType;
 import javax.persistence.ManyToMany;
 
-public class JpaManyToMany {
-
-    protected String targetEntityName;
-    protected FetchType fetch = FetchType.LAZY;
-    protected String mappedBy;
-    protected Collection<CascadeType> cascades;
+public class JpaManyToMany extends JpaAbstractRelationship {
 
     public JpaManyToMany() {
 
@@ -40,55 +31,8 @@
         for (int i = 0; i < annotation.cascade().length; i++) {
             cascades.add(annotation.cascade()[i]);
         }
-        
+
         fetch = annotation.fetch();
         mappedBy = annotation.mappedBy();
-    }
-    
-    public Collection<CascadeType> getCascades() {
-        if (cascades == null) {
-            // TODO: andrus, 4/20/2006 - replace with
-            // ArrayList<CascadeType>() once CAY-520 gets implemented in Cayenne > 1.2
-            cascades = new EnumList(CascadeType.class, CascadeType.values().length);
-        }
-
-        return cascades;
-    }
-
-    public FetchType getFetch() {
-        return fetch;
-    }
-
-    public void setFetch(FetchType fetch) {
-        this.fetch = fetch;
-    }
-
-    public String getMappedBy() {
-        return mappedBy;
-    }
-
-    public void setMappedBy(String mappedBy) {
-        this.mappedBy = mappedBy;
-    }
-
-    /**
-     * Returns a class name of the association target entity.
-     * <h3>Specification Docs</h3>
-     * <p>
-     * <b>Description:</b> (Optional) The entity class that is the target of the
-     * association. Optional only if collection property is defined using Java generics.
-     * Must be specified otherwise.
-     * </p>
-     * <p>
-     * <b>Default:</b> The parameterized type of the collection when defined using
-     * generics.
-     * </p>
-     */
-    public String getTargetEntityName() {
-        return targetEntityName;
-    }
-
-    public void setTargetEntityName(String targetEntityName) {
-        this.targetEntityName = targetEntityName;
     }
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToOne.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToOne.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToOne.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaManyToOne.java Sun Apr 30 07:33:02 2006
@@ -15,18 +15,11 @@
  */
 package org.apache.cayenne.jpa.map;
 
-import java.util.Collection;
-
-import javax.persistence.CascadeType;
-import javax.persistence.FetchType;
 import javax.persistence.ManyToOne;
 
-public class JpaManyToOne {
+public class JpaManyToOne extends JpaAbstractRelationship {
 
-    protected String targetEntityName;
-    protected FetchType fetch = FetchType.EAGER;
     protected boolean optional;
-    protected Collection<CascadeType> cascades;
 
     public JpaManyToOne() {
 
@@ -45,22 +38,12 @@
         optional = annotation.optional();
     }
 
-    public Collection<CascadeType> getCascades() {
-        if (cascades == null) {
-            // TODO: andrus, 4/20/2006 - replace with
-            // ArrayList<CascadeType>() once CAY-520 gets implemented in Cayenne > 1.2
-            cascades = new EnumList(CascadeType.class, CascadeType.values().length);
-        }
-
-        return cascades;
-    }
-
-    public FetchType getFetch() {
-        return fetch;
-    }
-
-    public void setFetch(FetchType fetch) {
-        this.fetch = fetch;
+    /**
+     * Always returns null as Many-To-One is always a relationship owner.
+     */
+    @Override
+    public String getMappedBy() {
+        return null;
     }
 
     public boolean isOptional() {
@@ -69,24 +52,5 @@
 
     public void setOptional(boolean optional) {
         this.optional = optional;
-    }
-
-    /**
-     * Returns a class name of the association target entity.
-     * <h3>Specification Docs</h3>
-     * <p>
-     * <b>Description:</b> (Optional) The entity class that is the target of the
-     * association.
-     * </p>
-     * <p>
-     * <b>Default:</b> The type of the field or property that stores the association
-     * </p>
-     */
-    public String getTargetEntityName() {
-        return targetEntityName;
-    }
-
-    public void setTargetEntityName(String targetEntityName) {
-        this.targetEntityName = targetEntityName;
     }
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToMany.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToMany.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToMany.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToMany.java Sun Apr 30 07:33:02 2006
@@ -15,18 +15,9 @@
  */
 package org.apache.cayenne.jpa.map;
 
-import java.util.Collection;
-
-import javax.persistence.CascadeType;
-import javax.persistence.FetchType;
 import javax.persistence.OneToMany;
 
-public class JpaOneToMany {
-
-    protected String targetEntityName;
-    protected FetchType fetch = FetchType.LAZY;
-    protected String mappedBy;
-    protected Collection<CascadeType> cascades;
+public class JpaOneToMany extends JpaAbstractRelationship {
 
     public JpaOneToMany() {
 
@@ -40,55 +31,9 @@
         for (int i = 0; i < annotation.cascade().length; i++) {
             cascades.add(annotation.cascade()[i]);
         }
-        
+
         fetch = annotation.fetch();
         mappedBy = annotation.mappedBy();
     }
-    
-    public Collection<CascadeType> getCascades() {
-        if (cascades == null) {
-            // TODO: andrus, 4/20/2006 - replace with
-            // ArrayList<CascadeType>() once CAY-520 gets implemented in Cayenne > 1.2
-            cascades = new EnumList(CascadeType.class, CascadeType.values().length);
-        }
 
-        return cascades;
-    }
-
-    public FetchType getFetch() {
-        return fetch;
-    }
-
-    public void setFetch(FetchType fetch) {
-        this.fetch = fetch;
-    }
-
-    public String getMappedBy() {
-        return mappedBy;
-    }
-
-    public void setMappedBy(String mappedBy) {
-        this.mappedBy = mappedBy;
-    }
-
-    /**
-     * Returns a class name of the association target entity.
-     * <h3>Specification Docs</h3>
-     * <p>
-     * <b>Description:</b> (Optional) The entity class that is the target of the
-     * association. Optional only if collection property is defined using Java generics.
-     * Must be specified otherwise.
-     * </p>
-     * <p>
-     * <b>Default:</b> The parameterized type of the collection when defined using
-     * generics.
-     * </p>
-     */
-    public String getTargetEntityName() {
-        return targetEntityName;
-    }
-
-    public void setTargetEntityName(String targetEntityName) {
-        this.targetEntityName = targetEntityName;
-    }
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToOne.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToOne.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToOne.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaOneToOne.java Sun Apr 30 07:33:02 2006
@@ -15,19 +15,11 @@
  */
 package org.apache.cayenne.jpa.map;
 
-import java.util.Collection;
-
-import javax.persistence.CascadeType;
-import javax.persistence.FetchType;
 import javax.persistence.OneToOne;
 
-public class JpaOneToOne {
+public class JpaOneToOne extends JpaAbstractRelationship {
 
-    protected Collection cascades;
-    protected String targetEntityName;
-    protected FetchType fetch = FetchType.EAGER;
     protected boolean optional;
-    protected String mappedBy;
 
     public JpaOneToOne() {
 
@@ -41,38 +33,12 @@
         for (int i = 0; i < annotation.cascade().length; i++) {
             cascades.add(annotation.cascade()[i]);
         }
-        
+
         fetch = annotation.fetch();
         optional = annotation.optional();
         mappedBy = annotation.mappedBy();
     }
 
-    public Collection<CascadeType> getCascades() {
-        if (cascades == null) {
-            // TODO: andrus, 4/20/2006 - replace with
-            // ArrayList<CascadeType>() once CAY-520 gets implemented in Cayenne > 1.2
-            cascades = new EnumList(CascadeType.class, CascadeType.values().length);
-        }
-
-        return cascades;
-    }
-
-    public FetchType getFetch() {
-        return fetch;
-    }
-
-    public void setFetch(FetchType fetch) {
-        this.fetch = fetch;
-    }
-
-    public String getMappedBy() {
-        return mappedBy;
-    }
-
-    public void setMappedBy(String mappedBy) {
-        this.mappedBy = mappedBy;
-    }
-
     public boolean isOptional() {
         return optional;
     }
@@ -80,24 +46,4 @@
     public void setOptional(boolean optional) {
         this.optional = optional;
     }
-
-    /**
-     * Returns a class name of the association target entity.
-     * <h3>Specification Docs</h3>
-     * <p>
-     * <b>Description:</b> (Optional) The entity class that is the target of the
-     * association.
-     * </p>
-     * <p>
-     * <b>Default:</b> The type of the field or property that stores the association
-     * </p>
-     */
-    public String getTargetEntityName() {
-        return targetEntityName;
-    }
-
-    public void setTargetEntityName(String targetEntityName) {
-        this.targetEntityName = targetEntityName;
-    }
-
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java Sun Apr 30 07:33:02 2006
@@ -23,6 +23,8 @@
 import org.apache.cayenne.jpa.entity.cayenne.MockCayenneEntity1;
 import org.apache.cayenne.jpa.entity.cayenne.MockCayenneEntity2;
 import org.apache.cayenne.jpa.entity.cayenne.MockCayenneEntityMap1;
+import org.apache.cayenne.jpa.entity.cayenne.MockCayenneTargetEntity1;
+import org.apache.cayenne.jpa.entity.cayenne.MockCayenneTargetEntity2;
 import org.apache.cayenne.jpa.map.JpaEntityMap;
 import org.objectstyle.cayenne.map.DataMap;
 
@@ -53,6 +55,8 @@
 
         loader.loadClassMapping(map, MockCayenneEntity1.class);
         loader.loadClassMapping(map, MockCayenneEntity2.class);
+        loader.loadClassMapping(map, MockCayenneTargetEntity1.class);
+        loader.loadClassMapping(map, MockCayenneTargetEntity2.class);
 
         loader.loadClassMapping(map, MockCayenneEntityMap1.class);
 

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java Sun Apr 30 07:33:02 2006
@@ -25,6 +25,7 @@
 import org.objectstyle.cayenne.map.DbEntity;
 import org.objectstyle.cayenne.map.ObjAttribute;
 import org.objectstyle.cayenne.map.ObjEntity;
+import org.objectstyle.cayenne.map.ObjRelationship;
 
 public class DataMapMappingAssertion extends Assert {
 
@@ -33,7 +34,7 @@
         assertEquals("n1", dataMap.getName());
 
         assertNotNull(dataMap.getObjEntities());
-        assertEquals(2, dataMap.getObjEntities().size());
+        assertEquals(4, dataMap.getObjEntities().size());
         Iterator<ObjEntity> entityIt = dataMap.getObjEntities().iterator();
         assertEntity1(entityIt.next());
         assertEntity2(entityIt.next());
@@ -48,7 +49,7 @@
         assertEquals(MockCayenneEntity1.class.getName(), entity1.getClassName());
 
         assertObjAttributes(entity1);
-        assertEquals(4, entity1.getRelationshipMap().size());
+        assertObjRelationships(entity1);
 
         assertDbEntity(entity1.getDbEntity());
     }
@@ -87,5 +88,14 @@
         assertEquals("attribute9", a3.getName());
         assertEquals("column9", a3.getDbAttributeName());
         assertEquals(Integer.TYPE.getName(), a3.getType());
+    }
+
+    protected void assertObjRelationships(ObjEntity entity1) {
+        assertEquals(4, entity1.getRelationshipMap().size());
+        
+        ObjRelationship attribute5 = (ObjRelationship) entity1.getRelationship("attribute5");
+        assertNotNull(attribute5);
+        assertFalse(attribute5.isToMany());
+        assertEquals("attribute5", attribute5.getDbRelationshipPath());
     }
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java?rev=398351&r1=398350&r2=398351&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java (original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java Sun Apr 30 07:33:02 2006
@@ -19,6 +19,7 @@
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
+import javax.persistence.Id;
 import javax.persistence.IdClass;
 import javax.persistence.ManyToMany;
 import javax.persistence.ManyToOne;
@@ -31,8 +32,6 @@
 import javax.persistence.Transient;
 import javax.persistence.Version;
 
-import org.apache.cayenne.jpa.entity.MockTargetEntity1;
-import org.apache.cayenne.jpa.entity.MockTargetEntity2;
 import org.objectstyle.cayenne.CayenneDataObject;
 import org.objectstyle.cayenne.ObjectId;
 
@@ -47,27 +46,30 @@
 @IdClass(ObjectId.class)
 public class MockCayenneEntity1 extends CayenneDataObject {
 
+    @Id
+    protected int id;
+
     protected String attribute1;
 
     @Version
     protected int attribute2;
 
-    @OneToOne(targetEntity = MockTargetEntity1.class, fetch = FetchType.LAZY, optional = true, mappedBy = "mb1", cascade = {
+    @OneToOne(targetEntity = MockCayenneTargetEntity1.class, fetch = FetchType.LAZY, optional = true, mappedBy = "mb1", cascade = {
             CascadeType.MERGE, CascadeType.PERSIST
     })
     protected int attribute3;
 
-    @OneToMany(targetEntity = MockTargetEntity2.class, fetch = FetchType.LAZY, mappedBy = "mb2", cascade = {
+    @OneToMany(targetEntity = MockCayenneTargetEntity2.class, fetch = FetchType.LAZY, mappedBy = "mb2", cascade = {
             CascadeType.MERGE, CascadeType.PERSIST
     })
     protected int attribute4;
 
-    @ManyToOne(targetEntity = MockTargetEntity1.class, fetch = FetchType.LAZY, optional = true, cascade = {
+    @ManyToOne(targetEntity = MockCayenneTargetEntity2.class, fetch = FetchType.LAZY, optional = true, cascade = {
             CascadeType.MERGE, CascadeType.PERSIST
     })
     protected int attribute5;
 
-    @ManyToMany(targetEntity = MockTargetEntity1.class, fetch = FetchType.LAZY, mappedBy = "mb4", cascade = {
+    @ManyToMany(targetEntity = MockCayenneTargetEntity1.class, fetch = FetchType.LAZY, mappedBy = "mb4", cascade = {
             CascadeType.MERGE, CascadeType.PERSIST
     })
     protected int attribute6;

Added: incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity1.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity1.java?rev=398351&view=auto
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity1.java (added)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity1.java Sun Apr 30 07:33:02 2006
@@ -0,0 +1,26 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.cayenne.jpa.entity.cayenne;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class MockCayenneTargetEntity1 {
+
+    @Id
+    protected int targetId1;
+}

Added: incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity2.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity2.java?rev=398351&view=auto
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity2.java (added)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneTargetEntity2.java Sun Apr 30 07:33:02 2006
@@ -0,0 +1,26 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.cayenne.jpa.entity.cayenne;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class MockCayenneTargetEntity2 {
+
+    @Id
+    protected int targetId2;
+}