How to save a field to which long id will be converted in spring boot
up vote
-1
down vote
favorite
I followed this link:
https://vladmihalcea.com/how-to-map-calculated-properties-with-hibernate-generated-annotation/
Here is my source code:
private String firstName;
private String lastName;
private String middleName1;
private String middleName2;
private String middleName3;
private String middleName4;
private String middleName5;
@Generated( value = GenerationTime.ALWAYS )
@Column(columnDefinition =
"AS CONCAT(" +
" COALESCE(firstName, ''), " +
" COALESCE(' ' + middleName1, ''), " +
" COALESCE(' ' + middleName2, ''), " +
" COALESCE(' ' + middleName3, ''), " +
" COALESCE(' ' + middleName4, ''), " +
" COALESCE(' ' + middleName5, ''), " +
" COALESCE(' ' + lastName, '') " +
")")
private String fullName;
It uses other string
fields to generate that fullname
field.
What i want is something different.
I have an auto increment id field in spring boot class:
private long id //or it can be int, does not matter
I have also another field, which must be unique and alphanumeric.
private String code;
This field will have 6 alphanumeric (upper or lower case does not matter).
Is it possible to save this field auto while a new object created, with such a method?
Long.toString("id_field_value",36);
Actually, it gives:
0 -> 0
1 -> 1
10 -> A
So, when first object is created, id will be 1.
Any suggestion?
java spring jpa
add a comment |
up vote
-1
down vote
favorite
I followed this link:
https://vladmihalcea.com/how-to-map-calculated-properties-with-hibernate-generated-annotation/
Here is my source code:
private String firstName;
private String lastName;
private String middleName1;
private String middleName2;
private String middleName3;
private String middleName4;
private String middleName5;
@Generated( value = GenerationTime.ALWAYS )
@Column(columnDefinition =
"AS CONCAT(" +
" COALESCE(firstName, ''), " +
" COALESCE(' ' + middleName1, ''), " +
" COALESCE(' ' + middleName2, ''), " +
" COALESCE(' ' + middleName3, ''), " +
" COALESCE(' ' + middleName4, ''), " +
" COALESCE(' ' + middleName5, ''), " +
" COALESCE(' ' + lastName, '') " +
")")
private String fullName;
It uses other string
fields to generate that fullname
field.
What i want is something different.
I have an auto increment id field in spring boot class:
private long id //or it can be int, does not matter
I have also another field, which must be unique and alphanumeric.
private String code;
This field will have 6 alphanumeric (upper or lower case does not matter).
Is it possible to save this field auto while a new object created, with such a method?
Long.toString("id_field_value",36);
Actually, it gives:
0 -> 0
1 -> 1
10 -> A
So, when first object is created, id will be 1.
Any suggestion?
java spring jpa
Do you want to generate the code field based on id?
– Eugen Covaci
Nov 12 at 9:10
@EugenCovaci yes. Both are unique so no need to use other methods to generate another alphanumeric. id is already incremented and autogenerated.
– vegan
Nov 12 at 10:51
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I followed this link:
https://vladmihalcea.com/how-to-map-calculated-properties-with-hibernate-generated-annotation/
Here is my source code:
private String firstName;
private String lastName;
private String middleName1;
private String middleName2;
private String middleName3;
private String middleName4;
private String middleName5;
@Generated( value = GenerationTime.ALWAYS )
@Column(columnDefinition =
"AS CONCAT(" +
" COALESCE(firstName, ''), " +
" COALESCE(' ' + middleName1, ''), " +
" COALESCE(' ' + middleName2, ''), " +
" COALESCE(' ' + middleName3, ''), " +
" COALESCE(' ' + middleName4, ''), " +
" COALESCE(' ' + middleName5, ''), " +
" COALESCE(' ' + lastName, '') " +
")")
private String fullName;
It uses other string
fields to generate that fullname
field.
What i want is something different.
I have an auto increment id field in spring boot class:
private long id //or it can be int, does not matter
I have also another field, which must be unique and alphanumeric.
private String code;
This field will have 6 alphanumeric (upper or lower case does not matter).
Is it possible to save this field auto while a new object created, with such a method?
Long.toString("id_field_value",36);
Actually, it gives:
0 -> 0
1 -> 1
10 -> A
So, when first object is created, id will be 1.
Any suggestion?
java spring jpa
I followed this link:
https://vladmihalcea.com/how-to-map-calculated-properties-with-hibernate-generated-annotation/
Here is my source code:
private String firstName;
private String lastName;
private String middleName1;
private String middleName2;
private String middleName3;
private String middleName4;
private String middleName5;
@Generated( value = GenerationTime.ALWAYS )
@Column(columnDefinition =
"AS CONCAT(" +
" COALESCE(firstName, ''), " +
" COALESCE(' ' + middleName1, ''), " +
" COALESCE(' ' + middleName2, ''), " +
" COALESCE(' ' + middleName3, ''), " +
" COALESCE(' ' + middleName4, ''), " +
" COALESCE(' ' + middleName5, ''), " +
" COALESCE(' ' + lastName, '') " +
")")
private String fullName;
It uses other string
fields to generate that fullname
field.
What i want is something different.
I have an auto increment id field in spring boot class:
private long id //or it can be int, does not matter
I have also another field, which must be unique and alphanumeric.
private String code;
This field will have 6 alphanumeric (upper or lower case does not matter).
Is it possible to save this field auto while a new object created, with such a method?
Long.toString("id_field_value",36);
Actually, it gives:
0 -> 0
1 -> 1
10 -> A
So, when first object is created, id will be 1.
Any suggestion?
java spring jpa
java spring jpa
edited Nov 12 at 15:28
Bsquare
2,21111028
2,21111028
asked Nov 12 at 8:41
vegan
167
167
Do you want to generate the code field based on id?
– Eugen Covaci
Nov 12 at 9:10
@EugenCovaci yes. Both are unique so no need to use other methods to generate another alphanumeric. id is already incremented and autogenerated.
– vegan
Nov 12 at 10:51
add a comment |
Do you want to generate the code field based on id?
– Eugen Covaci
Nov 12 at 9:10
@EugenCovaci yes. Both are unique so no need to use other methods to generate another alphanumeric. id is already incremented and autogenerated.
– vegan
Nov 12 at 10:51
Do you want to generate the code field based on id?
– Eugen Covaci
Nov 12 at 9:10
Do you want to generate the code field based on id?
– Eugen Covaci
Nov 12 at 9:10
@EugenCovaci yes. Both are unique so no need to use other methods to generate another alphanumeric. id is already incremented and autogenerated.
– vegan
Nov 12 at 10:51
@EugenCovaci yes. Both are unique so no need to use other methods to generate another alphanumeric. id is already incremented and autogenerated.
– vegan
Nov 12 at 10:51
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Yes, it can be possible.
You can generate your code in a different method, that will return your alphanumeric value and set the code
property to the return value of your random method
No i want id value to be converted to that field. Not another
– vegan
Nov 12 at 9:08
I'm not clear on what you are asking but you can use.flush()
to get the id of the persisted object.
– Artion Hasani
Nov 12 at 9:11
It will autosave id. For example the id is 9000. code field will be long value of 9000. It will be saved auto
– vegan
Nov 12 at 10:50
add a comment |
up vote
0
down vote
Try the following annotations on your long field:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@SequenceGenerator(name="id_sequence", sequenceName="id_sequence", allocationSize=1)
private long id
Save this persistence object using hibernate. Make sure you have a sequence with the specified name in your schema.
Schema can be created by:
CREATE SEQUENCE schema_name.id_sequence
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1;
You can refer to the link incase you need some referance
It is nonid field.
– vegan
Nov 13 at 17:08
I believe if you remove @Id annotation and keep everything as it is now, things will work fine.
– abj1305
Nov 14 at 3:47
No. it is not working withoutid
field
– vegan
Nov 26 at 6:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258454%2fhow-to-save-a-field-to-which-long-id-will-be-converted-in-spring-boot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Yes, it can be possible.
You can generate your code in a different method, that will return your alphanumeric value and set the code
property to the return value of your random method
No i want id value to be converted to that field. Not another
– vegan
Nov 12 at 9:08
I'm not clear on what you are asking but you can use.flush()
to get the id of the persisted object.
– Artion Hasani
Nov 12 at 9:11
It will autosave id. For example the id is 9000. code field will be long value of 9000. It will be saved auto
– vegan
Nov 12 at 10:50
add a comment |
up vote
0
down vote
Yes, it can be possible.
You can generate your code in a different method, that will return your alphanumeric value and set the code
property to the return value of your random method
No i want id value to be converted to that field. Not another
– vegan
Nov 12 at 9:08
I'm not clear on what you are asking but you can use.flush()
to get the id of the persisted object.
– Artion Hasani
Nov 12 at 9:11
It will autosave id. For example the id is 9000. code field will be long value of 9000. It will be saved auto
– vegan
Nov 12 at 10:50
add a comment |
up vote
0
down vote
up vote
0
down vote
Yes, it can be possible.
You can generate your code in a different method, that will return your alphanumeric value and set the code
property to the return value of your random method
Yes, it can be possible.
You can generate your code in a different method, that will return your alphanumeric value and set the code
property to the return value of your random method
answered Nov 12 at 9:00
Artion Hasani
269
269
No i want id value to be converted to that field. Not another
– vegan
Nov 12 at 9:08
I'm not clear on what you are asking but you can use.flush()
to get the id of the persisted object.
– Artion Hasani
Nov 12 at 9:11
It will autosave id. For example the id is 9000. code field will be long value of 9000. It will be saved auto
– vegan
Nov 12 at 10:50
add a comment |
No i want id value to be converted to that field. Not another
– vegan
Nov 12 at 9:08
I'm not clear on what you are asking but you can use.flush()
to get the id of the persisted object.
– Artion Hasani
Nov 12 at 9:11
It will autosave id. For example the id is 9000. code field will be long value of 9000. It will be saved auto
– vegan
Nov 12 at 10:50
No i want id value to be converted to that field. Not another
– vegan
Nov 12 at 9:08
No i want id value to be converted to that field. Not another
– vegan
Nov 12 at 9:08
I'm not clear on what you are asking but you can use
.flush()
to get the id of the persisted object.– Artion Hasani
Nov 12 at 9:11
I'm not clear on what you are asking but you can use
.flush()
to get the id of the persisted object.– Artion Hasani
Nov 12 at 9:11
It will autosave id. For example the id is 9000. code field will be long value of 9000. It will be saved auto
– vegan
Nov 12 at 10:50
It will autosave id. For example the id is 9000. code field will be long value of 9000. It will be saved auto
– vegan
Nov 12 at 10:50
add a comment |
up vote
0
down vote
Try the following annotations on your long field:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@SequenceGenerator(name="id_sequence", sequenceName="id_sequence", allocationSize=1)
private long id
Save this persistence object using hibernate. Make sure you have a sequence with the specified name in your schema.
Schema can be created by:
CREATE SEQUENCE schema_name.id_sequence
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1;
You can refer to the link incase you need some referance
It is nonid field.
– vegan
Nov 13 at 17:08
I believe if you remove @Id annotation and keep everything as it is now, things will work fine.
– abj1305
Nov 14 at 3:47
No. it is not working withoutid
field
– vegan
Nov 26 at 6:21
add a comment |
up vote
0
down vote
Try the following annotations on your long field:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@SequenceGenerator(name="id_sequence", sequenceName="id_sequence", allocationSize=1)
private long id
Save this persistence object using hibernate. Make sure you have a sequence with the specified name in your schema.
Schema can be created by:
CREATE SEQUENCE schema_name.id_sequence
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1;
You can refer to the link incase you need some referance
It is nonid field.
– vegan
Nov 13 at 17:08
I believe if you remove @Id annotation and keep everything as it is now, things will work fine.
– abj1305
Nov 14 at 3:47
No. it is not working withoutid
field
– vegan
Nov 26 at 6:21
add a comment |
up vote
0
down vote
up vote
0
down vote
Try the following annotations on your long field:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@SequenceGenerator(name="id_sequence", sequenceName="id_sequence", allocationSize=1)
private long id
Save this persistence object using hibernate. Make sure you have a sequence with the specified name in your schema.
Schema can be created by:
CREATE SEQUENCE schema_name.id_sequence
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1;
You can refer to the link incase you need some referance
Try the following annotations on your long field:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@SequenceGenerator(name="id_sequence", sequenceName="id_sequence", allocationSize=1)
private long id
Save this persistence object using hibernate. Make sure you have a sequence with the specified name in your schema.
Schema can be created by:
CREATE SEQUENCE schema_name.id_sequence
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1;
You can refer to the link incase you need some referance
answered Nov 12 at 11:22
abj1305
16510
16510
It is nonid field.
– vegan
Nov 13 at 17:08
I believe if you remove @Id annotation and keep everything as it is now, things will work fine.
– abj1305
Nov 14 at 3:47
No. it is not working withoutid
field
– vegan
Nov 26 at 6:21
add a comment |
It is nonid field.
– vegan
Nov 13 at 17:08
I believe if you remove @Id annotation and keep everything as it is now, things will work fine.
– abj1305
Nov 14 at 3:47
No. it is not working withoutid
field
– vegan
Nov 26 at 6:21
It is nonid field.
– vegan
Nov 13 at 17:08
It is nonid field.
– vegan
Nov 13 at 17:08
I believe if you remove @Id annotation and keep everything as it is now, things will work fine.
– abj1305
Nov 14 at 3:47
I believe if you remove @Id annotation and keep everything as it is now, things will work fine.
– abj1305
Nov 14 at 3:47
No. it is not working without
id
field– vegan
Nov 26 at 6:21
No. it is not working without
id
field– vegan
Nov 26 at 6:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258454%2fhow-to-save-a-field-to-which-long-id-will-be-converted-in-spring-boot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you want to generate the code field based on id?
– Eugen Covaci
Nov 12 at 9:10
@EugenCovaci yes. Both are unique so no need to use other methods to generate another alphanumeric. id is already incremented and autogenerated.
– vegan
Nov 12 at 10:51