diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java index f310f9449..2e1c47d1f 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java @@ -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) { diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java new file mode 100644 index 000000000..e60f32a30 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java @@ -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. + * + * OPENJPA-677 + * + * @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(); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java new file mode 100644 index 000000000..d83c4dbc3 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java @@ -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 regularUsers = new HashSet(); + + public boolean addRegularUser(RegularUser version) { + return regularUsers.add(version); + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java new file mode 100644 index 000000000..413a062f3 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java @@ -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; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java new file mode 100644 index 000000000..c6171878c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java @@ -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; + } + +}