3 Replies Latest reply on Jan 24, 2006 5:23 AM by okism

    NotSerializableException

    okism

      Can someone tell me why this code throws NotSerializableException:

      package com.gint.scm.ejb.entity;
      
      import java.io.Serializable;
      import java.util.Collection;
      
      import javax.ejb.Local;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratorType;
      import javax.persistence.Id;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
      
      @SuppressWarnings("serial")
      @Entity
      @Local
      @Table(name = "application")
      public class Application implements Serializable{
       private long id;
      
       private String name;
      
       private Collection<Version> versions;
      
       public Application() {
       }
      
       public Application(String name) {
       this.name = name;
       }
      
       @Id(generate=GeneratorType.AUTO)
       public long getId() {
       return id;
       }
      
       public void setId(long id) {
       this.id = id;
       }
      
       @Column(name="name")
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       @OneToMany
       public Collection<Version> getVersions() {
       return versions;
       }
      
       public void setVersions(Collection<Version> versions) {
       this.versions = versions;
       }
      }
      


      package com.gint.scm.ejb.entity;
      
      import javax.ejb.Local;
      import javax.persistence.AttributeOverride;
      import javax.persistence.AttributeOverrides;
      import javax.persistence.Column;
      import javax.persistence.EmbeddedId;
      import javax.persistence.Entity;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Table;
      
      @SuppressWarnings("serial")
      @Entity
      @Local
      @Table(name = "version")
      public class Version {
      
       String versionMark;
      
       Application application;
      
       VersionPK versionPK;
      
       @EmbeddedId
       @AttributeOverrides({
       @AttributeOverride(name="versionMark",column = @Column(name="versionMark")),
       @AttributeOverride(name="applicationId",column = @Column(name="applicationId"))
       })
       public VersionPK getVersionPK() {
       return versionPK;
       }
      
       public void setVersionPK(VersionPK versionPK) {
       this.versionPK = versionPK;
       }
      
       @ManyToOne
       @JoinColumn(name="applicationId",insertable=false,updatable=false)
       public Application getApplication() {
       return application;
       }
      
       public void setApplication(Application application){
       this.application = application;
       }
      }

      package com.gint.scm.ejb.entity;
      
      import java.io.Serializable;
      
      @SuppressWarnings("serial")
      public class VersionPK implements Serializable {
      
       private String versionMark;
      
       private long applicationId;
      
       public VersionPK() {
       }
      
       public VersionPK(String versionMark, long applicationId) {
       this.versionMark = versionMark;
       this.applicationId = applicationId;
       }
      
       public long getApplicationId() {
       return applicationId;
       }
      
       public void setApplicationId(long applicationId) {
       this.applicationId = applicationId;
       }
      
       public String getVersionMark() {
       return versionMark;
       }
      
       public void setVersionMark(String versionMark) {
       this.versionMark = versionMark;
       }
      
       public int hashCode() {
       return (int) versionMark.hashCode()
       + (int) (new Long(applicationId)).hashCode();
       }
      
       public boolean equals(Object obj) {
       if (obj == this)
       return true;
       if (!(obj instanceof VersionPK))
       return false;
       if (obj == null)
       return false;
       VersionPK pk = (VersionPK) obj;
       return pk.versionMark.equals(versionMark)
       && pk.applicationId == applicationId;
       }
      
      }
      


      Creating works fine for both entities, find (by primary key) works also. The problem is if I perform find for Version entity and try to pass it to remote client.
      NotSerializableException for Version entity.
      I must say that everything works fine within container - find returns Version entity to the stateless session bean that is used as the facade. Exception is trown if Version entity is passed to the remote client from the facade.

        • 1. Re: NotSerializableException
          okism

          Correction:
          Instead of:

          The problem is if I perform find for Version entity and try to pass it to remote client.
          NotSerializableException for Version entity.

          The problem is: if I perform find for Version entity and try to pass it to remote client NotSerializableException is thrown for Version entity.

          • 2. Re: NotSerializableException
            martinganserer

            Hello,

            I think that your entity bean Version just has to implement the java.io.Serializable interface!

            • 3. Re: NotSerializableException
              okism

              Thanks a lot. I must have missed it somehow.

              But here's another:
              I want to add another entity (Edition) whose primary key is composed of VersionPK and primary key from another entity (Organisation - type of the key is long).
              Can someone tell me what would entity and PK class look like?

              Thanks in advance.