mirror of https://github.com/apache/openjpa.git
OPENJPA-2142 test case
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1298490 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ecf492ac46
commit
e1c4c92888
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* 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;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinColumns;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@IdClass(ChildPK.class)
|
||||
@Table(name = "MRG_CHILD")
|
||||
public class Child implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@JoinColumns({ @JoinColumn(name = "KEY_1", referencedColumnName = "KEY_1"),
|
||||
@JoinColumn(name = "KEY_2", referencedColumnName = "KEY_2") })
|
||||
@ManyToOne
|
||||
private Parent parent;
|
||||
|
||||
@Id
|
||||
@Column(name = "KEY_3")
|
||||
private Integer childKey;
|
||||
|
||||
@OneToMany(mappedBy = "child", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
private Collection<GrandChild> grandChilds = new ArrayList<GrandChild>();
|
||||
|
||||
public Parent getParent() { return parent; }
|
||||
public void setParent(Parent parent) { this.parent = parent; }
|
||||
public Integer getChildKey() { return childKey; }
|
||||
public void setChildKey(Integer childKey) { this.childKey = childKey; }
|
||||
public Collection<GrandChild> getGrandChilds() { return grandChilds; }
|
||||
public void setGrandChilds(Collection<GrandChild> grandChilds) { this.grandChilds = grandChilds; }
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((childKey == null) ? 0 : childKey.hashCode());
|
||||
result = prime * result
|
||||
+ ((grandChilds == null) ? 0 : grandChilds.hashCode());
|
||||
result = prime * result + ((parent == null) ? 0 : parent.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;
|
||||
Child other = (Child) obj;
|
||||
if (childKey == null) {
|
||||
if (other.childKey != null)
|
||||
return false;
|
||||
} else if (!childKey.equals(other.childKey))
|
||||
return false;
|
||||
if (grandChilds == null) {
|
||||
if (other.grandChilds != null)
|
||||
return false;
|
||||
} else if (!grandChilds.equals(other.grandChilds))
|
||||
return false;
|
||||
if (parent == null) {
|
||||
if (other.parent != null)
|
||||
return false;
|
||||
} else if (!parent.equals(other.parent))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* 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 ChildPK implements Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private ParentPK parent;
|
||||
|
||||
private Integer childKey;
|
||||
|
||||
public ParentPK getParent(){ return parent; }
|
||||
public void setParent(ParentPK parent) { this.parent = parent; }
|
||||
public Integer getChildKey() { return childKey; }
|
||||
public void setChildKey(Integer childKey) { this.childKey = childKey; }
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((childKey == null) ? 0 : childKey.hashCode());
|
||||
result = prime * result + ((parent == null) ? 0 : parent.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
ChildPK other = (ChildPK) obj;
|
||||
if (childKey == null)
|
||||
{
|
||||
if (other.childKey != null)
|
||||
return false;
|
||||
}
|
||||
else if (!childKey.equals(other.childKey))
|
||||
return false;
|
||||
if (parent == null)
|
||||
{
|
||||
if (other.parent != null)
|
||||
return false;
|
||||
}
|
||||
else if (!parent.equals(other.parent))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinColumns;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "MRG_GRANDCHILD")
|
||||
public class GrandChild implements Serializable {
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* 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;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@IdClass(ParentPK.class)
|
||||
@Table(name = "MRG_PARENT")
|
||||
public class Parent implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@Column(name = "KEY_1")
|
||||
private String key1 = "00000000000000000000000000000000";
|
||||
|
||||
@Id
|
||||
@Column(name = "KEY_2")
|
||||
private Integer key2;
|
||||
|
||||
@OneToMany(mappedBy = "parent", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
private Collection<Child> childs = new ArrayList<Child>();
|
||||
|
||||
public String getKey1() { return key1; }
|
||||
public void setKey1(String key1) { this.key1 = key1; }
|
||||
public Integer getKey2() { return key2; }
|
||||
public void setKey2(Integer key2) { this.key2 = key2; }
|
||||
public Collection<Child> getChilds() { return childs; }
|
||||
public void setChilds(Collection<Child> childs) { this.childs = childs; }
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((childs == null) ? 0 : childs.hashCode());
|
||||
result = prime * result + ((key1 == null) ? 0 : key1.hashCode());
|
||||
result = prime * result + ((key2 == null) ? 0 : key2.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;
|
||||
Parent other = (Parent) obj;
|
||||
if (childs == null) {
|
||||
if (other.childs != null)
|
||||
return false;
|
||||
} else if (!childs.equals(other.childs))
|
||||
return false;
|
||||
if (key1 == null) {
|
||||
if (other.key1 != null)
|
||||
return false;
|
||||
} else if (!key1.equals(other.key1))
|
||||
return false;
|
||||
if (key2 == null) {
|
||||
if (other.key2 != null)
|
||||
return false;
|
||||
} else if (!key2.equals(other.key2))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* 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 ParentPK implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String key1;
|
||||
|
||||
private Integer key2;
|
||||
|
||||
public ParentPK() {
|
||||
this.key1 = "00000000000000000000000000000000";
|
||||
}
|
||||
|
||||
public ParentPK(Integer key2) {
|
||||
this();
|
||||
this.key2 = key2;
|
||||
}
|
||||
|
||||
public String getKey1() { return key1; }
|
||||
public void setKey1(String key1) { this.key1 = key1; }
|
||||
public Integer getKey2() { return key2; }
|
||||
public void setKey2(Integer key2) { this.key2 = key2; }
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((key1 == null) ? 0 : key1.hashCode());
|
||||
result = prime * result + ((key2 == null) ? 0 : key2.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
ParentPK other = (ParentPK) obj;
|
||||
if (key1 == null)
|
||||
{
|
||||
if (other.key1 != null)
|
||||
return false;
|
||||
}
|
||||
else if (!key1.equals(other.key1))
|
||||
return false;
|
||||
if (key2 == null)
|
||||
{
|
||||
if (other.key2 != null)
|
||||
return false;
|
||||
}
|
||||
else if (!key2.equals(other.key2))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* 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.util.ArrayList;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
|
||||
|
||||
/**
|
||||
* Test the merge of a new object that has an Entity as
|
||||
* part of its Id
|
||||
*/
|
||||
public class TestMergeNew extends SQLListenerTestCase {
|
||||
public void setUp() {
|
||||
setUp(CLEAR_TABLES, Parent.class,
|
||||
Child.class, GrandChild.class);
|
||||
assertNotNull(emf);
|
||||
populate();
|
||||
}
|
||||
|
||||
public void testMergeParent(){
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ParentPK pk = new ParentPK(1);
|
||||
pk.setKey1("K1");
|
||||
Parent parent = em.find(Parent.class, pk);
|
||||
|
||||
Child child = new Child();
|
||||
child.setChildKey(1);
|
||||
child.setParent(parent);
|
||||
parent.getChilds().add(child);
|
||||
|
||||
GrandChild grandChild = new GrandChild();
|
||||
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();
|
||||
}
|
||||
|
||||
private void populate() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
Parent p = new Parent();
|
||||
p.setKey1("K1");
|
||||
p.setKey2(1);
|
||||
em.persist(p);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue