Java中serialVersionUID的解释 serialVersionUID作用: 
       序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。
有两种生成方式:
       一个是默认的1L,比如:private static final long serialVersionUID = 1L;
       一个是根据类名、接口名、成员方法及属性等来生成一个64位的哈希字段,比如:
       private static final   long     serialVersionUID = xxxxL;当你一个类实现了Serializable接口,如果没有定义serialVersionUID,Eclipse会提供这个
     提示功能告诉你去定义 。在Eclipse中点击类中warning的图标一下,Eclipse就会
     自动给定两种生成的方式。如果不想定义它,在Eclipse的设置中也
      可以把它关掉的,设置如下: 
        Window ==> Preferences ==> Java ==> Compiler ==> Error/Warnings ==>
        Potential programming problems 
        将Serializable class without serialVersionUID的warning改成ignore即可。如果你没有考虑到兼容性问题时,就把它关掉,不过有这个功能是好的,只要任何类别实现了Serializable这个接口的话,如果没有加入serialVersionUID,Eclipse都会给你warning提示,这个serialVersionUID为了让该类别Serializable向后兼容。 如果你的类Serialized存到硬盘上面后,可是后来你却更改了类别的field(增加或减少或改名),当你Deserialize时,就会出现Exception的,这样就会造成不兼容性的问题。 但当serialVersionUID相同时,它就会将不一样的field以type的预设值Deserialize,可避开不兼容性问题。

解决方案 »

  1.   

    这篇blog可以说明你的问题http://snowlotus.javaeye.com/blog/247129
      

  2.   

    下面是我摘录的一段教学课程,应该对你有帮助。
    如果你对Serialize and Deserialize an object有了解的话可以直接去看后面的serialVersionUID的那部分,
    因为后面要用到前面的例子,所以我都写了上来。Exercise 3: Serialize and Deserialize an object
    In this exercise, you will learn how to do serialization and deserialization of the an object.  You will also learn how to use transient keyword.Serialize the current time 
    Use transient keyword(1.1) Serialize the current time
    0. Start NetBeans IDE if you have not done so yet.
    1. Create a new NetBeans projectSelect File->New Project (Ctrl+Shift+N). The New Project dialog box appears. 
    Under Choose Project pane, select General under Categories and Java Application under Projects. Click Next. 
    Under Name and Location pane, for the Project Name field, type in SerializeAndDeserializeCurrrentTime as project name. 
    For Create Main Class field, type in SerializeTime.  Click Finish. 
    Observe that SerializeAndDeserializeCurrrentTime project appears and IDE generated SerializeTime.java is displayed in the source editor window of NetBeans IDE. 
    2. Modify the IDE generated SerializeTime.java as shown in Code-1.11 below.  Study the code by paying special attention to the bold fonted parts. import java.io.ObjectOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;public class SerializeTime{
        
        public static void main(String [] args){
            
            String filename = "time.ser";
            if(args.length > 0) {
                filename = args[0];
            }
            
            // Create an object
            PersistentTime time = new PersistentTime();
            
            // Serialize the object instance and save it in
            // a file.
            FileOutputStream fos = null;
            ObjectOutputStream out = null;
            try {
                fos = new FileOutputStream(filename);
                out = new ObjectOutputStream(fos);
                out.writeObject(time);
                out.close();
            } catch(IOException ex) {
                ex.printStackTrace();
            }        System.out.println("Current time is saved into " + filename);
        }
    }
     
    Code-1.11: SerializeTime.java3. Write PersistentTime.java.import java.io.Serializable;
    import java.util.Date;
    import java.util.Calendar;public class PersistentTime implements Serializable{
        private Date time;
        
        public PersistentTime() {
            time = Calendar.getInstance().getTime();
        }
        
        public Date getTime() {
            return time;
        }
    }
     
    InputFile-1.12: farrago.txt3. Write DeserializeTime.java.import java.io.ObjectInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Calendar;public class DeserializeTime {
        
        public static void main(String [] args) {
            
            String filename = "time.ser";
            if(args.length > 0) {
                filename = args[0];
            }
            
            // Deserialize the previously saved 
            // PersistentTime object instance.
            PersistentTime time = null;
            FileInputStream fis = null;
            ObjectInputStream in = null;
            try {
                fis = new FileInputStream(filename);
                in = new ObjectInputStream(fis);
                time = (PersistentTime)in.readObject();
                in.close();
            } catch(IOException ex) {
                ex.printStackTrace();
            } catch(ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            
            // print out restored time
            System.out.println("Previously serialized time: " + time.getTime());
            
            // print out the current time
            System.out.println("Current time: " + Calendar.getInstance().getTime());
        }
    }
     
    InputFile-1.12: farrago.txt4. Build and run the Serialization part of the projectRight click SerializeTime.java project and select Run File. 
    Observe the result in the Output window. (Figure-1.13 below) 
    Current time is saved into time.ser
     
    Figure-1.13: Result of running SerializeAndDeserializeCurrrentTime application5. Build and run the Deserialization part of the projectRight click DeserializeTime.java project and select Run File. 
    Observe the result in the Output window. (Figure-1.13 below) 
    Previously serialized time: Mon Feb 26 01:57:16 EST 2007
    Current time: Mon Feb 26 01:58:58 EST 2007
     
    Figure-1.13: Result of running SerializeAndDeserializeCurrrentTime applicationSolution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as ./samples/SerializeAndDeserializeCurrrentTime.rar.  You can just open it and run it.  5. For your own exercise, do the following tasks. Build and run the application.Add another field called myName which is String type to the PersistentTIme class.Modify SerializeTime.java and DeserializeTime.java to display the myName field.                                                                                                              return to top of the exercise
    (1.2) Use transient keyword
    1. Modify PersistentTime.java as shown in Code-1.11 below. The code fragment that needs to be modified is highlighted in bold and blue-colored font. The change is to make the time field to be transient.import java.io.Serializable;
    import java.util.Date;
    import java.util.Calendar;public class PersistentTime implements Serializable{
        transient private Date time;
        
        public PersistentTime() {
            time = Calendar.getInstance().getTime();
        }
        
        public Date getTime() {
            return time;
        }
    }
     
    Code-1.11: SerializeTime.java2. Build and run the Serialization part of the projectRight click SerializeTime.java project and select Run File. 
    Observe the result in the Output window. (Figure-1.13 below) 
    Current time is saved into time.ser
     
    Figure-1.13: Result of running SerializeAndDeserializeCurrrentTime application3. Build and run the Deserialization part of the projectRight click DeserializeTime.java project and select Run File. 
    Observe the result in the Output window. (Figure-1.13 below) 
    Previously serialized time: null
    Current time: Mon Feb 26 02:07:09 EST 2007
     
    Figure-1.13: Result of running SerializeAndDeserializeCurrrentTime applicationSolution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as ./samples/SerializeAndDeserializeCurrrentTimeTransient.rar.  You can just open it and run it.  4. For your own exercise, do the following tasks. Build and run the application.Make myName field transient.Object that the field is not serialized.Summary
    In this exercise, you learned how to do serialization and deserialization of the an object.  You also learned how to use transient keyword.
                                                                                                                      return to the top
    It's time to have a break, just follow it:return to the top