diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java index 0343bf067..f1e46abcf 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java @@ -2138,8 +2138,9 @@ public class ClassMetaData /** * Assert that this class' access type is allowed. * If no access style is set or an explicit style is set return. - * Otherwise check that the superclass access style, if defaulted, is the - * same as that of this receiver. + * Otherwise, if the superclass has persistent attributes, check that + * the superclass access style, if defaulted, is the same as that of this + * receiver. */ private void validateAccessType() { if (AccessCode.isEmpty(_accessType) @@ -2148,7 +2149,7 @@ public class ClassMetaData ClassMetaData sup = getPCSuperclassMetaData(); while (sup != null && sup.isExplicitAccess()) sup = sup.getPCSuperclassMetaData(); - if (sup != null) { + if (sup != null && sup.getDeclaredFields().length > 0) { int supCode = sup.getAccessType(); if (!AccessCode.isCompatibleSuper(_accessType, supCode)) throw new MetaDataException(_loc.get("access-inconsistent-inherit", diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/MappedCallbackSup.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/MappedCallbackSup.java new file mode 100644 index 000000000..7d59d1c47 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/MappedCallbackSup.java @@ -0,0 +1,34 @@ +/* + * 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.access; + +import javax.persistence.MappedSuperclass; +import javax.persistence.PrePersist; + +/* + * Mapped superclass which contains no persistent attributes, but contains + * a callback. + */ +@MappedSuperclass +public class MappedCallbackSup { + + @PrePersist + public void prePersist() { + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/PropEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/PropEntity.java new file mode 100644 index 000000000..d79388498 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/PropEntity.java @@ -0,0 +1,48 @@ +/* + * 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.access; + +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class PropEntity extends MappedCallbackSup { + + private int id; + private String name; + + public void setId(int id) { + this.id = id; + } + + @Id + public int getId() { + return id; + } + + public void setName(String name) { + this.name = name; + } + + @Basic + public String getName() { + return name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/TestDefaultAccess.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/TestDefaultAccess.java index 390b59546..8a35c0e20 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/TestDefaultAccess.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/access/TestDefaultAccess.java @@ -18,6 +18,9 @@ */ package org.apache.openjpa.persistence.access; +import java.util.Random; + +import javax.persistence.EntityManager; import javax.persistence.Query; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; @@ -31,55 +34,81 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase; public class TestDefaultAccess extends SingleEMFTestCase { + public void setUp() { + setUp(CLEAR_TABLES, + PropEntity.class, MappedCallbackSup.class); + } + + + /** + * Validates that an property access entity extending a mapped superclass + * containing no persistent fields (only a callback) does not cause access + * validation failures. + */ + public void testDefaultMappedSuperclassAccess() { + + EntityManager em = emf.createEntityManager(); + + PropEntity pe = new PropEntity(); + + pe.setId(new Random().nextInt()); + pe.setName("Name"); + + em.getTransaction().begin(); + em.persist(pe); + em.getTransaction().commit(); + em.close(); + } + /** * Validates use of access specifier of FIELD in entity-mappings. */ public void testEMDefaultFieldAccess() { - OpenJPAEntityManagerFactorySPI emf = + OpenJPAEntityManagerFactorySPI emf1 = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. createEntityManagerFactory("Access-EMFldDef", "org/apache/openjpa/persistence/access/" + "access-def-persistence.xml"); - OpenJPAEntityManagerSPI em = emf.createEntityManager(); + OpenJPAEntityManagerSPI em = emf1.createEntityManager(); verifyDefaultFieldAccess(em); em.close(); - emf.close(); + emf1.close(); } /** * Validates use of access specifier of PROPERTY in entity-mappings. */ public void testEMDefaultPropertyAccess() { - OpenJPAEntityManagerFactorySPI emf = + OpenJPAEntityManagerFactorySPI emf1 = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. createEntityManagerFactory("Access-EMPropDef", "org/apache/openjpa/persistence/access/" + "access-def-persistence.xml"); - OpenJPAEntityManagerSPI em = emf.createEntityManager(); + OpenJPAEntityManagerSPI em = emf1.createEntityManager(); verifyDefaultPropertyAccess(em); em.close(); - emf.close(); + emf1.close(); } /** * Validates use of access specifier of FIELD in persistence unit defaults. */ public void testPUDefaultFieldAccess() { - OpenJPAEntityManagerFactorySPI emf = + OpenJPAEntityManagerFactorySPI emf1 = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. createEntityManagerFactory("Access-PUFldDef", "org/apache/openjpa/persistence/access/" + "access-pudef-persistence.xml"); - OpenJPAEntityManagerSPI em = emf.createEntityManager(); + OpenJPAEntityManagerSPI em = emf1.createEntityManager(); verifyDefaultFieldAccess(em); em.close(); - emf.close(); + emf1.close(); } /** @@ -87,17 +116,17 @@ public class TestDefaultAccess extends SingleEMFTestCase { * defaults. */ public void testPUDefaultPropertyAccess() { - OpenJPAEntityManagerFactorySPI emf = + OpenJPAEntityManagerFactorySPI emf1 = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. createEntityManagerFactory("Access-PUPropDef", "org/apache/openjpa/persistence/access/" + "access-pudef-persistence.xml"); - OpenJPAEntityManagerSPI em = emf.createEntityManager(); + OpenJPAEntityManagerSPI em = emf1.createEntityManager(); verifyDefaultPropertyAccess(em); em.close(); - emf.close(); + emf1.close(); } private void verifyDefaultFieldAccess(OpenJPAEntityManagerSPI em) {