3 Replies Latest reply on Jan 24, 2006 5:23 AM by Okanovi? Du?an

    NotSerializableException

    Okanovi? Du?an Newbie

      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.