From e63abd92a73968e7687c5928e0789177c8403fee Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Tue, 22 Jan 2013 14:37:21 +0000 Subject: [PATCH] OPENJPA-2325: Test cases for Mapped Super Class without identity field git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1436960 13f79535-47bb-0310-9956-ffa450edef68 --- ...DerivedEntityFromMappedSuperWithoutId.java | 67 +++++++++++++++++++ .../MappedSuperWithoutId.java | 42 ++++++++++++ .../TestMappedSuperClass.java | 43 +++++++++++- 3 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/DerivedEntityFromMappedSuperWithoutId.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/MappedSuperWithoutId.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/DerivedEntityFromMappedSuperWithoutId.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/DerivedEntityFromMappedSuperWithoutId.java new file mode 100644 index 000000000..0983dcb14 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/DerivedEntityFromMappedSuperWithoutId.java @@ -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.inheritance.mappedsuperclass; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; +import javax.persistence.Version; + +/** + * A Mapped Super Class without a declared identity field. The derived classes + * declare identity. + * + * @author Pinaki Poddar + * + */ + +@Entity +public class DerivedEntityFromMappedSuperWithoutId extends MappedSuperWithoutId { + @Id + private long id; + + private String name; + + @Version + private int version; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/MappedSuperWithoutId.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/MappedSuperWithoutId.java new file mode 100644 index 000000000..9d5c4214e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/MappedSuperWithoutId.java @@ -0,0 +1,42 @@ +/* + * 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.inheritance.mappedsuperclass; + +import javax.persistence.MappedSuperclass; + +/** + * A Mapped Super Class without a declared identity field. The derived classes + * declare identity. + * + * @author Pinaki Poddar + * + */ + +@MappedSuperclass +public class MappedSuperWithoutId { + private String description; + + public String getDesc() { + return description; + } + + public void setDesc(String desc) { + this.description = desc; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/TestMappedSuperClass.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/TestMappedSuperClass.java index 7a757b221..c9946f964 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/TestMappedSuperClass.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/mappedsuperclass/TestMappedSuperClass.java @@ -28,16 +28,22 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase; /** * Test case and domain classes were originally part of the reported issue * OPENJPA-873 - * + *

+ * Added a new test where mapped super class does not decalre an identity. + * Originally reported + * OPENJPA-2325 + * * @author pioneer_ip@yahoo.com * @author Fay Wang - * + * @author Pinaki Poddar */ public class TestMappedSuperClass extends SingleEMFTestCase { public void setUp() { setUp(CashBaseEntity.class, - SituationDA.class, ValuableItemDA.class, CLEAR_TABLES); + SituationDA.class, ValuableItemDA.class, + MappedSuperWithoutId.class, DerivedEntityFromMappedSuperWithoutId.class, + CLEAR_TABLES); } public void testMappedSuperClass() { @@ -73,4 +79,35 @@ public class TestMappedSuperClass extends SingleEMFTestCase { em.close(); } } + + /** + * Tests that new entity can be merged when the entity is derived from a mapped + * super class that does not declare an identity field. + */ + public void testMergeNewInstanceDerivedFromMappedSuperClassWithoutIdentityField() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + final long id = System.currentTimeMillis(); + DerivedEntityFromMappedSuperWithoutId pc = new DerivedEntityFromMappedSuperWithoutId(); + pc.setId(id); + pc.setName("abc"); + em.persist(pc); + em.getTransaction().commit(); + em.close(); + + DerivedEntityFromMappedSuperWithoutId newpc = new DerivedEntityFromMappedSuperWithoutId(); + em = emf.createEntityManager(); + em.getTransaction().begin(); + newpc.setId(id); + newpc.setName("xyz"); + em.merge(newpc); + em.getTransaction().commit(); + em.close(); + + em = emf.createEntityManager(); + DerivedEntityFromMappedSuperWithoutId found = em.find(DerivedEntityFromMappedSuperWithoutId.class, id); + assertNotNull(found); + assertEquals("xyz", found.getName()); + + } }