From 94a0bc3781da7a432ce7c83a8ba05ecbf3235e20 Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Wed, 9 Sep 2009 21:23:48 +0000 Subject: [PATCH] OPENJPA-1270: check in test case for this JIRA git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@813125 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/persistence/compat/EntityA.java | 45 +++++++++++ .../openjpa/persistence/compat/EntityB.java | 69 +++++++++++++++++ .../persistence/compat/MappedSuper.java | 77 +++++++++++++++++++ .../compat/TestSpecCompatibilityOptions.java | 39 +++++++++- .../persistence/compat/persistence_2_0.xml | 4 + 5 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/EntityA.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/EntityB.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/MappedSuper.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/EntityA.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/EntityA.java new file mode 100644 index 000000000..2253a8a45 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/EntityA.java @@ -0,0 +1,45 @@ +/* + * 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.compat; + +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Table; + +@Entity +@Table(name="MPSP_A") +public class EntityA extends MappedSuper { + + @Column(length=30) + @Basic(fetch=FetchType.LAZY) + String name; + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/EntityB.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/EntityB.java new file mode 100644 index 000000000..c9744c0bf --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/EntityB.java @@ -0,0 +1,69 @@ +/* + * 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.compat; + +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name="MPSP_B") +public class EntityB { + + @Id + int id; + + @Column(length=30) + @Basic(fetch=FetchType.LAZY) + String name; + + @OneToOne + EntityA entityA; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public EntityA getEntityA() { + return entityA; + } + + public void setEntityA(EntityA entityA) { + this.entityA = entityA; + } + + +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/MappedSuper.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/MappedSuper.java new file mode 100644 index 000000000..a93def424 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/MappedSuper.java @@ -0,0 +1,77 @@ +/* + * 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.compat; + +import java.util.Date; + +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; +import javax.persistence.OneToOne; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Version; + +@MappedSuperclass +public abstract class MappedSuper { + private static final long serialVersionUID = -3613354849799979342L; + @Temporal(TemporalType.TIMESTAMP) + private Date lastUpdateDate; + + @Version + private int version; + + @Id + private int id; + + @OneToOne(mappedBy="entityA") + EntityB entityB; + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public Date getLastUpdateDate() { + return lastUpdateDate; + } + + public void setLastUpdateDate(Date lastUpdateDate) { + this.lastUpdateDate = lastUpdateDate; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public EntityB getEntityB() { + return entityB; + } + + public void setEntityB(EntityB entityB) { + this.entityB = entityB; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java index 806b30af1..3dbff4b48 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java @@ -18,12 +18,13 @@ */ package org.apache.openjpa.persistence.compat; +import javax.persistence.EntityManager; + import org.apache.openjpa.conf.Compatibility; import org.apache.openjpa.conf.Specification; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAPersistence; import org.apache.openjpa.persistence.test.AbstractCachedEMFTestCase; -import org.apache.openjpa.persistence.test.PersistenceTestCase; public class TestSpecCompatibilityOptions extends AbstractCachedEMFTestCase { @@ -76,4 +77,40 @@ extends AbstractCachedEMFTestCase { emf.close(); } + + /* + * Per JPA 2.0, Relationships in mapped superclass must be unidirectional. + * An exceptioin will be thrown when a bi-directional relation is detected in + * a mapped superclass. + */ + public void testMappedSuperClass() { + OpenJPAEntityManagerFactorySPI emf = + (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. + createEntityManagerFactory("persistence_2_0", + "org/apache/openjpa/persistence/compat/" + + "persistence_2_0.xml"); + EntityManager em = null; + try { + em = emf.createEntityManager(); + EntityA a = new EntityA(); + a.setId(1); + EntityB b = new EntityB(); + b.setId(1); + a.setEntityB(b); + b.setEntityA(a); + em.getTransaction().begin(); + em.persist(a); + em.persist(b); + em.getTransaction().commit(); + em.close(); + fail("An exceptioin will be thrown for a bi-directional relation declared in mapped superclass"); + } catch (org.apache.openjpa.persistence.ArgumentException e) { + if (em != null) { + em.getTransaction().rollback(); + em.close(); + } + } finally { + emf.close(); + } + } } diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/compat/persistence_2_0.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/compat/persistence_2_0.xml index a1d82424e..df277104a 100644 --- a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/compat/persistence_2_0.xml +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/compat/persistence_2_0.xml @@ -24,5 +24,9 @@ http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0" > + org.apache.openjpa.persistence.compat.EntityA + org.apache.openjpa.persistence.compat.EntityB + org.apache.openjpa.persistence.compat.MappedSuper + \ No newline at end of file