0
@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
4

2 回答 2

1

不,您尝试这样做的方式是不合法的。

尝试用这个替换:

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

如果你这样做,hibernate 将创建额外的表来存储列表元素。它类似于将 @OneToMany 用于附加实体 Foo,但语法要短得多

于 2009-08-04T09:38:58.843 回答
0

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

于 2009-08-06T03:42:14.253 回答