OPENJPA-677: While loading intermediate data be more strict about when superclass oid can be allowed

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@683902 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2008-08-08 08:06:18 +00:00
parent 446b85a6ae
commit 9ebfe800a5
5 changed files with 210 additions and 1 deletions

View File

@ -556,8 +556,10 @@ public class RelationFieldStrategy
ClassMapping relMapping = field.getTypeMapping();
Object oid = null;
if (relMapping.isMapped()) {
boolean subs = field.getPolymorphic() != ValueMapping.POLY_FALSE
&& relMapping.getPCSubclasses().length > 0;
oid = relMapping.getObjectId(store, res, field.getForeignKey(),
field.getPolymorphic() != ValueMapping.POLY_FALSE, null);
subs, null);
} else {
Column[] cols = field.getColumns();
if (relMapping.getIdentityType() == ClassMapping.ID_DATASTORE) {

View File

@ -0,0 +1,86 @@
/*
* 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;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.inheritance.entity.Admin;
import org.apache.openjpa.persistence.inheritance.entity.ComputerUser;
import org.apache.openjpa.persistence.inheritance.entity.RegularUser;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Tests entities obtained via find(), getReference() or navigated to via
* relation traversal refers to the same instances.
*
* Original reported in the context of entities of a inheritance hierarchy with
* SINGLE_TABLE strategy.
*
* <A HREF="http://issues.apache.org/jira/browse/OPENJPA-677">OPENJPA-677</A>
*
* @author Przemek Koprowski
* @author Pinaki Poddar
*
*/
public class TestIdentityWithSingleTableStrategy extends SingleEMFTestCase {
private Admin admin;
private RegularUser user;
public void setUp() {
super.setUp(CLEAR_TABLES, Admin.class, RegularUser.class,
ComputerUser.class);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
admin = new Admin();
user = new RegularUser();
user.setAdmin(admin);
admin.addRegularUser(user);
em.persist(admin);
em.persist(user);
em.getTransaction().commit();
em.close();
}
@Override
public void tearDown() {
// problem deleting table in MySQL
}
public void testFindAndNaviagtedEntityIdential() {
EntityManager em1 = emf.createEntityManager();
RegularUser regularUserFromFind = (RegularUser) em1.find(
RegularUser.class, user.getOid());
Admin adminFromFind = em1.find(Admin.class, admin.getOid());
Admin adminFromMethodBean = regularUserFromFind.getAdmin();
assertTrue(adminFromFind == adminFromMethodBean);
em1.close();
}
public void testReferenceAndNaviagtedEntityIdential() {
EntityManager em1 = emf.createEntityManager();
RegularUser regularUserFromFind = (RegularUser) em1.find(
RegularUser.class, user.getOid());
Admin adminFromGetReference = em1.getReference(Admin.class, admin
.getOid());
Admin adminFromMethodBean = regularUserFromFind.getAdmin();
assertTrue(adminFromGetReference == adminFromMethodBean);
em1.close();
}
}

View File

@ -0,0 +1,38 @@
/*
* 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.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
@Entity
@DiscriminatorValue("admin")
public class Admin extends ComputerUser {
@OneToMany(mappedBy = "admin")
protected Set<RegularUser> regularUsers = new HashSet<RegularUser>();
public boolean addRegularUser(RegularUser version) {
return regularUsers.add(version);
}
}

View File

@ -0,0 +1,40 @@
/*
* 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.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class ComputerUser {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Integer oid;
public Integer getOid() {
return oid;
}
}

View File

@ -0,0 +1,43 @@
/*
* 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.entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import org.apache.openjpa.persistence.jdbc.Nonpolymorphic;
@Entity
@DiscriminatorValue("user")
public class RegularUser extends ComputerUser {
@ManyToOne(fetch = FetchType.LAZY)
private Admin admin;
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
}