Spring Data : issue with OneToMany, child not saved properly









up vote
1
down vote

favorite












I am having trouble dealing with @OneToMany relationship.



Here is my code :



@Entity
@Table(name = "type_mouvement")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class TypeMouvement implements Serializable

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
private List<CompteTypeMouvement> comptes;
...


@Entity
@Table(name = "type_mouvement_comptes")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class CompteTypeMouvement implements Serializable
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String numCompte;

@ManyToOne
private TypeMouvement typeMouvement;
...



How I use these entities :



TypeMouvement typeMouvementFromDB = typeMouvementRepository.findOne(new Long(1));
CompteTypeMouvement compte = new CompteTypeMouvement();
compte.setNumCompte("123");
compte.setTypeMouvement(typeMouvementFromDB);
typeMouvementFromDB.getComptes().add(compte);
typeMouvementRepository.save(typeMouvementFromDB);


The result I get :



enter image description here



I thought I would get :



enter image description here



Why are the properties of CompteTypeMouvement not filled when I save TypeMouvement?










share|improve this question



























    up vote
    1
    down vote

    favorite












    I am having trouble dealing with @OneToMany relationship.



    Here is my code :



    @Entity
    @Table(name = "type_mouvement")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public class TypeMouvement implements Serializable

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
    private List<CompteTypeMouvement> comptes;
    ...


    @Entity
    @Table(name = "type_mouvement_comptes")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public class CompteTypeMouvement implements Serializable
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String numCompte;

    @ManyToOne
    private TypeMouvement typeMouvement;
    ...



    How I use these entities :



    TypeMouvement typeMouvementFromDB = typeMouvementRepository.findOne(new Long(1));
    CompteTypeMouvement compte = new CompteTypeMouvement();
    compte.setNumCompte("123");
    compte.setTypeMouvement(typeMouvementFromDB);
    typeMouvementFromDB.getComptes().add(compte);
    typeMouvementRepository.save(typeMouvementFromDB);


    The result I get :



    enter image description here



    I thought I would get :



    enter image description here



    Why are the properties of CompteTypeMouvement not filled when I save TypeMouvement?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am having trouble dealing with @OneToMany relationship.



      Here is my code :



      @Entity
      @Table(name = "type_mouvement")
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      public class TypeMouvement implements Serializable

      private static final long serialVersionUID = 1L;

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;

      @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
      private List<CompteTypeMouvement> comptes;
      ...


      @Entity
      @Table(name = "type_mouvement_comptes")
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      public class CompteTypeMouvement implements Serializable
      private static final long serialVersionUID = 1L;

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;

      private String numCompte;

      @ManyToOne
      private TypeMouvement typeMouvement;
      ...



      How I use these entities :



      TypeMouvement typeMouvementFromDB = typeMouvementRepository.findOne(new Long(1));
      CompteTypeMouvement compte = new CompteTypeMouvement();
      compte.setNumCompte("123");
      compte.setTypeMouvement(typeMouvementFromDB);
      typeMouvementFromDB.getComptes().add(compte);
      typeMouvementRepository.save(typeMouvementFromDB);


      The result I get :



      enter image description here



      I thought I would get :



      enter image description here



      Why are the properties of CompteTypeMouvement not filled when I save TypeMouvement?










      share|improve this question















      I am having trouble dealing with @OneToMany relationship.



      Here is my code :



      @Entity
      @Table(name = "type_mouvement")
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      public class TypeMouvement implements Serializable

      private static final long serialVersionUID = 1L;

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;

      @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
      private List<CompteTypeMouvement> comptes;
      ...


      @Entity
      @Table(name = "type_mouvement_comptes")
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      public class CompteTypeMouvement implements Serializable
      private static final long serialVersionUID = 1L;

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;

      private String numCompte;

      @ManyToOne
      private TypeMouvement typeMouvement;
      ...



      How I use these entities :



      TypeMouvement typeMouvementFromDB = typeMouvementRepository.findOne(new Long(1));
      CompteTypeMouvement compte = new CompteTypeMouvement();
      compte.setNumCompte("123");
      compte.setTypeMouvement(typeMouvementFromDB);
      typeMouvementFromDB.getComptes().add(compte);
      typeMouvementRepository.save(typeMouvementFromDB);


      The result I get :



      enter image description here



      I thought I would get :



      enter image description here



      Why are the properties of CompteTypeMouvement not filled when I save TypeMouvement?







      java hibernate jpa orm spring-data






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 21 hours ago









      Maciej Kowalski

      11.1k72037




      11.1k72037










      asked 21 hours ago









      thomas

      9110




      9110






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You are trying to invoke save passing an already existing entity:



          TypeMouvement typeMouvementFromDB = typeMouvementRepository.findOne(new Long(1));
          typeMouvementRepository.save(typeMouvementFromDB);


          The relationship has a persist cascade type only though:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
          private List<CompteTypeMouvement> comptes;


          The save impl is a follows (spring-data-jpa-1.11.3):



          public <S extends T> S save(S entity) 

          if (entityInformation.isNew(entity))
          em.persist(entity);
          return entity;
          else
          return em.merge(entity);




          Which means that a merge instead of a persist will be invoked.



          This if you add merge to cascade it should work:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,
          cascade = CascadeType.PERSIST, CascadeType.MERGE)
          private List<CompteTypeMouvement> comptes;





          share|improve this answer




















          • If he is saving a TypeMouvement then won't he need to add the cascade on the @ManyToOne?
            – Alan Hay
            20 hours ago










          • If he was saving CompteTypeMouvement then yes.. and in that case he would only need PERSIST cascade. But that is not the situation
            – Maciej Kowalski
            20 hours ago










          • Sorry, yes, of course mis-read the code.
            – Alan Hay
            19 hours ago











          • Thanks! Now I understand
            – thomas
            17 hours ago










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



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224098%2fspring-data-issue-with-onetomany-child-not-saved-properly%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          You are trying to invoke save passing an already existing entity:



          TypeMouvement typeMouvementFromDB = typeMouvementRepository.findOne(new Long(1));
          typeMouvementRepository.save(typeMouvementFromDB);


          The relationship has a persist cascade type only though:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
          private List<CompteTypeMouvement> comptes;


          The save impl is a follows (spring-data-jpa-1.11.3):



          public <S extends T> S save(S entity) 

          if (entityInformation.isNew(entity))
          em.persist(entity);
          return entity;
          else
          return em.merge(entity);




          Which means that a merge instead of a persist will be invoked.



          This if you add merge to cascade it should work:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,
          cascade = CascadeType.PERSIST, CascadeType.MERGE)
          private List<CompteTypeMouvement> comptes;





          share|improve this answer




















          • If he is saving a TypeMouvement then won't he need to add the cascade on the @ManyToOne?
            – Alan Hay
            20 hours ago










          • If he was saving CompteTypeMouvement then yes.. and in that case he would only need PERSIST cascade. But that is not the situation
            – Maciej Kowalski
            20 hours ago










          • Sorry, yes, of course mis-read the code.
            – Alan Hay
            19 hours ago











          • Thanks! Now I understand
            – thomas
            17 hours ago














          up vote
          2
          down vote



          accepted










          You are trying to invoke save passing an already existing entity:



          TypeMouvement typeMouvementFromDB = typeMouvementRepository.findOne(new Long(1));
          typeMouvementRepository.save(typeMouvementFromDB);


          The relationship has a persist cascade type only though:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
          private List<CompteTypeMouvement> comptes;


          The save impl is a follows (spring-data-jpa-1.11.3):



          public <S extends T> S save(S entity) 

          if (entityInformation.isNew(entity))
          em.persist(entity);
          return entity;
          else
          return em.merge(entity);




          Which means that a merge instead of a persist will be invoked.



          This if you add merge to cascade it should work:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,
          cascade = CascadeType.PERSIST, CascadeType.MERGE)
          private List<CompteTypeMouvement> comptes;





          share|improve this answer




















          • If he is saving a TypeMouvement then won't he need to add the cascade on the @ManyToOne?
            – Alan Hay
            20 hours ago










          • If he was saving CompteTypeMouvement then yes.. and in that case he would only need PERSIST cascade. But that is not the situation
            – Maciej Kowalski
            20 hours ago










          • Sorry, yes, of course mis-read the code.
            – Alan Hay
            19 hours ago











          • Thanks! Now I understand
            – thomas
            17 hours ago












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You are trying to invoke save passing an already existing entity:



          TypeMouvement typeMouvementFromDB = typeMouvementRepository.findOne(new Long(1));
          typeMouvementRepository.save(typeMouvementFromDB);


          The relationship has a persist cascade type only though:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
          private List<CompteTypeMouvement> comptes;


          The save impl is a follows (spring-data-jpa-1.11.3):



          public <S extends T> S save(S entity) 

          if (entityInformation.isNew(entity))
          em.persist(entity);
          return entity;
          else
          return em.merge(entity);




          Which means that a merge instead of a persist will be invoked.



          This if you add merge to cascade it should work:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,
          cascade = CascadeType.PERSIST, CascadeType.MERGE)
          private List<CompteTypeMouvement> comptes;





          share|improve this answer












          You are trying to invoke save passing an already existing entity:



          TypeMouvement typeMouvementFromDB = typeMouvementRepository.findOne(new Long(1));
          typeMouvementRepository.save(typeMouvementFromDB);


          The relationship has a persist cascade type only though:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
          private List<CompteTypeMouvement> comptes;


          The save impl is a follows (spring-data-jpa-1.11.3):



          public <S extends T> S save(S entity) 

          if (entityInformation.isNew(entity))
          em.persist(entity);
          return entity;
          else
          return em.merge(entity);




          Which means that a merge instead of a persist will be invoked.



          This if you add merge to cascade it should work:



          @OneToMany(mappedBy="typeMouvement", fetch = FetchType.EAGER,
          cascade = CascadeType.PERSIST, CascadeType.MERGE)
          private List<CompteTypeMouvement> comptes;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 21 hours ago









          Maciej Kowalski

          11.1k72037




          11.1k72037











          • If he is saving a TypeMouvement then won't he need to add the cascade on the @ManyToOne?
            – Alan Hay
            20 hours ago










          • If he was saving CompteTypeMouvement then yes.. and in that case he would only need PERSIST cascade. But that is not the situation
            – Maciej Kowalski
            20 hours ago










          • Sorry, yes, of course mis-read the code.
            – Alan Hay
            19 hours ago











          • Thanks! Now I understand
            – thomas
            17 hours ago
















          • If he is saving a TypeMouvement then won't he need to add the cascade on the @ManyToOne?
            – Alan Hay
            20 hours ago










          • If he was saving CompteTypeMouvement then yes.. and in that case he would only need PERSIST cascade. But that is not the situation
            – Maciej Kowalski
            20 hours ago










          • Sorry, yes, of course mis-read the code.
            – Alan Hay
            19 hours ago











          • Thanks! Now I understand
            – thomas
            17 hours ago















          If he is saving a TypeMouvement then won't he need to add the cascade on the @ManyToOne?
          – Alan Hay
          20 hours ago




          If he is saving a TypeMouvement then won't he need to add the cascade on the @ManyToOne?
          – Alan Hay
          20 hours ago












          If he was saving CompteTypeMouvement then yes.. and in that case he would only need PERSIST cascade. But that is not the situation
          – Maciej Kowalski
          20 hours ago




          If he was saving CompteTypeMouvement then yes.. and in that case he would only need PERSIST cascade. But that is not the situation
          – Maciej Kowalski
          20 hours ago












          Sorry, yes, of course mis-read the code.
          – Alan Hay
          19 hours ago





          Sorry, yes, of course mis-read the code.
          – Alan Hay
          19 hours ago













          Thanks! Now I understand
          – thomas
          17 hours ago




          Thanks! Now I understand
          – thomas
          17 hours ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224098%2fspring-data-issue-with-onetomany-child-not-saved-properly%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo