Updating specific ManyToOne Entities without deleting
So I have an entity class "RecipeClientConfig" that has a many to one relationship with "RecipeClientConfigParam"
RecipeClientConfig:
@Entity
@Table(name="recipe_client_config", schema="app")
public class RecipeClientConfig {
@Id
@Column(name="recipe_client_config_id")
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequence-generator"
)
@SequenceGenerator(
name = "sequence-generator",
sequenceName = "app.recipe_client_config_seq",
allocationSize=1
)
private long recipeClientConfigId;
@Column(name="recipe_setup_id")
private int recipeSetupId;
@Column(name="personid", columnDefinition = "NVARCHAR(10)")
private String personId;
@Column(name="from_account_num")
private String fromAccountNum;
@Column(name="to_account_num")
private String toAccountNum;
@Column(name="status", columnDefinition = "CHAR(1)")
private String status;
@OneToMany(mappedBy="recipeClientConfig", cascade = CascadeType.ALL, fetch= FetchType.EAGER)
private List<RecipeClientConfigParam> configParams = new ArrayList<>();
RecipeClientConfigParam:
@Entity
@Table(name="recipe_client_config_params", schema="app")
public class RecipeClientConfigParam {
@Id
@Column(name="recipe_client_config_param_id")
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequence-generator"
)
@SequenceGenerator(
name = "sequence-generator",
sequenceName = "app.recipe_client_config_params_seq",
allocationSize=1
)
private long recipeClientConfigParamId;
@Column(name="param_name", columnDefinition = "NVARCHAR(50)")
private String paramName;
@Column(name="param_value", columnDefinition = "NVARCHAR(50)")
private String paramValue;
@ManyToOne
@JoinColumn(name = "recipe_client_config_id", nullable = false, updatable = false, insertable = true)
private RecipeClientConfig recipeClientConfig;
Now this RecipeConfiguration is able to be updated by the user where they will pass down DTO object and then mapped to a JPA entity. Some of these RecipeConfigParameters may or may not be updated.
This is my save method:
public void saveRecipeConfig (RecipeClientConfig recipeConfig) {
recipeConfig.setLastupdDat(LocalDateTime.now());
recipeConfig.setCreateDate(LocalDateTime.now());
List<RecipeClientConfigParam> params = recipeConfig.getConfigParams();
if (params != null) {
for (RecipeClientConfigParam param : params) {
param.setRecipeClientConfig(recipeConfig);
param.setCreateOp(recipeConfig.getPersonId());
param.setLastupdOp(recipeConfig.getPersonId());
param.setCreateDate(LocalDateTime.now());
param.setLastupdDat(LocalDateTime.now());
}
}
configRepository.saveAndFlush(recipeConfig);
}
If I just simply save the updated RecipeConfig entity object, all the fields and the RecipeConfigParameters that are not present in the update object but are present in the database will be deleted.
Not all the fields are shown in the code above for the sake of simplicity and some of the fields and Parameter objects really should not even be able to be updated.
I really want to avoid retrieving the RecipeConfig entity object using findById() or getOne() and then going through every single field and Parameter object to check if its already in the DB. Is there a better way to do this? Thanks!
java spring hibernate jpa persistence
add a comment |
So I have an entity class "RecipeClientConfig" that has a many to one relationship with "RecipeClientConfigParam"
RecipeClientConfig:
@Entity
@Table(name="recipe_client_config", schema="app")
public class RecipeClientConfig {
@Id
@Column(name="recipe_client_config_id")
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequence-generator"
)
@SequenceGenerator(
name = "sequence-generator",
sequenceName = "app.recipe_client_config_seq",
allocationSize=1
)
private long recipeClientConfigId;
@Column(name="recipe_setup_id")
private int recipeSetupId;
@Column(name="personid", columnDefinition = "NVARCHAR(10)")
private String personId;
@Column(name="from_account_num")
private String fromAccountNum;
@Column(name="to_account_num")
private String toAccountNum;
@Column(name="status", columnDefinition = "CHAR(1)")
private String status;
@OneToMany(mappedBy="recipeClientConfig", cascade = CascadeType.ALL, fetch= FetchType.EAGER)
private List<RecipeClientConfigParam> configParams = new ArrayList<>();
RecipeClientConfigParam:
@Entity
@Table(name="recipe_client_config_params", schema="app")
public class RecipeClientConfigParam {
@Id
@Column(name="recipe_client_config_param_id")
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequence-generator"
)
@SequenceGenerator(
name = "sequence-generator",
sequenceName = "app.recipe_client_config_params_seq",
allocationSize=1
)
private long recipeClientConfigParamId;
@Column(name="param_name", columnDefinition = "NVARCHAR(50)")
private String paramName;
@Column(name="param_value", columnDefinition = "NVARCHAR(50)")
private String paramValue;
@ManyToOne
@JoinColumn(name = "recipe_client_config_id", nullable = false, updatable = false, insertable = true)
private RecipeClientConfig recipeClientConfig;
Now this RecipeConfiguration is able to be updated by the user where they will pass down DTO object and then mapped to a JPA entity. Some of these RecipeConfigParameters may or may not be updated.
This is my save method:
public void saveRecipeConfig (RecipeClientConfig recipeConfig) {
recipeConfig.setLastupdDat(LocalDateTime.now());
recipeConfig.setCreateDate(LocalDateTime.now());
List<RecipeClientConfigParam> params = recipeConfig.getConfigParams();
if (params != null) {
for (RecipeClientConfigParam param : params) {
param.setRecipeClientConfig(recipeConfig);
param.setCreateOp(recipeConfig.getPersonId());
param.setLastupdOp(recipeConfig.getPersonId());
param.setCreateDate(LocalDateTime.now());
param.setLastupdDat(LocalDateTime.now());
}
}
configRepository.saveAndFlush(recipeConfig);
}
If I just simply save the updated RecipeConfig entity object, all the fields and the RecipeConfigParameters that are not present in the update object but are present in the database will be deleted.
Not all the fields are shown in the code above for the sake of simplicity and some of the fields and Parameter objects really should not even be able to be updated.
I really want to avoid retrieving the RecipeConfig entity object using findById() or getOne() and then going through every single field and Parameter object to check if its already in the DB. Is there a better way to do this? Thanks!
java spring hibernate jpa persistence
Possible duplicate of Update single field using spring data jpa
– Tom
Nov 23 '18 at 8:54
add a comment |
So I have an entity class "RecipeClientConfig" that has a many to one relationship with "RecipeClientConfigParam"
RecipeClientConfig:
@Entity
@Table(name="recipe_client_config", schema="app")
public class RecipeClientConfig {
@Id
@Column(name="recipe_client_config_id")
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequence-generator"
)
@SequenceGenerator(
name = "sequence-generator",
sequenceName = "app.recipe_client_config_seq",
allocationSize=1
)
private long recipeClientConfigId;
@Column(name="recipe_setup_id")
private int recipeSetupId;
@Column(name="personid", columnDefinition = "NVARCHAR(10)")
private String personId;
@Column(name="from_account_num")
private String fromAccountNum;
@Column(name="to_account_num")
private String toAccountNum;
@Column(name="status", columnDefinition = "CHAR(1)")
private String status;
@OneToMany(mappedBy="recipeClientConfig", cascade = CascadeType.ALL, fetch= FetchType.EAGER)
private List<RecipeClientConfigParam> configParams = new ArrayList<>();
RecipeClientConfigParam:
@Entity
@Table(name="recipe_client_config_params", schema="app")
public class RecipeClientConfigParam {
@Id
@Column(name="recipe_client_config_param_id")
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequence-generator"
)
@SequenceGenerator(
name = "sequence-generator",
sequenceName = "app.recipe_client_config_params_seq",
allocationSize=1
)
private long recipeClientConfigParamId;
@Column(name="param_name", columnDefinition = "NVARCHAR(50)")
private String paramName;
@Column(name="param_value", columnDefinition = "NVARCHAR(50)")
private String paramValue;
@ManyToOne
@JoinColumn(name = "recipe_client_config_id", nullable = false, updatable = false, insertable = true)
private RecipeClientConfig recipeClientConfig;
Now this RecipeConfiguration is able to be updated by the user where they will pass down DTO object and then mapped to a JPA entity. Some of these RecipeConfigParameters may or may not be updated.
This is my save method:
public void saveRecipeConfig (RecipeClientConfig recipeConfig) {
recipeConfig.setLastupdDat(LocalDateTime.now());
recipeConfig.setCreateDate(LocalDateTime.now());
List<RecipeClientConfigParam> params = recipeConfig.getConfigParams();
if (params != null) {
for (RecipeClientConfigParam param : params) {
param.setRecipeClientConfig(recipeConfig);
param.setCreateOp(recipeConfig.getPersonId());
param.setLastupdOp(recipeConfig.getPersonId());
param.setCreateDate(LocalDateTime.now());
param.setLastupdDat(LocalDateTime.now());
}
}
configRepository.saveAndFlush(recipeConfig);
}
If I just simply save the updated RecipeConfig entity object, all the fields and the RecipeConfigParameters that are not present in the update object but are present in the database will be deleted.
Not all the fields are shown in the code above for the sake of simplicity and some of the fields and Parameter objects really should not even be able to be updated.
I really want to avoid retrieving the RecipeConfig entity object using findById() or getOne() and then going through every single field and Parameter object to check if its already in the DB. Is there a better way to do this? Thanks!
java spring hibernate jpa persistence
So I have an entity class "RecipeClientConfig" that has a many to one relationship with "RecipeClientConfigParam"
RecipeClientConfig:
@Entity
@Table(name="recipe_client_config", schema="app")
public class RecipeClientConfig {
@Id
@Column(name="recipe_client_config_id")
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequence-generator"
)
@SequenceGenerator(
name = "sequence-generator",
sequenceName = "app.recipe_client_config_seq",
allocationSize=1
)
private long recipeClientConfigId;
@Column(name="recipe_setup_id")
private int recipeSetupId;
@Column(name="personid", columnDefinition = "NVARCHAR(10)")
private String personId;
@Column(name="from_account_num")
private String fromAccountNum;
@Column(name="to_account_num")
private String toAccountNum;
@Column(name="status", columnDefinition = "CHAR(1)")
private String status;
@OneToMany(mappedBy="recipeClientConfig", cascade = CascadeType.ALL, fetch= FetchType.EAGER)
private List<RecipeClientConfigParam> configParams = new ArrayList<>();
RecipeClientConfigParam:
@Entity
@Table(name="recipe_client_config_params", schema="app")
public class RecipeClientConfigParam {
@Id
@Column(name="recipe_client_config_param_id")
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequence-generator"
)
@SequenceGenerator(
name = "sequence-generator",
sequenceName = "app.recipe_client_config_params_seq",
allocationSize=1
)
private long recipeClientConfigParamId;
@Column(name="param_name", columnDefinition = "NVARCHAR(50)")
private String paramName;
@Column(name="param_value", columnDefinition = "NVARCHAR(50)")
private String paramValue;
@ManyToOne
@JoinColumn(name = "recipe_client_config_id", nullable = false, updatable = false, insertable = true)
private RecipeClientConfig recipeClientConfig;
Now this RecipeConfiguration is able to be updated by the user where they will pass down DTO object and then mapped to a JPA entity. Some of these RecipeConfigParameters may or may not be updated.
This is my save method:
public void saveRecipeConfig (RecipeClientConfig recipeConfig) {
recipeConfig.setLastupdDat(LocalDateTime.now());
recipeConfig.setCreateDate(LocalDateTime.now());
List<RecipeClientConfigParam> params = recipeConfig.getConfigParams();
if (params != null) {
for (RecipeClientConfigParam param : params) {
param.setRecipeClientConfig(recipeConfig);
param.setCreateOp(recipeConfig.getPersonId());
param.setLastupdOp(recipeConfig.getPersonId());
param.setCreateDate(LocalDateTime.now());
param.setLastupdDat(LocalDateTime.now());
}
}
configRepository.saveAndFlush(recipeConfig);
}
If I just simply save the updated RecipeConfig entity object, all the fields and the RecipeConfigParameters that are not present in the update object but are present in the database will be deleted.
Not all the fields are shown in the code above for the sake of simplicity and some of the fields and Parameter objects really should not even be able to be updated.
I really want to avoid retrieving the RecipeConfig entity object using findById() or getOne() and then going through every single field and Parameter object to check if its already in the DB. Is there a better way to do this? Thanks!
java spring hibernate jpa persistence
java spring hibernate jpa persistence
edited Nov 22 '18 at 22:22
Oscar S.
asked Nov 22 '18 at 22:13
Oscar S.Oscar S.
11
11
Possible duplicate of Update single field using spring data jpa
– Tom
Nov 23 '18 at 8:54
add a comment |
Possible duplicate of Update single field using spring data jpa
– Tom
Nov 23 '18 at 8:54
Possible duplicate of Update single field using spring data jpa
– Tom
Nov 23 '18 at 8:54
Possible duplicate of Update single field using spring data jpa
– Tom
Nov 23 '18 at 8:54
add a comment |
0
active
oldest
votes
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%2f53438543%2fupdating-specific-manytoone-entities-without-deleting%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53438543%2fupdating-specific-manytoone-entities-without-deleting%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
Possible duplicate of Update single field using spring data jpa
– Tom
Nov 23 '18 at 8:54