我有一个这样的课程,
class SampleClass implements Serializable {
String name;
Serializable fieldName;
}
还有另一个类,
class AnotherClass implements Serializable {
SampleClass sampleClass;
}
这两个类都有getter和setter方法。
在主类中,我从getter函数获取sampleClass
变量,并尝试使用sampleClass
个对象。但是当我使用它时,我遇到了错误,如could not deserialize
。
如何访问SampleClass
的成员,或者我们是否有Serializable
类型的字段成员?
感谢。
编辑: 我正在使用hibernate,它使用aemploye和aaddress表之间的多对一关系。
我为上面两个表创建了Hibernate配置文件和net beans中的逆向工程文件。
然后我生成了POJO类。
class和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
<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
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
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
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());
}
错误是:
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$$44bec229.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
答案 0 :(得分:1)
根据问题和评论部分的一些信息,我相信您的麻烦是由以下原因引起的:
您出于某种原因选择了street属性为serializable类型。在您的表中,此列已定义为类型TEXT
。 Hibernate可能设法将序列化数据保存到列中,但数据库可能无法保持不变。因此,在检索时,现在的乱码序列化无法反序列化。
正如PetrPudlák所指出的 ,解决方案是让你的映射正确。如果选择合适的二进制类型,例如BYTEA
,则可以不加改变地存储二进制数据。然后检索应该有效。
这不是正确的解决方案恕我直言,这将首先在您的Java代码中选择合适的数据类型。将街道类型设置为Serializable会让任何查看代码的人感到困惑。 String
可能更有意义,也非常适合列类型TEXT
。
答案 1 :(得分:0)
使用getter不涉及序列化,除非你有一些非常不寻常的框架来执行此操作。
我建议您查看确切的堆栈跟踪(并在问题中发布)并查看异常实际发生的位置。
答案 2 :(得分:0)
我尝试过你的课程,这对我有用:
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);
}
}
您可以发布导致问题的确切代码吗?