#@Id return 0

1 messages · Page 1 of 1 (latest)

tough night
#

I'm trying to learn "spring boot" and I don't know if I'm doing something wrong or I don't understand something. I'm creating an entity class with variables with ID among others, so I want to automatically generate the ID using

@ID
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

and to get the id value I created the get method

private int getId(){ return id};

But every time in every object ID equals 0,
I tried using other types, changed Int to Integer and every time Id was 0 or zero.
Am I doing something wrong? Maybe I can't return this auto-generated ID?

mystic inletBOT
#

<@&1004656351647117403> please have a look, thanks.

mystic inletBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

echo slate
#

You can't get the ID of an object before saving it. This is because the ID is generated from a sequence which Hibernate does not fetch the value for on just an object initialization (at least not by default)

fringe dagger
#

yep, it needs to be persisted before it can be used