You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by di...@apache.org on 2012/04/30 17:29:54 UTC
svn commit: r1332244 - in
/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge:
GrandChild.java GrandChildPK.java Parent.java TestMergeNew.java
Author: dianner
Date: Mon Apr 30 15:29:53 2012
New Revision: 1332244
URL: http://svn.apache.org/viewvc?rev=1332244&view=rev
Log:
OPENJPA-2142 Update test case for second problem
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChildPK.java (with props)
Modified:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChild.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/Parent.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestMergeNew.java
Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChild.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChild.java?rev=1332244&r1=1332243&r2=1332244&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChild.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChild.java Mon Apr 30 15:29:53 2012
@@ -20,48 +20,71 @@ package org.apache.openjpa.persistence.m
import java.io.Serializable;
+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 javax.persistence.Table;
@Entity
+@IdClass(GrandChildPK.class)
@Table(name = "MRG_GRANDCHILD")
public class GrandChild implements Serializable {
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
- @Id
- @JoinColumns({ @JoinColumn(name = "KEY_1", referencedColumnName = "KEY_1"),
- @JoinColumn(name = "KEY_2", referencedColumnName = "KEY_2"),
- @JoinColumn(name = "KEY_3", referencedColumnName = "KEY_3") })
- @ManyToOne
- private Child child;
-
- public Child getChild() { return child; }
- public void setChild(Child child) { this.child = child; }
-@Override
-public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((child == null) ? 0 : child.hashCode());
- return result;
-}
-@Override
-public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- GrandChild other = (GrandChild) obj;
- if (child == null) {
- if (other.child != null)
- return false;
- } else if (!child.equals(other.child))
- return false;
- return true;
-}
+ @Id
+ @JoinColumns({ @JoinColumn(name = "KEY_1", referencedColumnName = "KEY_1"),
+ @JoinColumn(name = "KEY_2", referencedColumnName = "KEY_2"),
+ @JoinColumn(name = "KEY_3", referencedColumnName = "KEY_3") })
+ @ManyToOne
+ private Child child;
+
+ @Id
+ @Column(name = "KEY_4")
+ private Integer grandChildKey;
+
+ public Child getChild() {
+ return child;
+ }
+
+ public void setChild(Child child) {
+ this.child = child;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((child == null) ? 0 : child.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ GrandChild other = (GrandChild) obj;
+ if (child == null) {
+ if (other.child != null)
+ return false;
+ } else if (!child.equals(other.child))
+ return false;
+ return true;
+ }
+
+ public Integer getGrandChildKey() {
+ return grandChildKey;
+ }
+
+ public void setGrandChildKey(Integer grandChildKey) {
+ this.grandChildKey = grandChildKey;
+
+ }
}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChildPK.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChildPK.java?rev=1332244&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChildPK.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChildPK.java Mon Apr 30 15:29:53 2012
@@ -0,0 +1,78 @@
+/*
+ * 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.merge;
+
+import java.io.Serializable;
+
+public class GrandChildPK implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ private ChildPK child;
+
+ private Integer grandChildKey;
+
+ public ChildPK getChild() {
+ return child;
+ }
+
+ public void setChild(ChildPK child) {
+ this.child = child;
+ }
+
+ public Integer getGrandChildKey() {
+ return grandChildKey;
+ }
+
+ public void setGrandChildKey(Integer grandChildKey) {
+ this.grandChildKey = grandChildKey;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((child == null) ? 0 : child.hashCode());
+ result =
+ prime * result
+ + ((grandChildKey == null) ? 0 : grandChildKey.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ GrandChildPK other = (GrandChildPK) obj;
+ if (child == null) {
+ if (other.child != null)
+ return false;
+ } else if (!child.equals(other.child))
+ return false;
+ if (grandChildKey == null) {
+ if (other.grandChildKey != null)
+ return false;
+ } else if (!grandChildKey.equals(other.grandChildKey))
+ return false;
+ return true;
+ }
+}
Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/GrandChildPK.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/Parent.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/Parent.java?rev=1332244&r1=1332243&r2=1332244&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/Parent.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/Parent.java Mon Apr 30 15:29:53 2012
@@ -47,6 +47,7 @@ public class Parent implements Serializa
@OneToMany(mappedBy = "parent", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Collection<Child> childs = new ArrayList<Child>();
+ public Parent() {}
public String getKey1() { return key1; }
public void setKey1(String key1) { this.key1 = key1; }
Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestMergeNew.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestMergeNew.java?rev=1332244&r1=1332243&r2=1332244&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestMergeNew.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/merge/TestMergeNew.java Mon Apr 30 15:29:53 2012
@@ -18,6 +18,7 @@
*/
package org.apache.openjpa.persistence.merge;
+import java.io.IOException;
import java.util.ArrayList;
import javax.persistence.EntityManager;
@@ -35,8 +36,8 @@ public class TestMergeNew extends SQLLis
assertNotNull(emf);
populate();
}
-
- public void testMergeParent(){
+
+ public void testMergeNewParent() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
ParentPK pk = new ParentPK(1);
@@ -46,13 +47,72 @@ public class TestMergeNew extends SQLLis
Child child = new Child();
child.setChildKey(1);
child.setParent(parent);
- parent.getChilds().add(child);
+ parent.getChilds().add(child);
GrandChild grandChild = new GrandChild();
+ grandChild.setGrandChildKey(1);
grandChild.setChild(child);
- child.getGrandChilds().add(grandChild);
+ child.getGrandChilds().add(grandChild);
- Parent newParent = em.merge(parent);
+ Parent newParent = em.merge(parent);
+ assertNotNull(newParent);
+
+ // verify key fields
+ assertEquals(newParent.getKey1(), "K1");
+ assertEquals(newParent.getKey2(), new Integer(1));
+
+ // verify Child field
+ ArrayList<Child> childs = (ArrayList<Child>)newParent.getChilds();
+ assertNotNull(childs);
+ assertEquals(childs.size(), 1);
+ Child newChild = childs.get(0);
+ assertNotSame(child, newChild);
+ Parent childParent = newChild.getParent();
+ assertEquals(childParent, newParent);
+ assertEquals(newChild.getChildKey(), new Integer(1));
+
+ // verify GrandChild field
+ ArrayList<GrandChild> grandChilds = (ArrayList<GrandChild>)newChild.getGrandChilds();
+ assertNotNull(grandChilds);
+ assertEquals(grandChilds.size(), 1);
+ GrandChild newGrandChild = grandChilds.get(0);
+ assertNotSame(newGrandChild, grandChild);
+ Child grandChildChild = newGrandChild.getChild();
+ assertEquals(grandChildChild, newChild);
+
+ em.getTransaction().commit();
+ em.close();
+ }
+
+ public void testMergeParentRoundTrip()throws ClassNotFoundException, IOException {
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ ParentPK pk = new ParentPK(1);
+ pk.setKey1("K1");
+ Parent parent = em.find(Parent.class, pk);
+
+ //Simulate an EJB Call to get the Parent from the server:
+ Parent p2 = (Parent) roundtrip(parent);
+
+ Child child = new Child();
+ child.setChildKey(1);
+ child.setParent(p2);
+ p2.getChilds().add(child);
+
+ GrandChild grandChild = new GrandChild();
+ grandChild.setChild(child);
+ grandChild.setGrandChildKey(1);
+ child.getGrandChilds().add(grandChild);
+
+ //Simulate an EJB Call to send the Parent back to the server:
+ Parent p3 = (Parent) roundtrip(p2);
+
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+
+ Parent newParent = em.merge(p3);
+
+ em.getTransaction().commit();
assertNotNull(newParent);
// verify key fields
@@ -66,7 +126,7 @@ public class TestMergeNew extends SQLLis
Child newChild = childs.get(0);
assertNotSame(child, newChild);
Parent childParent = newChild.getParent();
- assertEquals(childParent, newParent);
+ assertNotNull(childParent);
assertEquals(newChild.getChildKey(), new Integer(1));
// verify GrandChild field
@@ -76,9 +136,7 @@ public class TestMergeNew extends SQLLis
GrandChild newGrandChild = grandChilds.get(0);
assertNotSame(newGrandChild, grandChild);
Child grandChildChild = newGrandChild.getChild();
- assertEquals(grandChildChild, newChild);
-
- em.getTransaction().commit();
+ assertNotNull(grandChildChild);
em.close();
}