degath
degath

Reputation: 1621

Unique annotation @GeneratedValue

What I want to do is add (POST) resource with automaticly generated id. I added annotations and my model looks like this

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String brand;

Question is why when I POST some value without id for example:

{
    "brand": "sony"
}

I GET automaticly id = 0:

{
    "id": 0,
    "brand": "sony"
}

And if I post more resources without calling id they all have id = 0 (so it's not unique).

What I do wrong?

Upvotes: 1

Views: 376

Answers (1)

Naman
Naman

Reputation: 31868

I GET automaticly id = 0:

That is because you are using a primitive data type long and its default value is 0.

And the serialization-deserialization of such fields would end up appending the default value of the primitive data types if not assigned explicitly.

In cases where you need to exclude such fields when there is not value set, you might intend to use Long(reference type) instead.

Upvotes: 1

Related Questions