Hibernate @OneToOne unidirectional relation from “weak” entity












0















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.










share|improve this question





























    0















    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.










    share|improve this question



























      0












      0








      0








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 3:38









      codeLover

      2,2571620




      2,2571620










      asked Nov 21 '18 at 0:42









      BeRniToBeRniTo

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer
























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














          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.






          share|improve this answer
























          • 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
















          0














          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.






          share|improve this answer
























          • 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














          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)