java Hibernate 抛出 StreamCorruptedException: invalid stream header
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11878884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate throws StreamCorruptedException: invalid stream header
提问by Boopathy
I have a class like this,
我有一堂这样的课
class SampleClass implements Serializable {
String name;
Serializable fieldName;
}
And another class like,
另一个类,例如,
class AnotherClass implements Serializable {
SampleClass sampleClass;
}
where both class has their getter and setter methods.
两个类都有自己的 getter 和 setter 方法。
In the main class, I'm getting the sampleClass
variable from getter function, and trying to use the sampleClass
objects. But when I'm using that, I encounter the error like, could not deserialize
.
在主类中,我sampleClass
从 getter 函数中获取变量,并尝试使用这些sampleClass
对象。但是当我使用它时,我遇到了类似的错误,could not deserialize
.
How to access the members of SampleClass
, or shall we have field members of type Serializable
?
如何访问 的成员SampleClass
,或者我们应该拥有类型为 的字段成员Serializable
?
Thanks.
谢谢。
Edited: I am using hibernate, which uses many to one relation between aemploye and aaddress tables.
编辑:我正在使用 hibernate,它使用 aemploye 和 aaddress 表之间的多对一关系。
I created the Hibernate configuration file, and Reverse engineering file in net beans, for the above two tables.
我为上面两个表创建了 Hibernate 配置文件和 net bean 中的逆向工程文件。
Then I generated the POJO class.
然后我生成了 POJO 类。
The class and xml are:
类和xml是:
Aaddress.hbm.xml
Aaddress.hbm.xml
<hibernate-mapping>
<class name="hibernatetutor.tablebeans.Aaddress" table="aaddress" schema="public">
<id name="sno" type="int">
<column name="sno" />
<generator class="assigned" />
</id>
<property name="street" type="serializable">
<column name="street" />
</property>
<set name="aemployes" inverse="true">
<key>
<column name="address" />
</key>
<one-to-many class="hibernatetutor.tablebeans.Aemploye" />
</set>
</class>
Aemploye.hbm.xml
Aemploye.hbm.xml
<hibernate-mapping>
<class name="hibernatetutor.tablebeans.Aemploye" table="aemploye" schema="public">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<many-to-one name="aaddress" class="hibernatetutor.tablebeans.Aaddress" fetch="select">
<column name="address" />
</many-to-one>
<property name="name" type="string">
<column name="name" />
</property>
</class>
Aaddress.java
Aaddress.java
public class Aaddress implements java.io.Serializable {
private int sno;
private Serializable street;
private Set aemployes = new HashSet(0);
public int getSno() {
return this.sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public Serializable getStreet() {
return this.street;
}
public void setStreet(Serializable street) {
this.street = street;
}
public Set getAemployes() {
return this.aemployes;
}
public void setAemployes(Set aemployes) {
this.aemployes = aemployes;
}
}
Aemploye.java
Aemploye.java
public class Aemploye implements java.io.Serializable {
private int id;
private Aaddress aaddress;
private String name;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public Aaddress getAaddress() {
return this.aaddress;
}
public void setAaddress(Aaddress aaddress) {
this.aaddress = aaddress;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Main.java
Main.java
private void getData() {
Session session = HibernateUtils.getInstance().openSession();
Query query = session.createQuery("from Aemploye where id=:id");
query.setParameter("id", 1);
Aemploye a = (Aemploye) query.uniqueResult();
Aaddress a1 = a.getAaddress();
System.out.println(a1.getStreet());
}
The error is:
错误是:
org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:217)
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:2096)
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.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:98)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:836)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:66)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at hibernatetutor.tablebeans.Aaddress$$EnhancerByCGLIB$bec229.getStreet(<generated>)
at hibernatetutor.Main.getData(Main.java:33)
at hibernatetutor.Main.main(Main.java:24)
Caused by: java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:753)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)
at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:252)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:209)
... 29 more
回答by oligofren
On the basis of both the question, and some of the info from the comment section, I believe your troubles are caused by the following:
综合以上两个问题,以及评论区的一些信息,我认为您的问题是由以下原因引起的:
You have for some reason chosen the street attribute to be of type serializable. In your table this column has been defined as of type TEXT
. Hibernate probably manages to save the serialized data to the column, but the database probably does not manage to keep them unaltered. Therefore, on retrieval, the now garbled serialized fail to deserialize.
由于某种原因,您选择了可序列化类型的 street 属性。在您的表中,此列已定义为类型TEXT
。Hibernate 可能设法将序列化数据保存到列中,但数据库可能无法保持它们不变。因此,在检索时,现在乱码的序列化无法反序列化。
The solution is, as Petr Pudlák noted, to get your mapping to be correct. If you choose a suitable binary type, such as BYTEA
, then you will be able to store the binary data unaltered. The retrieval should then work.
正如 Petr Pudlák 所指出的,解决方案是让您的映射正确无误。如果您选择合适的二进制类型,例如BYTEA
,那么您将能够原封不动地存储二进制数据。然后检索应该工作。
This is not the right solution IMHO, which would be to choose a suitable data type in your java code in the first place. Having the type of street to be Serializable is confusing to anyone viewing your code. String
would probably make more sense, and would also be a good fit for the column type TEXT
.
恕我直言,这不是正确的解决方案,首先要在 Java 代码中选择合适的数据类型。将街道类型设为可序列化会让任何查看您代码的人感到困惑。String
可能更有意义,并且也非常适合列类型TEXT
。
回答by Peter Lawrey
Using a getter doesn't involve serialization unless you have some very unusual framework to do this.
除非您有一些非常不寻常的框架来执行此操作,否则使用 getter 不涉及序列化。
I suggest you look at the exact stack trace (and post it in the question) and see where the exception is actually occurring.
我建议您查看确切的堆栈跟踪(并将其发布在问题中)并查看异常实际发生的位置。
回答by Petr Pudlák
I tried your classes, and it works for me:
我试过你的课,它对我有用:
import java.io.*;
class SampleClass implements Serializable {
String name;
Serializable fieldName;
}
class AnotherClass implements Serializable {
SampleClass sampleClass;
}
public class Ser {
public static void main(String argv[])
throws Exception
{
SampleClass s = new SampleClass();
s.name = "name";
s.fieldName = "fieldName";
AnotherClass a = new AnotherClass();
a.sampleClass = s;
// serialize the classes to a byte array
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(a);
oos.close();
// deserialize the classes from the byte array
ObjectInputStream is
= new ObjectInputStream(
new ByteArrayInputStream( os.toByteArray() ));
a = (AnotherClass)is.readObject();
is.close();
// print something
System.out.println(a.sampleClass.name);
}
}
Can you post the exact code that causes the problem?
你能发布导致问题的确切代码吗?