开发者

Storing objects in session

开发者 https://www.devze.com 2023-01-10 01:25 出处:网络
I have a PersistenceCapable class @PersistenceCapable(identityType = IdentityType.APPLICATION) public class MyClass

I have a PersistenceCapable class

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class MyClass
{
 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 private Long id;
         ..........
         ..........
}

In my servlet, I need to store object of this class in session

..........开发者_StackOverflow中文版..
MyClass u = new MyClass();
......
......
HttpSession session = req.getSession(true);
session.setAttribute("SV", u);
........

I am getting java.lang.RuntimeException: java.io.NotSerializableException:

What is this?


Sessions can be temporarily stored on disk or migrated to another application server. In order to make sure that objects in the session can be handled in those situations they need to be serailisable. You can flag this by implementing the Serializable interface:

import java.io.Serializable;

public class MyClass implements Serializable {
}


rsp is right: Serializable is the correct answer, but implementing Serializable is only the first step. E.g. you need to also make all fields either Serializable or transient.

Read one of the many tutorials about Java Serialization, or best of all: buy Effective Java by Joshua Bloch and read all of it, including the 4 chapters about Serialization.


I guess putting a bunch of annotations in your class does not make it implement the Serializable interface. Why should it? Do public class MyClass implements Serializable { ...

0

精彩评论

暂无评论...
验证码 换一张
取 消