OPENJPA-926 Do not validate access hierarchy when mapped superclass has no persistent attributes

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@783968 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jeremy Bauer 2009-06-12 02:51:49 +00:00
parent 62be4544b3
commit 6ee7af451c
4 changed files with 127 additions and 15 deletions

View File

@ -2138,8 +2138,9 @@ public class ClassMetaData
/** /**
* Assert that this class' access type is allowed. * Assert that this class' access type is allowed.
* If no access style is set or an explicit style is set return. * If no access style is set or an explicit style is set return.
* Otherwise check that the superclass access style, if defaulted, is the * Otherwise, if the superclass has persistent attributes, check that
* same as that of this receiver. * the superclass access style, if defaulted, is the same as that of this
* receiver.
*/ */
private void validateAccessType() { private void validateAccessType() {
if (AccessCode.isEmpty(_accessType) if (AccessCode.isEmpty(_accessType)
@ -2148,7 +2149,7 @@ public class ClassMetaData
ClassMetaData sup = getPCSuperclassMetaData(); ClassMetaData sup = getPCSuperclassMetaData();
while (sup != null && sup.isExplicitAccess()) while (sup != null && sup.isExplicitAccess())
sup = sup.getPCSuperclassMetaData(); sup = sup.getPCSuperclassMetaData();
if (sup != null) { if (sup != null && sup.getDeclaredFields().length > 0) {
int supCode = sup.getAccessType(); int supCode = sup.getAccessType();
if (!AccessCode.isCompatibleSuper(_accessType, supCode)) if (!AccessCode.isCompatibleSuper(_accessType, supCode))
throw new MetaDataException(_loc.get("access-inconsistent-inherit", throw new MetaDataException(_loc.get("access-inconsistent-inherit",

View File

@ -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() {
}
}

View File

@ -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;
}
}

View File

@ -18,6 +18,9 @@
*/ */
package org.apache.openjpa.persistence.access; package org.apache.openjpa.persistence.access;
import java.util.Random;
import javax.persistence.EntityManager;
import javax.persistence.Query; import javax.persistence.Query;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
@ -31,55 +34,81 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestDefaultAccess extends 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. * Validates use of access specifier of FIELD in entity-mappings.
*/ */
public void testEMDefaultFieldAccess() { public void testEMDefaultFieldAccess() {
OpenJPAEntityManagerFactorySPI emf = OpenJPAEntityManagerFactorySPI emf1 =
(OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
createEntityManagerFactory("Access-EMFldDef", createEntityManagerFactory("Access-EMFldDef",
"org/apache/openjpa/persistence/access/" + "org/apache/openjpa/persistence/access/" +
"access-def-persistence.xml"); "access-def-persistence.xml");
OpenJPAEntityManagerSPI em = emf.createEntityManager(); OpenJPAEntityManagerSPI em = emf1.createEntityManager();
verifyDefaultFieldAccess(em); verifyDefaultFieldAccess(em);
em.close(); em.close();
emf.close(); emf1.close();
} }
/** /**
* Validates use of access specifier of PROPERTY in entity-mappings. * Validates use of access specifier of PROPERTY in entity-mappings.
*/ */
public void testEMDefaultPropertyAccess() { public void testEMDefaultPropertyAccess() {
OpenJPAEntityManagerFactorySPI emf = OpenJPAEntityManagerFactorySPI emf1 =
(OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
createEntityManagerFactory("Access-EMPropDef", createEntityManagerFactory("Access-EMPropDef",
"org/apache/openjpa/persistence/access/" + "org/apache/openjpa/persistence/access/" +
"access-def-persistence.xml"); "access-def-persistence.xml");
OpenJPAEntityManagerSPI em = emf.createEntityManager(); OpenJPAEntityManagerSPI em = emf1.createEntityManager();
verifyDefaultPropertyAccess(em); verifyDefaultPropertyAccess(em);
em.close(); em.close();
emf.close(); emf1.close();
} }
/** /**
* Validates use of access specifier of FIELD in persistence unit defaults. * Validates use of access specifier of FIELD in persistence unit defaults.
*/ */
public void testPUDefaultFieldAccess() { public void testPUDefaultFieldAccess() {
OpenJPAEntityManagerFactorySPI emf = OpenJPAEntityManagerFactorySPI emf1 =
(OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
createEntityManagerFactory("Access-PUFldDef", createEntityManagerFactory("Access-PUFldDef",
"org/apache/openjpa/persistence/access/" + "org/apache/openjpa/persistence/access/" +
"access-pudef-persistence.xml"); "access-pudef-persistence.xml");
OpenJPAEntityManagerSPI em = emf.createEntityManager(); OpenJPAEntityManagerSPI em = emf1.createEntityManager();
verifyDefaultFieldAccess(em); verifyDefaultFieldAccess(em);
em.close(); em.close();
emf.close(); emf1.close();
} }
/** /**
@ -87,17 +116,17 @@ public class TestDefaultAccess extends SingleEMFTestCase {
* defaults. * defaults.
*/ */
public void testPUDefaultPropertyAccess() { public void testPUDefaultPropertyAccess() {
OpenJPAEntityManagerFactorySPI emf = OpenJPAEntityManagerFactorySPI emf1 =
(OpenJPAEntityManagerFactorySPI)OpenJPAPersistence. (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
createEntityManagerFactory("Access-PUPropDef", createEntityManagerFactory("Access-PUPropDef",
"org/apache/openjpa/persistence/access/" + "org/apache/openjpa/persistence/access/" +
"access-pudef-persistence.xml"); "access-pudef-persistence.xml");
OpenJPAEntityManagerSPI em = emf.createEntityManager(); OpenJPAEntityManagerSPI em = emf1.createEntityManager();
verifyDefaultPropertyAccess(em); verifyDefaultPropertyAccess(em);
em.close(); em.close();
emf.close(); emf1.close();
} }
private void verifyDefaultFieldAccess(OpenJPAEntityManagerSPI em) { private void verifyDefaultFieldAccess(OpenJPAEntityManagerSPI em) {