Generating unique serializable id for each of the generated classes in JAXB
Asked Answered
S

2

12

I am using ant wsimport to generate client stub from the wsdls. Also, I would like to generate client classes that implements Serializable. I would like to generate a different serialVersionUID for each class. I tried with the binding file that was shown below. But its generating same serialVersionUID for all the classes. Is there any way I can give my own serialVersionUID to each class?

<wsimport xendorsed="true" binding="binding.xml" debug="true" keep="true" 
verbose="false"  sourcedestdir="${generated}" wsdl="${src}${wsdl.file}"
wsdlLocation="${wsdl.file}">
</wsimport>

binding configuration

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <globalBindings>   
        <serializable uid="1" />        
    </globalBindings>    
</bindings>
Suasion answered 15/12, 2014 at 5:41 Comment(5)
Waitaminute... If you implement Serializable you are binding yourself for a contract for life; generating random UUIDs?Avowal
@Avowal The OP does not say he wants random uids, the OP wants different uids for different classes (i.e. not always the same/1). Theoretically it is possible to generate a uid based on the contents of the class. Different contents - different uids. So the question makes sense to me.Powers
<xjc:serializable uid="1"/> customization can only occur within your <jaxb:globalBindings> see here. Anyway you can add a work around using binding for each element generated.Eboni
why do they need to be unique?Revivify
Possible duplicate of How to generate a Java class which implements Serializable interface from xsd using JAXB?Sosa
A
3

This is the binding file we use, which does the trick for us.

<xs:schema elementFormDefault="qualified" version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="2.0"
jaxb:extensionBindingPrefixes="xjc">
<xs:annotation>
    <xs:appinfo>
        <jaxb:globalBindings>
            <xjc:serializable />
        </jaxb:globalBindings>
    </xs:appinfo>
</xs:annotation>

Andesine answered 22/1, 2015 at 7:36 Comment(1)
No, it’s not a complete schema, but the relevant bit to insert into your schema. At least, it did the trick to insert these lines into my WSDL file.Jink
A
3

Just for the record, there is no way to generate a unique serialVersionUID for each generated class because it doesn't make sense to do so.

Let me explain : A serialVersionUID represents a version of your class at a particular point in time. If you modify your class, your serialVersionUID should change. So when the JDK deserialize objects of the same class, it knows to which version of your class to deserialize it to.

In the case of JAXB, since you generate all your classes at once every time it doesn't make sense to version all the classes individually. Simply because they can only change as a group. (Unless you take them out of your target folder..)

I hope that makes a little bit more sense.

Abandoned answered 24/1, 2015 at 2:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.