OPENJPA-2142 Update test case for second problem

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1332244 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dianne E. Richards 2012-04-30 15:29:53 +00:00
parent badcf47af8
commit a0f7a2f50f
4 changed files with 201 additions and 41 deletions

View File

@ -20,48 +20,71 @@ package org.apache.openjpa.persistence.merge;
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;
@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
@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;
}
}

View File

@ -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;
}
}

View File

@ -47,6 +47,7 @@ public class Parent implements Serializable {
@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; }

View File

@ -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 SQLListenerTestCase {
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 SQLListenerTestCase {
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 SQLListenerTestCase {
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 SQLListenerTestCase {
GrandChild newGrandChild = grandChilds.get(0);
assertNotSame(newGrandChild, grandChild);
Child grandChildChild = newGrandChild.getChild();
assertEquals(grandChildChild, newChild);
em.getTransaction().commit();
assertNotNull(grandChildChild);
em.close();
}