openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r678518 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-kernel/src/main/java/org/a...
Date Mon, 21 Jul 2008 17:55:18 GMT
Author: ppoddar
Date: Mon Jul 21 10:55:17 2008
New Revision: 678518

URL: http://svn.apache.org/viewvc?rev=678518&view=rev
Log:
OPENJPA-207: Support multi-level derived identity. Enhancer has been modified.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity.java
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java?rev=678518&r1=678517&r2=678518&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
Mon Jul 21 10:55:17 2008
@@ -872,7 +872,7 @@
             fieldVal = store.getContext().getObjectId(fieldVal);
         if (fieldVal instanceof OpenJPAId)
             fieldVal = ((OpenJPAId) fieldVal).getIdObject();
-        else if (relmapping.getObjectIdType() != null
+        if (relmapping.getObjectIdType() != null
             && relmapping.getObjectIdType().isInstance(fieldVal)) {
             Object[] pks = ApplicationIds.toPKValues(fieldVal, relmapping);
             fieldVal = pks[relmapping.getField(j.getFieldIndex()).

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java?rev=678518&r1=678517&r2=678518&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java
Mon Jul 21 10:55:17 2008
@@ -1963,6 +1963,11 @@
                 code.iadd();
                 code.invokeinterface().setMethod
                     (getFieldSupplierMethod(type));
+                if (fmds[i].getObjectIdFieldTypeCode() == JavaTypes.OBJECT) {
+                	code.checkcast().setType(ObjectId.class);
+                	code.invokevirtual().setMethod(ObjectId.class, "getId", 
+            			Object.class, null);                	
+                }
 
                 // if the type of this field meta data is
                 // non-primitive and non-string, be sure to cast
@@ -2157,6 +2162,11 @@
             }
         } else if (pkmeta.getObjectIdType() != null) {
             code.aload().setLocal(oid);
+            if (pkcode == JavaTypes.OBJECT) {
+                code.checkcast().setType(ObjectId.class);
+                code.invokevirtual().setMethod(ObjectId.class, "getId", 
+            			Object.class, null);
+            } 
             code.checkcast().setType(pktype);
         } else
             code.aload().setLocal(oid);

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java?rev=678518&r1=678517&r2=678518&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
Mon Jul 21 10:55:17 2008
@@ -1450,6 +1450,7 @@
             return null;
 
         ClassMetaData relmeta = fmd.getDeclaredTypeMetaData();
+        pk = ApplicationIds.wrap(relmeta, pk);
         if (relmeta.getIdentityType() == ClassMetaData.ID_DATASTORE
             && fmd.getObjectIdFieldTypeCode() == JavaTypes.LONG)
             pk = _broker.getStoreManager().newDataStoreId(pk, relmeta);

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java?rev=678518&r1=678517&r2=678518&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java
Mon Jul 21 10:55:17 2008
@@ -58,7 +58,7 @@
     /**
      * Return the primary key values for the given object id. The values
      * will be returned in the same order as the metadata primary key fields.
-     * Values for PC primary key fields will be the primarky key value or
+     * Values for PC primary key fields will be the primary key value or
      * oid value of the related instance (depending on 
      * {@link FieldMetaData#isObjectIdFieldIdOfPC}).
      */
@@ -89,6 +89,7 @@
             // copy fields from the oid
             PrimaryKeyFieldManager consumer = new PrimaryKeyFieldManager();
             consumer.setStore(pks);
+            oid = wrap(meta, oid);
             PCRegistry.copyKeyFieldsFromObjectId(meta.getDescribedType(),
                 consumer, oid);
             return consumer.getStore();
@@ -108,10 +109,28 @@
         }
         return pks;
     }
+    
+    /**
+     * Wraps the given object for the given type into a OpenJPA specific 
+     * application identity object wrapper instance (i.e. ObjectId) if all of 
+     * the following is true:
+     * the given type is not using built-in OpenJPA identity types
+     * the given type is using a shared OpenJPA identity type
+     * the given object is not already a wrapper identity type
+     */
+    public static Object wrap(ClassMetaData meta, Object oid) {
+        if (!meta.isOpenJPAIdentity() 
+         && meta.isObjectIdTypeShared() 
+         && !(oid instanceof ObjectId)) {
+        	return new ObjectId(meta.getDescribedType(), oid);
+        } 
+        return oid;
+    }
+    
 
     /**
      * Return a new object id constructed from the given primary key values.
-     * Values for PC primary key fields should be the primarky key value or
+     * Values for PC primary key fields should be the primary key value or
      * oid value of the related instance (depending on 
      * {@link FieldMetaData#isObjectIdFieldIdOfPC}).
      */
@@ -194,7 +213,7 @@
                 producer.setMetaData(meta);
             PCRegistry.copyKeyFieldsToObjectId(meta.getDescribedType(),
                 producer, oid);
-            return oid;
+            return ApplicationIds.wrap(meta, oid);
         }
 
         // default to reflection

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book.java?rev=678518&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book.java
Mon Jul 21 10:55:17 2008
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+import java.util.HashSet;
+import java.util.Set;
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.IdClass;
+import javax.persistence.ManyToOne;
+import javax.persistence.OneToMany;
+
+import org.apache.openjpa.persistence.jdbc.VersionColumn;
+
+/**
+ * Entity used to test compound primary keys using entity as relationship to 
+ * more than one level.
+ * 
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *  
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+@IdClass(BookId.class)
+@VersionColumn
+public class Book implements Serializable {
+    @Id
+    @Column(name="BOOK_NAME", nullable = false)
+    private String name;
+    
+    @OneToMany(cascade = CascadeType.ALL, mappedBy = "book")
+    private Set<Page> pages = new HashSet<Page>();
+    
+    @Id
+    @Column(nullable = false)
+    @ManyToOne (cascade = CascadeType.ALL)
+    private Library library;
+    
+    private String author;
+    
+	public String getName() {
+        return name;
+    }
+    
+    public void setName(String name) {
+        this.name = name;
+    }
+    
+    public Library getLibrary() {
+        return library;
+    }
+
+    public void setLibrary(Library library) {
+        this.library = library;
+    }
+
+    public Page getPage(int n) {
+        for (Page p: pages) {
+            if (p.getNumber() == n) {
+                return p;
+            }
+        }
+        return null;
+    }
+    
+    public void addPage(Page p) {
+        p.setBook(this);
+        pages.add(p);
+    }
+    
+	public String getAuthor() {
+		return author;
+	}
+
+	public void setAuthor(String author) {
+		this.author = author;
+	}
+
+    public boolean equals(Object o) {
+        if (!(o instanceof Book)) {
+            return false;
+        }
+        
+        Book other = (Book)o;
+        
+        if (!getName().equals(other.getName())) {
+            return false;
+        }
+        
+        return true;
+    }
+
+    public int hashCode() {
+        return getName().hashCode();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId.java?rev=678518&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId.java
Mon Jul 21 10:55:17 2008
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+
+/**
+ * Entity identity used to test compound primary keys using entity as 
+ * relationship to more than one level.
+ * 
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *  
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+public final class BookId implements Serializable {
+    private String name;
+    private String library;
+
+    
+    public boolean equals(Object o) {
+        if (!(o instanceof BookId)) {
+            return false;
+        }
+        
+        BookId other = (BookId)o;
+        
+        if (!(getName().equals(other.getName()))) {
+            return false;
+        }
+        
+        if (!getLibrary().equals(other.getLibrary())) {
+            return false;
+        }
+        
+        return true;
+    }
+    
+    public int hashCode() {
+        return safeHash(getName()) * safeHash(getLibrary());
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getLibrary() {
+        return library;
+    }
+
+    public void setLibrary(String library) {
+        this.library = library;
+    }
+    
+    private int safeHash(Object o) {
+    	return (o == null) ? 31 : o.hashCode();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library.java?rev=678518&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library.java
Mon Jul 21 10:55:17 2008
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+import java.util.HashSet;
+import java.util.Set;
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+
+import org.apache.openjpa.persistence.jdbc.VersionColumn;
+
+/**
+ * Entity used to test compound primary keys using entity as relationship to 
+ * more than one level.
+ * 
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *  
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+@VersionColumn
+public class Library implements Serializable {
+    @Id
+    @Column(name="LIBRARY_NAME", nullable = false)
+    private String name;
+    
+    @OneToMany(cascade = CascadeType.ALL, mappedBy = "library")
+    private Set<Book> books = new HashSet<Book>();
+    
+    private String location;
+    
+	public String getName() {
+        return name;
+    }
+    
+    public void setName(String name) {
+        this.name = name;
+    }
+    
+    public Set<Book> getBooks() {
+		return books;
+	}
+
+    public Book getBook(String name) {
+        for (Book b: books) {
+            if (b.getName().equals(name)) {
+                return b;
+            }
+        }
+        
+        return null;
+    }
+
+    public void addBook(Book book) {
+        book.setLibrary(this);
+        books.add(book);
+    }
+    
+	public String getLocation() {
+		return location;
+	}
+
+	public void setLocation(String location) {
+		this.location = location;
+	}
+
+    public boolean equals(Object o) {
+        if (!(o instanceof Library)) {
+            return false;
+        }
+        
+        Library other = (Library)o;
+        
+        if (!getName().equals(other.getName())) {
+            return false;
+        }
+        
+        return true;
+    }
+
+    public int hashCode() {
+        return getName().hashCode();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page.java?rev=678518&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page.java
Mon Jul 21 10:55:17 2008
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.IdClass;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
+import javax.persistence.ManyToOne;
+
+import org.apache.openjpa.persistence.jdbc.VersionColumn;
+
+/**
+ * Entity used to test compound primary keys using entity as relationship to 
+ * more than one level.
+ * 
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *  
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+@IdClass(PageId.class)
+@VersionColumn
+public class Page implements Serializable {
+    @Id
+    @Column(name="PAGE_NUMBER",nullable = false)
+    private int number;
+
+    @Id
+    @Column(nullable = false)
+    @ManyToOne (cascade = CascadeType.ALL)
+    @JoinColumns({
+        @JoinColumn(name="BOOK_LIBRARY_LIBRARY_NAME", referencedColumnName="LIBRARY_LIBRARY_NAME"),
+        @JoinColumn(name="BOOK_BOOK_NAME", referencedColumnName="BOOK_NAME")    
+    })
+    private Book book;
+    
+    public int getNumber() {
+        return number;
+    }
+
+    public void setNumber(int number) {
+        this.number = number;
+    }
+
+    public Book getBook() {
+        return book;
+    }
+
+    public void setBook(Book book) {
+        this.book = book;
+    }    
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId.java?rev=678518&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId.java
Mon Jul 21 10:55:17 2008
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+
+/**
+ * Entity identity used to test compound primary keys using entity as 
+ * relationship to more than one level.
+ * 
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *  
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+public final class PageId implements Serializable {
+    private int number;
+    private BookId book;
+
+    public int getNumber() {
+        return number;
+    }
+
+    public void setNumber(int number) {
+        this.number = number;
+    }
+    
+    public boolean equals(Object o) {
+        if (!(o instanceof PageId)) {
+            return false;
+        }
+        
+        PageId other = (PageId)o;
+        
+        if (!(getNumber() == other.getNumber())) {
+            return false;
+        }
+      
+        if (!getBook().equals(other.getBook())) {
+            return false;
+        }
+
+        return true;
+    }
+    
+    public int hashCode() {
+        return number * (book != null ? getBook().hashCode() : 31);
+    }
+
+    
+    public BookId getBook() {
+        return book;
+    }
+
+    public void setBook(BookId book) {
+        this.book = book;
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity.java?rev=678518&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity.java
Mon Jul 21 10:55:17 2008
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.enhance.identity;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Tests entities that use compound keys that includes entity relationship at
+ * more than one level.
+ * 
+ * Page has a compound identity to Book which itself uses a compound identity to
+ * Library.
+ * 
+ * Test case and domain classes were originally part of the reported issue <A
+ * href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ * 
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ * 
+ */
+public class TestMultipleLevelDerivedIdentity extends SingleEMFTestCase {
+	private static String LIBRARY_NAME = "LIB";
+	private static String BOOK_NAME    = "foo";
+	private static int    NUM_PAGES    = 3;
+	
+	public void setUp() throws Exception {
+		super.setUp(CLEAR_TABLES, Library.class, Book.class, Page.class,
+				"openjpa.RuntimeUnenhancedClasses", "unsupported");
+		create();
+	}
+	
+//	public void tearDown() throws Exception {
+//		
+//	}
+	
+	public void testPersist() {
+		create();
+	}
+
+	public void testQueryRootLevel() {
+		EntityManager em = emf.createEntityManager();
+		List<Library> list = em.createQuery("SELECT p FROM Library p")
+							   .getResultList();
+		assertFalse(list.isEmpty());
+		Library lib = (Library) list.get(0);
+		assertNotNull(lib.getBook(BOOK_NAME));
+		assertNotNull(lib.getBook(BOOK_NAME).getPage(1));
+	}
+	
+	public void testQueryIntermediateLevel() {
+		EntityManager em = emf.createEntityManager();
+		List<Book> list = em.createQuery("SELECT p FROM Book p")
+							   .getResultList();
+		assertFalse(list.isEmpty());
+		Book book = list.get(0);
+		Library lib = book.getLibrary();
+		for (int i=1; i<=NUM_PAGES; i++) {
+			Page page = book.getPage(i);
+			assertNotNull(page);
+			assertEquals(book, page.getBook());
+			assertEquals(lib, page.getBook().getLibrary());
+			assertEquals(page, page.getBook().getPage(page.getNumber()));
+		}
+	}
+
+	
+	public void testQueryLeafLevel() {
+		EntityManager em = emf.createEntityManager();
+		List<Page> list = em.createQuery("SELECT p FROM Page p")
+							   .getResultList();
+		assertFalse(list.isEmpty());
+		Book book = list.get(0).getBook();
+		Library lib = book.getLibrary();
+		for (Page page : list) {
+			assertEquals(book, page.getBook());
+			assertEquals(lib, page.getBook().getLibrary());
+			assertEquals(page, page.getBook().getPage(page.getNumber()));
+		}
+	}
+
+	public void testFindRootNode() {
+		EntityManager em = emf.createEntityManager();
+		Library lib = em.find(Library.class, LIBRARY_NAME);
+		assertNotNull(lib);
+		assertNotNull(lib.getBook(BOOK_NAME));
+		assertNotNull(lib.getBook(BOOK_NAME).getPage(1));
+	}
+	
+	public void testFindIntermediateNode() {
+		EntityManager em = emf.createEntityManager();
+		
+		BookId bookId = new BookId();
+		bookId.setLibrary(LIBRARY_NAME);
+		bookId.setName(BOOK_NAME);
+		Book book = em.find(Book.class, bookId);
+		assertNotNull(book);
+	}
+	
+	public void testFindLeafNode() {
+		EntityManager em = emf.createEntityManager();
+		
+		BookId bookId = new BookId();
+		bookId.setLibrary(LIBRARY_NAME);
+		bookId.setName(BOOK_NAME);
+		PageId pageId = new PageId();
+		pageId.setBook(bookId);
+		pageId.setNumber(2);
+		Page page = em.find(Page.class, pageId);
+		assertNotNull(page);
+	}
+	
+	public void testUpdate() {
+		EntityManager em = emf.createEntityManager();
+		em.getTransaction().begin();
+		BookId bookId = new BookId();
+		bookId.setLibrary(LIBRARY_NAME);
+		bookId.setName(BOOK_NAME);
+		Book book = em.find(Book.class, bookId);
+		assertNotNull(book);
+		book.setAuthor("modifiy Author");
+		em.getTransaction().commit();
+	}
+	
+	public void testDelete() {
+		EntityManager em = emf.createEntityManager();
+		em.getTransaction().begin();
+		Library lib = em.find(Library.class, LIBRARY_NAME);
+		em.remove(lib);
+		em.getTransaction().commit();
+		
+	    assertEquals(0, count(Library.class));
+	    assertEquals(0, count(Book.class));
+	    assertEquals(0, count(Page.class));
+	}
+
+	
+	/**
+	 * Create a Library with a Book and three Pages.
+	 */
+	public void create() {
+		if (count(Library.class) > 0)
+			return;
+		
+		EntityManager em = null;
+		em = emf.createEntityManager();
+		em.getTransaction().begin();
+		
+		Library lib = new Library();
+		lib.setName(LIBRARY_NAME);
+
+		Book book = new Book();
+		book.setName(BOOK_NAME);
+		lib.addBook(book);
+		for (int i = 1; i <= NUM_PAGES; i++) {
+			Page page = new Page();
+			page.setNumber(i);
+			book.addPage(page);
+		}
+		em.persist(lib);
+		em.getTransaction().commit();
+
+		em.clear();
+	}
+}

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java?rev=678518&r1=678517&r2=678518&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
Mon Jul 21 10:55:17 2008
@@ -106,4 +106,21 @@
                 .getMetaDataRepositoryInstance().getMetaData(entityClass,
                         getClass().getClassLoader(), true);
     }
+    
+    /**
+     * Get number of instances by an aggregate query with the given alias.
+     */
+    public int count(String alias) {
+    	return ((Number)emf.createEntityManager()
+    					   .createQuery("SELECT COUNT(p) FROM " + alias + " p")
+    					   .getSingleResult()).intValue();
+    }
+    
+    /**
+     * Count number of instances of the given class assuming that the alias
+     * for the class is its simple name.
+     */
+    public int count(Class c) {
+    	return count(c.getSimpleName());
+    }
 }



Mime
View raw message