MongoRepository doesn't associate nested object with the class where is declared











up vote
0
down vote

favorite












I'm having some troubles with the MongoRepository when I try to execute a post to the FlightClass service.



So, I have a class Catering



@NoArgsConstructor @Getter @Setter @ToString
@Document(collection = "catering")
public class Catering {

@Id
private String id;

@Indexed(unique = true)
private String title;

public Catering(String title) {
this.title = title;
}
}


and another class FlightClass.



@NoArgsConstructor @Getter @Setter @ToString
@Document(collection = "flight-class")
public class FlightClass {

@Id
private String id;

private String title;

@DBRef
private Catering catering;
}


I declared my repository as follows:



@RepositoryRestResource(collectionResourceRel = "classes", path = "classes")
public interface FlightClassRepository extends
MongoRepository<FlightClass, String> {
}


But I'm having an issue when I try to do a POST



{
"title": "Classic",
"catering": {
"id": <some_id_that_already_exists>
}
}


And for some reason, spring-data doesn't associate the current record with the catering one.



The most bizarre thing is that I created a controller like the one below and it works.



@PostMapping("/flight-classes")
public ResponseEntity<FlightClass> post(@RequestBody FlightClass flightClass) {
return new ResponseEntity<>(flightClassRepository.insert(flightClass), HttpStatus.ACCEPTED);
}


Is this a bug in the Mongo repository? Or Am I doing something wrong?



These are my dependencies



...
dependencies {
implementation('org.springframework.boot:spring-boot-starter-aop')
implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
implementation('org.springframework.boot:spring-boot-starter-data-rest')
implementation('org.springframework.boot:spring-boot-starter-hateoas')
implementation('org.springframework.boot:spring-boot-starter-web')
runtimeOnly('org.springframework.boot:spring-boot-devtools')
compileOnly('org.projectlombok:lombok')
}


Does someone have an idea of why is this happening?



I will like just to clarify that I'm not trying to do saving in cascade (I'm aware that this is not supported yet by the framework). I've already created an instance of catering. I was just hoping to associate it with a class










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm having some troubles with the MongoRepository when I try to execute a post to the FlightClass service.



    So, I have a class Catering



    @NoArgsConstructor @Getter @Setter @ToString
    @Document(collection = "catering")
    public class Catering {

    @Id
    private String id;

    @Indexed(unique = true)
    private String title;

    public Catering(String title) {
    this.title = title;
    }
    }


    and another class FlightClass.



    @NoArgsConstructor @Getter @Setter @ToString
    @Document(collection = "flight-class")
    public class FlightClass {

    @Id
    private String id;

    private String title;

    @DBRef
    private Catering catering;
    }


    I declared my repository as follows:



    @RepositoryRestResource(collectionResourceRel = "classes", path = "classes")
    public interface FlightClassRepository extends
    MongoRepository<FlightClass, String> {
    }


    But I'm having an issue when I try to do a POST



    {
    "title": "Classic",
    "catering": {
    "id": <some_id_that_already_exists>
    }
    }


    And for some reason, spring-data doesn't associate the current record with the catering one.



    The most bizarre thing is that I created a controller like the one below and it works.



    @PostMapping("/flight-classes")
    public ResponseEntity<FlightClass> post(@RequestBody FlightClass flightClass) {
    return new ResponseEntity<>(flightClassRepository.insert(flightClass), HttpStatus.ACCEPTED);
    }


    Is this a bug in the Mongo repository? Or Am I doing something wrong?



    These are my dependencies



    ...
    dependencies {
    implementation('org.springframework.boot:spring-boot-starter-aop')
    implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
    implementation('org.springframework.boot:spring-boot-starter-data-rest')
    implementation('org.springframework.boot:spring-boot-starter-hateoas')
    implementation('org.springframework.boot:spring-boot-starter-web')
    runtimeOnly('org.springframework.boot:spring-boot-devtools')
    compileOnly('org.projectlombok:lombok')
    }


    Does someone have an idea of why is this happening?



    I will like just to clarify that I'm not trying to do saving in cascade (I'm aware that this is not supported yet by the framework). I've already created an instance of catering. I was just hoping to associate it with a class










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm having some troubles with the MongoRepository when I try to execute a post to the FlightClass service.



      So, I have a class Catering



      @NoArgsConstructor @Getter @Setter @ToString
      @Document(collection = "catering")
      public class Catering {

      @Id
      private String id;

      @Indexed(unique = true)
      private String title;

      public Catering(String title) {
      this.title = title;
      }
      }


      and another class FlightClass.



      @NoArgsConstructor @Getter @Setter @ToString
      @Document(collection = "flight-class")
      public class FlightClass {

      @Id
      private String id;

      private String title;

      @DBRef
      private Catering catering;
      }


      I declared my repository as follows:



      @RepositoryRestResource(collectionResourceRel = "classes", path = "classes")
      public interface FlightClassRepository extends
      MongoRepository<FlightClass, String> {
      }


      But I'm having an issue when I try to do a POST



      {
      "title": "Classic",
      "catering": {
      "id": <some_id_that_already_exists>
      }
      }


      And for some reason, spring-data doesn't associate the current record with the catering one.



      The most bizarre thing is that I created a controller like the one below and it works.



      @PostMapping("/flight-classes")
      public ResponseEntity<FlightClass> post(@RequestBody FlightClass flightClass) {
      return new ResponseEntity<>(flightClassRepository.insert(flightClass), HttpStatus.ACCEPTED);
      }


      Is this a bug in the Mongo repository? Or Am I doing something wrong?



      These are my dependencies



      ...
      dependencies {
      implementation('org.springframework.boot:spring-boot-starter-aop')
      implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
      implementation('org.springframework.boot:spring-boot-starter-data-rest')
      implementation('org.springframework.boot:spring-boot-starter-hateoas')
      implementation('org.springframework.boot:spring-boot-starter-web')
      runtimeOnly('org.springframework.boot:spring-boot-devtools')
      compileOnly('org.projectlombok:lombok')
      }


      Does someone have an idea of why is this happening?



      I will like just to clarify that I'm not trying to do saving in cascade (I'm aware that this is not supported yet by the framework). I've already created an instance of catering. I was just hoping to associate it with a class










      share|improve this question















      I'm having some troubles with the MongoRepository when I try to execute a post to the FlightClass service.



      So, I have a class Catering



      @NoArgsConstructor @Getter @Setter @ToString
      @Document(collection = "catering")
      public class Catering {

      @Id
      private String id;

      @Indexed(unique = true)
      private String title;

      public Catering(String title) {
      this.title = title;
      }
      }


      and another class FlightClass.



      @NoArgsConstructor @Getter @Setter @ToString
      @Document(collection = "flight-class")
      public class FlightClass {

      @Id
      private String id;

      private String title;

      @DBRef
      private Catering catering;
      }


      I declared my repository as follows:



      @RepositoryRestResource(collectionResourceRel = "classes", path = "classes")
      public interface FlightClassRepository extends
      MongoRepository<FlightClass, String> {
      }


      But I'm having an issue when I try to do a POST



      {
      "title": "Classic",
      "catering": {
      "id": <some_id_that_already_exists>
      }
      }


      And for some reason, spring-data doesn't associate the current record with the catering one.



      The most bizarre thing is that I created a controller like the one below and it works.



      @PostMapping("/flight-classes")
      public ResponseEntity<FlightClass> post(@RequestBody FlightClass flightClass) {
      return new ResponseEntity<>(flightClassRepository.insert(flightClass), HttpStatus.ACCEPTED);
      }


      Is this a bug in the Mongo repository? Or Am I doing something wrong?



      These are my dependencies



      ...
      dependencies {
      implementation('org.springframework.boot:spring-boot-starter-aop')
      implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
      implementation('org.springframework.boot:spring-boot-starter-data-rest')
      implementation('org.springframework.boot:spring-boot-starter-hateoas')
      implementation('org.springframework.boot:spring-boot-starter-web')
      runtimeOnly('org.springframework.boot:spring-boot-devtools')
      compileOnly('org.projectlombok:lombok')
      }


      Does someone have an idea of why is this happening?



      I will like just to clarify that I'm not trying to do saving in cascade (I'm aware that this is not supported yet by the framework). I've already created an instance of catering. I was just hoping to associate it with a class







      java spring spring-boot spring-data-mongodb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 18:19









      nullpointer

      37.5k1072145




      37.5k1072145










      asked Nov 8 at 14:09









      winter

      929826




      929826
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can find the answer in this link
          more specific in this two lines



          flightClass.setCatering(Stream.of(yourObjectfindByID).collect(Collectors.toSet()));
          flightClassRepo.save(flightClass);





          share|improve this answer





















          • Hi @Albert, my question is why if I'm using the annotation @RepositoryRestResource I'm not able to perform a POST request sending the nested object catering.
            – winter
            Nov 8 at 16:17











          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%2f53209461%2fmongorepository-doesnt-associate-nested-object-with-the-class-where-is-declared%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








          up vote
          0
          down vote













          You can find the answer in this link
          more specific in this two lines



          flightClass.setCatering(Stream.of(yourObjectfindByID).collect(Collectors.toSet()));
          flightClassRepo.save(flightClass);





          share|improve this answer





















          • Hi @Albert, my question is why if I'm using the annotation @RepositoryRestResource I'm not able to perform a POST request sending the nested object catering.
            – winter
            Nov 8 at 16:17















          up vote
          0
          down vote













          You can find the answer in this link
          more specific in this two lines



          flightClass.setCatering(Stream.of(yourObjectfindByID).collect(Collectors.toSet()));
          flightClassRepo.save(flightClass);





          share|improve this answer





















          • Hi @Albert, my question is why if I'm using the annotation @RepositoryRestResource I'm not able to perform a POST request sending the nested object catering.
            – winter
            Nov 8 at 16:17













          up vote
          0
          down vote










          up vote
          0
          down vote









          You can find the answer in this link
          more specific in this two lines



          flightClass.setCatering(Stream.of(yourObjectfindByID).collect(Collectors.toSet()));
          flightClassRepo.save(flightClass);





          share|improve this answer












          You can find the answer in this link
          more specific in this two lines



          flightClass.setCatering(Stream.of(yourObjectfindByID).collect(Collectors.toSet()));
          flightClassRepo.save(flightClass);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 16:01









          Alber

          708




          708












          • Hi @Albert, my question is why if I'm using the annotation @RepositoryRestResource I'm not able to perform a POST request sending the nested object catering.
            – winter
            Nov 8 at 16:17


















          • Hi @Albert, my question is why if I'm using the annotation @RepositoryRestResource I'm not able to perform a POST request sending the nested object catering.
            – winter
            Nov 8 at 16:17
















          Hi @Albert, my question is why if I'm using the annotation @RepositoryRestResource I'm not able to perform a POST request sending the nested object catering.
          – winter
          Nov 8 at 16:17




          Hi @Albert, my question is why if I'm using the annotation @RepositoryRestResource I'm not able to perform a POST request sending the nested object catering.
          – winter
          Nov 8 at 16:17


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53209461%2fmongorepository-doesnt-associate-nested-object-with-the-class-where-is-declared%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







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud