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
java spring spring-boot spring-data-mongodb
add a comment |
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
java spring spring-boot spring-data-mongodb
add a comment |
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
java spring spring-boot spring-data-mongodb
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
java spring spring-boot spring-data-mongodb
edited Nov 8 at 18:19
nullpointer
37.5k1072145
37.5k1072145
asked Nov 8 at 14:09
winter
929826
929826
add a comment |
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
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.
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.
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%2f53209461%2fmongorepository-doesnt-associate-nested-object-with-the-class-where-is-declared%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