user15980267
user15980267

Reputation: 47

Hibernate JPA Generate id

I want that the primary key "puid" gets automatically generated. However I the puid still is always 0. I tried different GeneratedValue variants (Sequence, Identity and Auto) but the result is the same.

This is my code:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long puid;

Does anyone have an idea how to fix that?

Upvotes: 1

Views: 1070

Answers (2)

Alien
Alien

Reputation: 15878

Use Wrapper object which supports null values as well.

Change long to Long

Upvotes: 2

v.ladynev
v.ladynev

Reputation: 19956

  1. puid has default value 0, when an object is just created.
  2. Hibernate thinks that you set puid value manually, Hibernate doesn't change it. Just change long to Long to have null initial value.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long puid;

Upvotes: 4

Related Questions