mirror of
https://github.com/apache/openjpa.git
synced 2025-02-08 02:59:42 +00:00
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:
parent
badcf47af8
commit
a0f7a2f50f
@ -20,48 +20,71 @@ package org.apache.openjpa.persistence.merge;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.IdClass;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.JoinColumns;
|
import javax.persistence.JoinColumns;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@IdClass(GrandChildPK.class)
|
||||||
@Table(name = "MRG_GRANDCHILD")
|
@Table(name = "MRG_GRANDCHILD")
|
||||||
public class GrandChild implements Serializable {
|
public class GrandChild implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@JoinColumns({ @JoinColumn(name = "KEY_1", referencedColumnName = "KEY_1"),
|
@JoinColumns({ @JoinColumn(name = "KEY_1", referencedColumnName = "KEY_1"),
|
||||||
@JoinColumn(name = "KEY_2", referencedColumnName = "KEY_2"),
|
@JoinColumn(name = "KEY_2", referencedColumnName = "KEY_2"),
|
||||||
@JoinColumn(name = "KEY_3", referencedColumnName = "KEY_3") })
|
@JoinColumn(name = "KEY_3", referencedColumnName = "KEY_3") })
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
private Child child;
|
private Child child;
|
||||||
|
|
||||||
public Child getChild() { return child; }
|
@Id
|
||||||
public void setChild(Child child) { this.child = child; }
|
@Column(name = "KEY_4")
|
||||||
@Override
|
private Integer grandChildKey;
|
||||||
public int hashCode() {
|
|
||||||
final int prime = 31;
|
public Child getChild() {
|
||||||
int result = 1;
|
return child;
|
||||||
result = prime * result + ((child == null) ? 0 : child.hashCode());
|
}
|
||||||
return result;
|
|
||||||
}
|
public void setChild(Child child) {
|
||||||
@Override
|
this.child = child;
|
||||||
public boolean equals(Object obj) {
|
}
|
||||||
if (this == obj)
|
|
||||||
return true;
|
@Override
|
||||||
if (obj == null)
|
public int hashCode() {
|
||||||
return false;
|
final int prime = 31;
|
||||||
if (getClass() != obj.getClass())
|
int result = 1;
|
||||||
return false;
|
result = prime * result + ((child == null) ? 0 : child.hashCode());
|
||||||
GrandChild other = (GrandChild) obj;
|
return result;
|
||||||
if (child == null) {
|
}
|
||||||
if (other.child != null)
|
|
||||||
return false;
|
@Override
|
||||||
} else if (!child.equals(other.child))
|
public boolean equals(Object obj) {
|
||||||
return false;
|
if (this == obj)
|
||||||
return true;
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -47,6 +47,7 @@ public class Parent implements Serializable {
|
|||||||
|
|
||||||
@OneToMany(mappedBy = "parent", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@OneToMany(mappedBy = "parent", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
private Collection<Child> childs = new ArrayList<Child>();
|
private Collection<Child> childs = new ArrayList<Child>();
|
||||||
|
public Parent() {}
|
||||||
|
|
||||||
public String getKey1() { return key1; }
|
public String getKey1() { return key1; }
|
||||||
public void setKey1(String key1) { this.key1 = key1; }
|
public void setKey1(String key1) { this.key1 = key1; }
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.openjpa.persistence.merge;
|
package org.apache.openjpa.persistence.merge;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
@ -36,7 +37,7 @@ public class TestMergeNew extends SQLListenerTestCase {
|
|||||||
populate();
|
populate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMergeParent(){
|
public void testMergeNewParent() {
|
||||||
EntityManager em = emf.createEntityManager();
|
EntityManager em = emf.createEntityManager();
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
ParentPK pk = new ParentPK(1);
|
ParentPK pk = new ParentPK(1);
|
||||||
@ -46,13 +47,72 @@ public class TestMergeNew extends SQLListenerTestCase {
|
|||||||
Child child = new Child();
|
Child child = new Child();
|
||||||
child.setChildKey(1);
|
child.setChildKey(1);
|
||||||
child.setParent(parent);
|
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);
|
||||||
|
|
||||||
|
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 grandChild = new GrandChild();
|
||||||
grandChild.setChild(child);
|
grandChild.setChild(child);
|
||||||
|
grandChild.setGrandChildKey(1);
|
||||||
child.getGrandChilds().add(grandChild);
|
child.getGrandChilds().add(grandChild);
|
||||||
|
|
||||||
Parent newParent = em.merge(parent);
|
//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);
|
assertNotNull(newParent);
|
||||||
|
|
||||||
// verify key fields
|
// verify key fields
|
||||||
@ -66,7 +126,7 @@ public class TestMergeNew extends SQLListenerTestCase {
|
|||||||
Child newChild = childs.get(0);
|
Child newChild = childs.get(0);
|
||||||
assertNotSame(child, newChild);
|
assertNotSame(child, newChild);
|
||||||
Parent childParent = newChild.getParent();
|
Parent childParent = newChild.getParent();
|
||||||
assertEquals(childParent, newParent);
|
assertNotNull(childParent);
|
||||||
assertEquals(newChild.getChildKey(), new Integer(1));
|
assertEquals(newChild.getChildKey(), new Integer(1));
|
||||||
|
|
||||||
// verify GrandChild field
|
// verify GrandChild field
|
||||||
@ -76,9 +136,7 @@ public class TestMergeNew extends SQLListenerTestCase {
|
|||||||
GrandChild newGrandChild = grandChilds.get(0);
|
GrandChild newGrandChild = grandChilds.get(0);
|
||||||
assertNotSame(newGrandChild, grandChild);
|
assertNotSame(newGrandChild, grandChild);
|
||||||
Child grandChildChild = newGrandChild.getChild();
|
Child grandChildChild = newGrandChild.getChild();
|
||||||
assertEquals(grandChildChild, newChild);
|
assertNotNull(grandChildChild);
|
||||||
|
|
||||||
em.getTransaction().commit();
|
|
||||||
em.close();
|
em.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user