gpt4 book ai didi

java - 在 oracle blob 中持久保存 java 对象

转载 作者:行者123 更新时间:2023-12-01 12:16:30 25 4
gpt4 key购买 nike

我遇到了一个小问题,也许你可以帮忙。我想做的是将任何给定的 java 对象(可序列化)持久保存到数据库中的 blob 字段中。

目标:在 Oracle Blob 中保留 java 对象

方式:序列化对象并创建 BlobDomain 作为实体的一部分,映射到数据库中的 BLOB 字段

代码:

实体:

@Entity
@Table(name = "ARGUMENT")
public class Argument implements java.io.Serializable {
private static final long serialVersionUID = 1L;

@Id @Column(name = "ID")
private Long id;

@Column(name = "B_VALUE")
private BlobDomain value;

...
}

设置值:

private void createArgument (Serializable argVal) throws RuntimeException {
// argVal is the object to be stored
Argument arg = new Argument();
byte[] bytes = serialize(argVal);
BlobDomain value = new BlobDomain(bytes);
arg.setValue(value);
argumentDao.persist(arg);
}

public byte[] serialize(Object obj) throws IOException, SerialException, SQLException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bs);
os.writeObject(obj);
os.close();
return bs.toByteArray();
}

错误:Argument 实体已正确存储在数据库(ARGUMENT 表)中,但是当我尝试查询表的任何行时,它会引发以下异常:

org.hibernate.type.SerializationException:无法反序列化

有人知道为什么会发生这种情况吗?

提前致谢!!

完整堆栈:

org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:188)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:211)
at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:105)
at org.hibernate.type.SerializableType.get(SerializableType.java:62)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2124)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
at org.hibernate.loader.Loader.getRow(Loader.java:1230)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
at org.hibernate.loader.Loader.doQuery(Loader.java:724)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2015)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:59)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:587)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1743)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:476)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:867)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:264)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:935)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
... 38 more
Caused by: java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:764)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:223)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:180)
... 67 more

最佳答案

您需要添加注释

@Lob


import javax.persistence.Lob;

@Column(name = "B_VALUE")
@Lob
private BlobDomain value;

如果对象很大,您还可以定义列的长度。

@Column(length = 20971520)

@Lob,指定持久属性或字段应作为大对象持久保存到数据库支持的大对象类型。

关于java - 在 oracle blob 中持久保存 java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26964702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com