Alex Rivera | Logout

What causes a NotSerializableException in Tomcat 7?

Asked 2011-08-01T02:49:53.087
21

My DAO implementation is throwing a not serializable exception on server start up with Tomcat7. Any idea what causes this? None of my other DAOs are doing this.

Here's the class:

package com.project.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.project.model.User;

public class UserDAOImpl implements UserDAO {
    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    public void saveUser(User user) {
        hibernateTemplate.saveOrUpdate(user);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<User> listUser() {
        return hibernateTemplate.find("from User");
    }

    @Override
    @SuppressWarnings("unchecked")
    public User getUserByID(long userID) {
        List<User> users= hibernateTemplate.find("from User where id = '" + userID + "'");
        return users.size() > 0 ? users.get(0) : null;
    }
}

Here's my config:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springfram
Edit
Report

1 Answer

21

UserDaoImpl must implement the java.io.Serializable interface if it is to be serialized (your stack trace indicates an attempt is made to write an instance of the class to an object stream).

The instance to be serialized, along with all objects in the object graph of that instance, must all be serializable.

From the javadocs for Serializable,

Serializability of a class is enabled by the class implementing the java.io.Serializable interface...When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.

Note that there are exceptions to these rules. I recommend that you read the Java Object Serialization Specification for a full understanding of when object serialization takes place and it what it takes to avoid a NotSerializableException.

answered 2011-08-01T03:00:22.870

Your Answer