hibernate:允许这种映射吗?

时间:2009-08-04 06:53:36

标签: java hibernate hibernate-mapping

@Entity
public class A {
    @Column(name="Foos")
    @Basic
    private HashSet<Foo> fooList = new HashSet<Foo>();
}

其中类Foo没什么特别的,只需实现Serializable。

基本上我想将整个HashSet序列化到数据库。这种映射可以持久化,但是当我尝试加载它时,我在Foo上收到一个ClassNotFoundException。

全部例外:

...

Caused by: org.hibernate.type.SerializationException: could not deserialize
    at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:214)
    at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)
    at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:82)
    at org.hibernate.type.SerializableType.get(SerializableType.java:39)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
    at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2101)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
    at org.hibernate.loader.Loader.getRow(Loader.java:1206)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
    at org.hibernate.loader.Loader.doQuery(Loader.java:701)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2213)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:66)
    ... 25 more
Caused by: java.lang.ClassNotFoundException: Foo
    at com.sun.appserv.server.util.ASURLClassLoader.loadClass(ASURLClassLoader.java:129)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604)
    at org.hibernate.util.SerializationHelper$CustomObjectInputStream.resolveClass(SerializationHelper.java:268)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at java.util.HashSet.readObject(HashSet.java:291)
    at sun.reflect.GeneratedMethodAccessor25.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:210)
    ... 47 more

2 个答案:

答案 0 :(得分:1)

你试图这样做是不合法的。

尝试替换为:

@CollectionOfElements 
private HashSet<Foo> fooList = new HashSet<Foo>();

如果这样做,hibernate将创建额外的表来存储列表元素。它类似于将@OneToMany添加到其他实体Foo但语法更短

答案 1 :(得分:0)

事实证明,这是Glassfish中灰熊类加载器的一个问题。它与hibernate映射无关。虽然休眠仍然可能会导致较低级别的原因。