From 3320287392e035415d7867ac7312d9109d02306a Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Mon, 4 Jan 2010 21:17:35 +0000 Subject: [PATCH] OPENJPA-1020: fix class cast exception when retrieving eager persistent collection git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@895788 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/jdbc/kernel/JDBCStoreManager.java | 2 +- .../annotations/PColl_EntityStringEager.java | 53 ++++++++++++++++++ .../annotations/PColl_EntityStringLazy.java | 53 ++++++++++++++++++ .../annotations/TestPersistentCollection.java | 55 ++++++++++++++++++- 4 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/PColl_EntityStringEager.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/PColl_EntityStringLazy.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java index 5efd1d0f8..e7d7bd87b 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java @@ -407,7 +407,7 @@ public class JDBCStoreManager // Check if the owner has eagerly loaded ToMany relations. for (int i = 0; i < fms.length; i++) { if (res.getEager(fms[i]) != null) { - if (fms[i].isElementCollection() && !fms[i].getElement().isTypePC()) + if (!fms[i].getElement().isTypePC()) continue; Object coll = owner.fetchObject(fms[i].getIndex()); if (coll instanceof Map) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/PColl_EntityStringEager.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/PColl_EntityStringEager.java new file mode 100644 index 000000000..fe3299823 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/PColl_EntityStringEager.java @@ -0,0 +1,53 @@ +/* + * 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.annotations; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; + +import org.apache.openjpa.persistence.PersistentCollection; + +@Entity +public class PColl_EntityStringEager { + + @Id + private int id; + + @PersistentCollection(fetch=FetchType.EAGER) + private Set collectionOfStrings = new HashSet(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Set getCollectionOfStrings() { + return collectionOfStrings; + } + + public void setCollectionOfStrings(Set collectionOfStrings) { + this.collectionOfStrings = collectionOfStrings; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/PColl_EntityStringLazy.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/PColl_EntityStringLazy.java new file mode 100644 index 000000000..60828e536 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/PColl_EntityStringLazy.java @@ -0,0 +1,53 @@ +/* + * 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.annotations; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; + +import org.apache.openjpa.persistence.PersistentCollection; + +@Entity +public class PColl_EntityStringLazy { + + @Id + private int id; + + @PersistentCollection(fetch=FetchType.LAZY) + private Set collectionOfStrings = new HashSet(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Set getCollectionOfStrings() { + return collectionOfStrings; + } + + public void setCollectionOfStrings(Set collectionOfStrings) { + this.collectionOfStrings = collectionOfStrings; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/TestPersistentCollection.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/TestPersistentCollection.java index 6c30242a8..a50d73cc5 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/TestPersistentCollection.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/annotations/TestPersistentCollection.java @@ -35,7 +35,8 @@ public class TestPersistentCollection extends SingleEMFTestCase { public void setUp() { setUp(PColl_EntityA.class, PColl_EmbedB.class, PColl_EntityC.class, - PColl_EntityA1.class, PColl_EntityB.class, CLEAR_TABLES); + PColl_EntityA1.class, PColl_EntityB.class, PColl_EntityStringEager.class, + PColl_EntityStringLazy.class, CLEAR_TABLES); } @SuppressWarnings("unchecked") @@ -116,4 +117,56 @@ public class TestPersistentCollection extends SingleEMFTestCase { fail(t.getMessage()); } } + + public void testPersistentCollectionStringsLazy() { + try { + EntityManager em = emf.createEntityManager(); + + em.getTransaction().begin(); + + PColl_EntityStringLazy a = new PColl_EntityStringLazy(); + a.setId(1); + a.getCollectionOfStrings().add("one"); + em.persist(a); + em.getTransaction().commit(); + em.close(); + + em = emf.createEntityManager(); + Query q = em.createQuery("SELECT o FROM PColl_EntityStringLazy o"); + PColl_EntityStringLazy a1 = (PColl_EntityStringLazy)q.getSingleResult(); + + assertEquals(1, a1.getCollectionOfStrings().size()); + assertEquals("one", a1.getCollectionOfStrings().toArray()[0]); + assertEquals(1, a1.getId()); + em.close(); + } catch (Throwable t) { + fail(t.getMessage()); + } + } + + public void testPersistentCollectionStringsEager() { + try { + EntityManager em = emf.createEntityManager(); + + em.getTransaction().begin(); + + PColl_EntityStringEager a = new PColl_EntityStringEager(); + a.setId(1); + a.getCollectionOfStrings().add("one"); + em.persist(a); + em.getTransaction().commit(); + em.close(); + + em = emf.createEntityManager(); + Query q = em.createQuery("SELECT o FROM PColl_EntityStringEager o"); + PColl_EntityStringEager a1 = (PColl_EntityStringEager)q.getSingleResult(); + + assertEquals(1, a1.getCollectionOfStrings().size()); + assertEquals("one", a1.getCollectionOfStrings().toArray()[0]); + assertEquals(1, a1.getId()); + em.close(); + } catch (Throwable t) { + fail(t.getMessage()); + } + } }