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
This commit is contained in:
Fay Wang 2010-01-04 21:17:35 +00:00
parent f78ddef9c2
commit 3320287392
4 changed files with 161 additions and 2 deletions

View File

@ -407,7 +407,7 @@ public class JDBCStoreManager
// Check if the owner has eagerly loaded ToMany relations. // Check if the owner has eagerly loaded ToMany relations.
for (int i = 0; i < fms.length; i++) { for (int i = 0; i < fms.length; i++) {
if (res.getEager(fms[i]) != null) { if (res.getEager(fms[i]) != null) {
if (fms[i].isElementCollection() && !fms[i].getElement().isTypePC()) if (!fms[i].getElement().isTypePC())
continue; continue;
Object coll = owner.fetchObject(fms[i].getIndex()); Object coll = owner.fetchObject(fms[i].getIndex());
if (coll instanceof Map) if (coll instanceof Map)

View File

@ -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<String> collectionOfStrings = new HashSet<String>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<String> getCollectionOfStrings() {
return collectionOfStrings;
}
public void setCollectionOfStrings(Set<String> collectionOfStrings) {
this.collectionOfStrings = collectionOfStrings;
}
}

View File

@ -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<String> collectionOfStrings = new HashSet<String>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<String> getCollectionOfStrings() {
return collectionOfStrings;
}
public void setCollectionOfStrings(Set<String> collectionOfStrings) {
this.collectionOfStrings = collectionOfStrings;
}
}

View File

@ -35,7 +35,8 @@ public class TestPersistentCollection extends SingleEMFTestCase {
public void setUp() { public void setUp() {
setUp(PColl_EntityA.class, PColl_EmbedB.class, PColl_EntityC.class, 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") @SuppressWarnings("unchecked")
@ -116,4 +117,56 @@ public class TestPersistentCollection extends SingleEMFTestCase {
fail(t.getMessage()); 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());
}
}
} }