Spring boot - save non-id field depending on id field while saving object
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, in same class:
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 for that class, 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?
For example, when i save Person
object,
the id
will be 1 automatically with JPA - hibarnate.
And also i want another field to be saved automatically, which is depending on id
field.
So, id is 1, that string code
must be 1 also.
Should i do this after creating object ? Like:
Person person = personRepository.save(person3);
person.setCode(person.getId().toString);//or another functions
java hibernate jpa
add a comment |
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, in same class:
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 for that class, 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?
For example, when i save Person
object,
the id
will be 1 automatically with JPA - hibarnate.
And also i want another field to be saved automatically, which is depending on id
field.
So, id is 1, that string code
must be 1 also.
Should i do this after creating object ? Like:
Person person = personRepository.save(person3);
person.setCode(person.getId().toString);//or another functions
java hibernate jpa
Apart form appearing completely pointless, you need to clarify what it is you want to do.You state this field will have 6 alphanumeric (upper or lower case does not matter) but also when string code must be 1 also. So what is it 1 char or 6 chars. Why do you specify 36 as an arg. What are you expecting that to do?
– Alan Hay
Nov 13 '18 at 16:05
I need another unique field. It must be alphanumeric. 26 capital letters + 10 numbers make 36 so i thought it will be best. E2E user will see this code on the UI and also we are using that field in some codes @AlanHay
– asdasasd
Nov 13 '18 at 16:44
add a comment |
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, in same class:
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 for that class, 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?
For example, when i save Person
object,
the id
will be 1 automatically with JPA - hibarnate.
And also i want another field to be saved automatically, which is depending on id
field.
So, id is 1, that string code
must be 1 also.
Should i do this after creating object ? Like:
Person person = personRepository.save(person3);
person.setCode(person.getId().toString);//or another functions
java hibernate jpa
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, in same class:
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 for that class, 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?
For example, when i save Person
object,
the id
will be 1 automatically with JPA - hibarnate.
And also i want another field to be saved automatically, which is depending on id
field.
So, id is 1, that string code
must be 1 also.
Should i do this after creating object ? Like:
Person person = personRepository.save(person3);
person.setCode(person.getId().toString);//or another functions
java hibernate jpa
java hibernate jpa
edited Nov 13 '18 at 16:06
Alan Hay
15.3k22769
15.3k22769
asked Nov 13 '18 at 14:16
asdasasdasdasasd
237
237
Apart form appearing completely pointless, you need to clarify what it is you want to do.You state this field will have 6 alphanumeric (upper or lower case does not matter) but also when string code must be 1 also. So what is it 1 char or 6 chars. Why do you specify 36 as an arg. What are you expecting that to do?
– Alan Hay
Nov 13 '18 at 16:05
I need another unique field. It must be alphanumeric. 26 capital letters + 10 numbers make 36 so i thought it will be best. E2E user will see this code on the UI and also we are using that field in some codes @AlanHay
– asdasasd
Nov 13 '18 at 16:44
add a comment |
Apart form appearing completely pointless, you need to clarify what it is you want to do.You state this field will have 6 alphanumeric (upper or lower case does not matter) but also when string code must be 1 also. So what is it 1 char or 6 chars. Why do you specify 36 as an arg. What are you expecting that to do?
– Alan Hay
Nov 13 '18 at 16:05
I need another unique field. It must be alphanumeric. 26 capital letters + 10 numbers make 36 so i thought it will be best. E2E user will see this code on the UI and also we are using that field in some codes @AlanHay
– asdasasd
Nov 13 '18 at 16:44
Apart form appearing completely pointless, you need to clarify what it is you want to do.You state this field will have 6 alphanumeric (upper or lower case does not matter) but also when string code must be 1 also. So what is it 1 char or 6 chars. Why do you specify 36 as an arg. What are you expecting that to do?
– Alan Hay
Nov 13 '18 at 16:05
Apart form appearing completely pointless, you need to clarify what it is you want to do.You state this field will have 6 alphanumeric (upper or lower case does not matter) but also when string code must be 1 also. So what is it 1 char or 6 chars. Why do you specify 36 as an arg. What are you expecting that to do?
– Alan Hay
Nov 13 '18 at 16:05
I need another unique field. It must be alphanumeric. 26 capital letters + 10 numbers make 36 so i thought it will be best. E2E user will see this code on the UI and also we are using that field in some codes @AlanHay
– asdasasd
Nov 13 '18 at 16:44
I need another unique field. It must be alphanumeric. 26 capital letters + 10 numbers make 36 so i thought it will be best. E2E user will see this code on the UI and also we are using that field in some codes @AlanHay
– asdasasd
Nov 13 '18 at 16:44
add a comment |
2 Answers
2
active
oldest
votes
You can try @PostPersist
annotation. You will have an id in your entity as soon as it is persisted. Something like:
@PostPersist
private void postPersist() {
this.setCode( generateMyCode( getId() ) );
}
This works fine as long as EntityManager
is used, the field code should also be persisted to the db.
When using Spring repositories it might be needed to perform extra save()
(I do not have to, having just default config) after initial save but you should test this approach with your configuration.
(Spring repositories handle persistence context a bit different way compared to the JPA standard usage with EntityManager
.)
EntityManager
is not default? I tried and thispostpersist
worked.
– asdasasd
Nov 26 '18 at 6:18
@asdasasd Default was a bit bad wording. Repository uses em but not in 'default mode' it starts transaction. When you just inject em all changes to managed entities propagate to db withoutpersist()
orsave()
. But with repositories - with default settings - you always need to callsave(..)
. AndpostPersist()
might work differently depending on the settings.
– pirho
Nov 26 '18 at 6:46
yes, i always userepository.save
. So, withpostpersist
it wont be problem?
– asdasasd
Nov 26 '18 at 8:10
Should not be a problem. I'm just unable to find documentation & proof about how Spring data actually handles this case.
– pirho
Nov 26 '18 at 9:03
1
@asdasasd You could make method static, inject it if possible to make it bean, or instantiate with new() when yoou need it. But I recommend you to do prepare a separate question considering that it is more helpful then to others also.
– pirho
Nov 29 '18 at 6:59
|
show 1 more comment
It seems like the code
column you want is just exactly the id
column but with another encoding (base 36 rather than base 10).
You don't need to store it, nor use a persistence annotation for that :
- For reading the code : on the entity, just add a getter method
getCode()
that compute it based on theid
value. - For searching by code, just convert the code from base 36 to base 10, then search by id.
But i need another unique thing to store.
– asdasasd
Nov 13 '18 at 14:56
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',
autoActivateHeartbeat: false,
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%2f53283009%2fspring-boot-save-non-id-field-depending-on-id-field-while-saving-object%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
You can try @PostPersist
annotation. You will have an id in your entity as soon as it is persisted. Something like:
@PostPersist
private void postPersist() {
this.setCode( generateMyCode( getId() ) );
}
This works fine as long as EntityManager
is used, the field code should also be persisted to the db.
When using Spring repositories it might be needed to perform extra save()
(I do not have to, having just default config) after initial save but you should test this approach with your configuration.
(Spring repositories handle persistence context a bit different way compared to the JPA standard usage with EntityManager
.)
EntityManager
is not default? I tried and thispostpersist
worked.
– asdasasd
Nov 26 '18 at 6:18
@asdasasd Default was a bit bad wording. Repository uses em but not in 'default mode' it starts transaction. When you just inject em all changes to managed entities propagate to db withoutpersist()
orsave()
. But with repositories - with default settings - you always need to callsave(..)
. AndpostPersist()
might work differently depending on the settings.
– pirho
Nov 26 '18 at 6:46
yes, i always userepository.save
. So, withpostpersist
it wont be problem?
– asdasasd
Nov 26 '18 at 8:10
Should not be a problem. I'm just unable to find documentation & proof about how Spring data actually handles this case.
– pirho
Nov 26 '18 at 9:03
1
@asdasasd You could make method static, inject it if possible to make it bean, or instantiate with new() when yoou need it. But I recommend you to do prepare a separate question considering that it is more helpful then to others also.
– pirho
Nov 29 '18 at 6:59
|
show 1 more comment
You can try @PostPersist
annotation. You will have an id in your entity as soon as it is persisted. Something like:
@PostPersist
private void postPersist() {
this.setCode( generateMyCode( getId() ) );
}
This works fine as long as EntityManager
is used, the field code should also be persisted to the db.
When using Spring repositories it might be needed to perform extra save()
(I do not have to, having just default config) after initial save but you should test this approach with your configuration.
(Spring repositories handle persistence context a bit different way compared to the JPA standard usage with EntityManager
.)
EntityManager
is not default? I tried and thispostpersist
worked.
– asdasasd
Nov 26 '18 at 6:18
@asdasasd Default was a bit bad wording. Repository uses em but not in 'default mode' it starts transaction. When you just inject em all changes to managed entities propagate to db withoutpersist()
orsave()
. But with repositories - with default settings - you always need to callsave(..)
. AndpostPersist()
might work differently depending on the settings.
– pirho
Nov 26 '18 at 6:46
yes, i always userepository.save
. So, withpostpersist
it wont be problem?
– asdasasd
Nov 26 '18 at 8:10
Should not be a problem. I'm just unable to find documentation & proof about how Spring data actually handles this case.
– pirho
Nov 26 '18 at 9:03
1
@asdasasd You could make method static, inject it if possible to make it bean, or instantiate with new() when yoou need it. But I recommend you to do prepare a separate question considering that it is more helpful then to others also.
– pirho
Nov 29 '18 at 6:59
|
show 1 more comment
You can try @PostPersist
annotation. You will have an id in your entity as soon as it is persisted. Something like:
@PostPersist
private void postPersist() {
this.setCode( generateMyCode( getId() ) );
}
This works fine as long as EntityManager
is used, the field code should also be persisted to the db.
When using Spring repositories it might be needed to perform extra save()
(I do not have to, having just default config) after initial save but you should test this approach with your configuration.
(Spring repositories handle persistence context a bit different way compared to the JPA standard usage with EntityManager
.)
You can try @PostPersist
annotation. You will have an id in your entity as soon as it is persisted. Something like:
@PostPersist
private void postPersist() {
this.setCode( generateMyCode( getId() ) );
}
This works fine as long as EntityManager
is used, the field code should also be persisted to the db.
When using Spring repositories it might be needed to perform extra save()
(I do not have to, having just default config) after initial save but you should test this approach with your configuration.
(Spring repositories handle persistence context a bit different way compared to the JPA standard usage with EntityManager
.)
answered Nov 25 '18 at 10:50
pirhopirho
4,005101830
4,005101830
EntityManager
is not default? I tried and thispostpersist
worked.
– asdasasd
Nov 26 '18 at 6:18
@asdasasd Default was a bit bad wording. Repository uses em but not in 'default mode' it starts transaction. When you just inject em all changes to managed entities propagate to db withoutpersist()
orsave()
. But with repositories - with default settings - you always need to callsave(..)
. AndpostPersist()
might work differently depending on the settings.
– pirho
Nov 26 '18 at 6:46
yes, i always userepository.save
. So, withpostpersist
it wont be problem?
– asdasasd
Nov 26 '18 at 8:10
Should not be a problem. I'm just unable to find documentation & proof about how Spring data actually handles this case.
– pirho
Nov 26 '18 at 9:03
1
@asdasasd You could make method static, inject it if possible to make it bean, or instantiate with new() when yoou need it. But I recommend you to do prepare a separate question considering that it is more helpful then to others also.
– pirho
Nov 29 '18 at 6:59
|
show 1 more comment
EntityManager
is not default? I tried and thispostpersist
worked.
– asdasasd
Nov 26 '18 at 6:18
@asdasasd Default was a bit bad wording. Repository uses em but not in 'default mode' it starts transaction. When you just inject em all changes to managed entities propagate to db withoutpersist()
orsave()
. But with repositories - with default settings - you always need to callsave(..)
. AndpostPersist()
might work differently depending on the settings.
– pirho
Nov 26 '18 at 6:46
yes, i always userepository.save
. So, withpostpersist
it wont be problem?
– asdasasd
Nov 26 '18 at 8:10
Should not be a problem. I'm just unable to find documentation & proof about how Spring data actually handles this case.
– pirho
Nov 26 '18 at 9:03
1
@asdasasd You could make method static, inject it if possible to make it bean, or instantiate with new() when yoou need it. But I recommend you to do prepare a separate question considering that it is more helpful then to others also.
– pirho
Nov 29 '18 at 6:59
EntityManager
is not default? I tried and this postpersist
worked.– asdasasd
Nov 26 '18 at 6:18
EntityManager
is not default? I tried and this postpersist
worked.– asdasasd
Nov 26 '18 at 6:18
@asdasasd Default was a bit bad wording. Repository uses em but not in 'default mode' it starts transaction. When you just inject em all changes to managed entities propagate to db without
persist()
or save()
. But with repositories - with default settings - you always need to call save(..)
. And postPersist()
might work differently depending on the settings.– pirho
Nov 26 '18 at 6:46
@asdasasd Default was a bit bad wording. Repository uses em but not in 'default mode' it starts transaction. When you just inject em all changes to managed entities propagate to db without
persist()
or save()
. But with repositories - with default settings - you always need to call save(..)
. And postPersist()
might work differently depending on the settings.– pirho
Nov 26 '18 at 6:46
yes, i always use
repository.save
. So, with postpersist
it wont be problem?– asdasasd
Nov 26 '18 at 8:10
yes, i always use
repository.save
. So, with postpersist
it wont be problem?– asdasasd
Nov 26 '18 at 8:10
Should not be a problem. I'm just unable to find documentation & proof about how Spring data actually handles this case.
– pirho
Nov 26 '18 at 9:03
Should not be a problem. I'm just unable to find documentation & proof about how Spring data actually handles this case.
– pirho
Nov 26 '18 at 9:03
1
1
@asdasasd You could make method static, inject it if possible to make it bean, or instantiate with new() when yoou need it. But I recommend you to do prepare a separate question considering that it is more helpful then to others also.
– pirho
Nov 29 '18 at 6:59
@asdasasd You could make method static, inject it if possible to make it bean, or instantiate with new() when yoou need it. But I recommend you to do prepare a separate question considering that it is more helpful then to others also.
– pirho
Nov 29 '18 at 6:59
|
show 1 more comment
It seems like the code
column you want is just exactly the id
column but with another encoding (base 36 rather than base 10).
You don't need to store it, nor use a persistence annotation for that :
- For reading the code : on the entity, just add a getter method
getCode()
that compute it based on theid
value. - For searching by code, just convert the code from base 36 to base 10, then search by id.
But i need another unique thing to store.
– asdasasd
Nov 13 '18 at 14:56
add a comment |
It seems like the code
column you want is just exactly the id
column but with another encoding (base 36 rather than base 10).
You don't need to store it, nor use a persistence annotation for that :
- For reading the code : on the entity, just add a getter method
getCode()
that compute it based on theid
value. - For searching by code, just convert the code from base 36 to base 10, then search by id.
But i need another unique thing to store.
– asdasasd
Nov 13 '18 at 14:56
add a comment |
It seems like the code
column you want is just exactly the id
column but with another encoding (base 36 rather than base 10).
You don't need to store it, nor use a persistence annotation for that :
- For reading the code : on the entity, just add a getter method
getCode()
that compute it based on theid
value. - For searching by code, just convert the code from base 36 to base 10, then search by id.
It seems like the code
column you want is just exactly the id
column but with another encoding (base 36 rather than base 10).
You don't need to store it, nor use a persistence annotation for that :
- For reading the code : on the entity, just add a getter method
getCode()
that compute it based on theid
value. - For searching by code, just convert the code from base 36 to base 10, then search by id.
answered Nov 13 '18 at 14:45
ThierryThierry
3,6122129
3,6122129
But i need another unique thing to store.
– asdasasd
Nov 13 '18 at 14:56
add a comment |
But i need another unique thing to store.
– asdasasd
Nov 13 '18 at 14:56
But i need another unique thing to store.
– asdasasd
Nov 13 '18 at 14:56
But i need another unique thing to store.
– asdasasd
Nov 13 '18 at 14:56
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.
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%2f53283009%2fspring-boot-save-non-id-field-depending-on-id-field-while-saving-object%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
Apart form appearing completely pointless, you need to clarify what it is you want to do.You state this field will have 6 alphanumeric (upper or lower case does not matter) but also when string code must be 1 also. So what is it 1 char or 6 chars. Why do you specify 36 as an arg. What are you expecting that to do?
– Alan Hay
Nov 13 '18 at 16:05
I need another unique field. It must be alphanumeric. 26 capital letters + 10 numbers make 36 so i thought it will be best. E2E user will see this code on the UI and also we are using that field in some codes @AlanHay
– asdasasd
Nov 13 '18 at 16:44