Hibernate @OneToOne unidirectional relation from “weak” entity
I have a User and ActivationKey entities.
The ActivationKey entity refers to a User entity created when the user is created and it's deleted once the user has visited a link that has been sent to his e-mail.
public class User implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
}
public class ActivationKey {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private Usuario usuario;
...
}
The thing is: when I delete de activation key, the user is also deleted because of the cascade.
Is there a way to avoid this but keep the automatic deletion the other way around, meaning that if a User gets deleted that hasn't been activated, it automatically deletes the corresponding ActivationKey before deleting the user (because of the FK)?
If I remove the (cascade = CascadeType.ALL) then when I try to delete a User that hasn't been activated, it fails because of the FK.
What am I missing here?
Thanks.
hibernate jpa cascading-deletes
add a comment |
I have a User and ActivationKey entities.
The ActivationKey entity refers to a User entity created when the user is created and it's deleted once the user has visited a link that has been sent to his e-mail.
public class User implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
}
public class ActivationKey {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private Usuario usuario;
...
}
The thing is: when I delete de activation key, the user is also deleted because of the cascade.
Is there a way to avoid this but keep the automatic deletion the other way around, meaning that if a User gets deleted that hasn't been activated, it automatically deletes the corresponding ActivationKey before deleting the user (because of the FK)?
If I remove the (cascade = CascadeType.ALL) then when I try to delete a User that hasn't been activated, it fails because of the FK.
What am I missing here?
Thanks.
hibernate jpa cascading-deletes
add a comment |
I have a User and ActivationKey entities.
The ActivationKey entity refers to a User entity created when the user is created and it's deleted once the user has visited a link that has been sent to his e-mail.
public class User implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
}
public class ActivationKey {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private Usuario usuario;
...
}
The thing is: when I delete de activation key, the user is also deleted because of the cascade.
Is there a way to avoid this but keep the automatic deletion the other way around, meaning that if a User gets deleted that hasn't been activated, it automatically deletes the corresponding ActivationKey before deleting the user (because of the FK)?
If I remove the (cascade = CascadeType.ALL) then when I try to delete a User that hasn't been activated, it fails because of the FK.
What am I missing here?
Thanks.
hibernate jpa cascading-deletes
I have a User and ActivationKey entities.
The ActivationKey entity refers to a User entity created when the user is created and it's deleted once the user has visited a link that has been sent to his e-mail.
public class User implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
}
public class ActivationKey {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private Usuario usuario;
...
}
The thing is: when I delete de activation key, the user is also deleted because of the cascade.
Is there a way to avoid this but keep the automatic deletion the other way around, meaning that if a User gets deleted that hasn't been activated, it automatically deletes the corresponding ActivationKey before deleting the user (because of the FK)?
If I remove the (cascade = CascadeType.ALL) then when I try to delete a User that hasn't been activated, it fails because of the FK.
What am I missing here?
Thanks.
hibernate jpa cascading-deletes
hibernate jpa cascading-deletes
edited Nov 21 '18 at 3:38
codeLover
2,2571620
2,2571620
asked Nov 21 '18 at 0:42
BeRniToBeRniTo
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In such scenario, you should declare the bidirectional relationship . You should also specify @OneToOne
in User
class as :
@OneToOne(cascade = CascadeType.ALL, orphanRemoval=true,mappedBy="usuario")
private ActivationKey activationKey;
Here you are basically specifying that User
is parent entity for ActivationKey
and you are specifying that for every action on User
, same action should be taken on ActivationKey
while vice versa may not be true(that will depend on the cascading type in ActivationKey
). mappedBy
attribute specifies that in child entity you will find the instance of parent entity by the value of this attribute.
In ActivationKey
class, remove cascade
type:
@OneToOne
@JoinColumn(name = "user_id")
private Usuario usuario;
Here since you are not mentioning any cascading type, thus, the operation performed on ActivationKey
alone will not show any impact on the parent User
object.
So there's no way to avoid the bidirectional relationship? It's kind of useless once the user has been activated.
– BeRniTo
Nov 21 '18 at 22:19
As per the scenarios mentioned by you, I don't think there is any other solution
– codeLover
Nov 22 '18 at 0:28
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%2f53403738%2fhibernate-onetoone-unidirectional-relation-from-weak-entity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In such scenario, you should declare the bidirectional relationship . You should also specify @OneToOne
in User
class as :
@OneToOne(cascade = CascadeType.ALL, orphanRemoval=true,mappedBy="usuario")
private ActivationKey activationKey;
Here you are basically specifying that User
is parent entity for ActivationKey
and you are specifying that for every action on User
, same action should be taken on ActivationKey
while vice versa may not be true(that will depend on the cascading type in ActivationKey
). mappedBy
attribute specifies that in child entity you will find the instance of parent entity by the value of this attribute.
In ActivationKey
class, remove cascade
type:
@OneToOne
@JoinColumn(name = "user_id")
private Usuario usuario;
Here since you are not mentioning any cascading type, thus, the operation performed on ActivationKey
alone will not show any impact on the parent User
object.
So there's no way to avoid the bidirectional relationship? It's kind of useless once the user has been activated.
– BeRniTo
Nov 21 '18 at 22:19
As per the scenarios mentioned by you, I don't think there is any other solution
– codeLover
Nov 22 '18 at 0:28
add a comment |
In such scenario, you should declare the bidirectional relationship . You should also specify @OneToOne
in User
class as :
@OneToOne(cascade = CascadeType.ALL, orphanRemoval=true,mappedBy="usuario")
private ActivationKey activationKey;
Here you are basically specifying that User
is parent entity for ActivationKey
and you are specifying that for every action on User
, same action should be taken on ActivationKey
while vice versa may not be true(that will depend on the cascading type in ActivationKey
). mappedBy
attribute specifies that in child entity you will find the instance of parent entity by the value of this attribute.
In ActivationKey
class, remove cascade
type:
@OneToOne
@JoinColumn(name = "user_id")
private Usuario usuario;
Here since you are not mentioning any cascading type, thus, the operation performed on ActivationKey
alone will not show any impact on the parent User
object.
So there's no way to avoid the bidirectional relationship? It's kind of useless once the user has been activated.
– BeRniTo
Nov 21 '18 at 22:19
As per the scenarios mentioned by you, I don't think there is any other solution
– codeLover
Nov 22 '18 at 0:28
add a comment |
In such scenario, you should declare the bidirectional relationship . You should also specify @OneToOne
in User
class as :
@OneToOne(cascade = CascadeType.ALL, orphanRemoval=true,mappedBy="usuario")
private ActivationKey activationKey;
Here you are basically specifying that User
is parent entity for ActivationKey
and you are specifying that for every action on User
, same action should be taken on ActivationKey
while vice versa may not be true(that will depend on the cascading type in ActivationKey
). mappedBy
attribute specifies that in child entity you will find the instance of parent entity by the value of this attribute.
In ActivationKey
class, remove cascade
type:
@OneToOne
@JoinColumn(name = "user_id")
private Usuario usuario;
Here since you are not mentioning any cascading type, thus, the operation performed on ActivationKey
alone will not show any impact on the parent User
object.
In such scenario, you should declare the bidirectional relationship . You should also specify @OneToOne
in User
class as :
@OneToOne(cascade = CascadeType.ALL, orphanRemoval=true,mappedBy="usuario")
private ActivationKey activationKey;
Here you are basically specifying that User
is parent entity for ActivationKey
and you are specifying that for every action on User
, same action should be taken on ActivationKey
while vice versa may not be true(that will depend on the cascading type in ActivationKey
). mappedBy
attribute specifies that in child entity you will find the instance of parent entity by the value of this attribute.
In ActivationKey
class, remove cascade
type:
@OneToOne
@JoinColumn(name = "user_id")
private Usuario usuario;
Here since you are not mentioning any cascading type, thus, the operation performed on ActivationKey
alone will not show any impact on the parent User
object.
answered Nov 21 '18 at 3:54
codeLovercodeLover
2,2571620
2,2571620
So there's no way to avoid the bidirectional relationship? It's kind of useless once the user has been activated.
– BeRniTo
Nov 21 '18 at 22:19
As per the scenarios mentioned by you, I don't think there is any other solution
– codeLover
Nov 22 '18 at 0:28
add a comment |
So there's no way to avoid the bidirectional relationship? It's kind of useless once the user has been activated.
– BeRniTo
Nov 21 '18 at 22:19
As per the scenarios mentioned by you, I don't think there is any other solution
– codeLover
Nov 22 '18 at 0:28
So there's no way to avoid the bidirectional relationship? It's kind of useless once the user has been activated.
– BeRniTo
Nov 21 '18 at 22:19
So there's no way to avoid the bidirectional relationship? It's kind of useless once the user has been activated.
– BeRniTo
Nov 21 '18 at 22:19
As per the scenarios mentioned by you, I don't think there is any other solution
– codeLover
Nov 22 '18 at 0:28
As per the scenarios mentioned by you, I don't think there is any other solution
– codeLover
Nov 22 '18 at 0:28
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%2f53403738%2fhibernate-onetoone-unidirectional-relation-from-weak-entity%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