Algorithm for generating serialVersionUID for java entities

Here is some way of generating serialVersionUID for java entities.
It is stable in sense that it depends only on your schema, not JVM/time or date of creation.
On the other hand it depends on all fields in entity.
If we have next n fields:
typeName_1 fieldName_1;
typeName_2 fieldName_2;

typeName_n fieldName_n;
we could calculate
serialVersionUID =
(fieldName_1.hashCode() XOR typeName_1.hashCode()) * 31^0+
(fieldName_2.hashCode() XOR typeName_2.hashCode()) * 31^1 + … +
(fieldName_n.hashCode() XOR typeName_n.hashCode()) * 31^n
where fieldName_i.hashCode() is hashCode() of fieldName_i string
(for int id; it will be “id”.hashCode())
and typeName_i.hashCode() is hashCode() of typeName_i string
(for int id; it will be “int”.hashCode())
so, we will be independent from
1) JVM
2) time/date of creation
and depend on
1) fields order
2) field types
3) field names

Advertisement

Author: Artem's Blog

Working on software and more...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: