Map an Object Property in a POJO given an ID from a JSON
up vote
0
down vote
favorite
First of all, sorry, I cannot explain myself in any better way.
I am programming an example API, I have a POJO (with JPA) called Movie, so in my controller, I want to give it a JSON to insert a Movie.
Movie has a @ManyToOne(optional = false) property, relating another POJO called Genre (idGenre, Name)
I want to give in a JSON not an object with every property but an id, so:
CONTROLLER
@RequestMapping(value = "/sendMovie", method = RequestMethod.POST)
public void setMovie(@RequestBody Movie movie) {
mRepo.save(movie);
}
Movie
@Entity
public class Movie {
@Id
@Column(name = "ID_MOVIE", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long idMovie;
@Column(name = "NAME")
private String name;
@Column(name = "SYNOPSIS")
private String synopsis;
@Column(name = "POSTER")
private String poster;
@Column(name = "DIRECTOR")
private String director;
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idGenre")
@JsonIdentityReference(alwaysAsId = true)
@ManyToOne(optional = false)
@JoinColumn(name = "ID_GENRE", referencedColumnName = "ID_GENRE")
private Genre genre;
JSON I want to use
{
"name": "MATRIX",
"idGenre": 3,
"synopsis": "NEO DOING THINGS",
"poster": "matrix.jpg",
"director": "WACHOWSKIS"
}
Is there possibility to achieve that?
spring jpa jackson
add a comment |
up vote
0
down vote
favorite
First of all, sorry, I cannot explain myself in any better way.
I am programming an example API, I have a POJO (with JPA) called Movie, so in my controller, I want to give it a JSON to insert a Movie.
Movie has a @ManyToOne(optional = false) property, relating another POJO called Genre (idGenre, Name)
I want to give in a JSON not an object with every property but an id, so:
CONTROLLER
@RequestMapping(value = "/sendMovie", method = RequestMethod.POST)
public void setMovie(@RequestBody Movie movie) {
mRepo.save(movie);
}
Movie
@Entity
public class Movie {
@Id
@Column(name = "ID_MOVIE", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long idMovie;
@Column(name = "NAME")
private String name;
@Column(name = "SYNOPSIS")
private String synopsis;
@Column(name = "POSTER")
private String poster;
@Column(name = "DIRECTOR")
private String director;
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idGenre")
@JsonIdentityReference(alwaysAsId = true)
@ManyToOne(optional = false)
@JoinColumn(name = "ID_GENRE", referencedColumnName = "ID_GENRE")
private Genre genre;
JSON I want to use
{
"name": "MATRIX",
"idGenre": 3,
"synopsis": "NEO DOING THINGS",
"poster": "matrix.jpg",
"director": "WACHOWSKIS"
}
Is there possibility to achieve that?
spring jpa jackson
1
Sure. Design a class that matches with the JSON you want to send and receive, different from the Movie class, Let's call it a MovieCreationCommand. When you receive a MovieCreationCommand in your controller, create a Movie, initializing all its properties by getting them out of the command, and getting the genre from the database using the genre ID found in the command, then insert that movie.
– JB Nizet
Nov 14 at 17:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
First of all, sorry, I cannot explain myself in any better way.
I am programming an example API, I have a POJO (with JPA) called Movie, so in my controller, I want to give it a JSON to insert a Movie.
Movie has a @ManyToOne(optional = false) property, relating another POJO called Genre (idGenre, Name)
I want to give in a JSON not an object with every property but an id, so:
CONTROLLER
@RequestMapping(value = "/sendMovie", method = RequestMethod.POST)
public void setMovie(@RequestBody Movie movie) {
mRepo.save(movie);
}
Movie
@Entity
public class Movie {
@Id
@Column(name = "ID_MOVIE", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long idMovie;
@Column(name = "NAME")
private String name;
@Column(name = "SYNOPSIS")
private String synopsis;
@Column(name = "POSTER")
private String poster;
@Column(name = "DIRECTOR")
private String director;
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idGenre")
@JsonIdentityReference(alwaysAsId = true)
@ManyToOne(optional = false)
@JoinColumn(name = "ID_GENRE", referencedColumnName = "ID_GENRE")
private Genre genre;
JSON I want to use
{
"name": "MATRIX",
"idGenre": 3,
"synopsis": "NEO DOING THINGS",
"poster": "matrix.jpg",
"director": "WACHOWSKIS"
}
Is there possibility to achieve that?
spring jpa jackson
First of all, sorry, I cannot explain myself in any better way.
I am programming an example API, I have a POJO (with JPA) called Movie, so in my controller, I want to give it a JSON to insert a Movie.
Movie has a @ManyToOne(optional = false) property, relating another POJO called Genre (idGenre, Name)
I want to give in a JSON not an object with every property but an id, so:
CONTROLLER
@RequestMapping(value = "/sendMovie", method = RequestMethod.POST)
public void setMovie(@RequestBody Movie movie) {
mRepo.save(movie);
}
Movie
@Entity
public class Movie {
@Id
@Column(name = "ID_MOVIE", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long idMovie;
@Column(name = "NAME")
private String name;
@Column(name = "SYNOPSIS")
private String synopsis;
@Column(name = "POSTER")
private String poster;
@Column(name = "DIRECTOR")
private String director;
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idGenre")
@JsonIdentityReference(alwaysAsId = true)
@ManyToOne(optional = false)
@JoinColumn(name = "ID_GENRE", referencedColumnName = "ID_GENRE")
private Genre genre;
JSON I want to use
{
"name": "MATRIX",
"idGenre": 3,
"synopsis": "NEO DOING THINGS",
"poster": "matrix.jpg",
"director": "WACHOWSKIS"
}
Is there possibility to achieve that?
spring jpa jackson
spring jpa jackson
edited Nov 14 at 17:35
Thomas Fritsch
4,780121933
4,780121933
asked Nov 14 at 17:29
mariotepro
235
235
1
Sure. Design a class that matches with the JSON you want to send and receive, different from the Movie class, Let's call it a MovieCreationCommand. When you receive a MovieCreationCommand in your controller, create a Movie, initializing all its properties by getting them out of the command, and getting the genre from the database using the genre ID found in the command, then insert that movie.
– JB Nizet
Nov 14 at 17:35
add a comment |
1
Sure. Design a class that matches with the JSON you want to send and receive, different from the Movie class, Let's call it a MovieCreationCommand. When you receive a MovieCreationCommand in your controller, create a Movie, initializing all its properties by getting them out of the command, and getting the genre from the database using the genre ID found in the command, then insert that movie.
– JB Nizet
Nov 14 at 17:35
1
1
Sure. Design a class that matches with the JSON you want to send and receive, different from the Movie class, Let's call it a MovieCreationCommand. When you receive a MovieCreationCommand in your controller, create a Movie, initializing all its properties by getting them out of the command, and getting the genre from the database using the genre ID found in the command, then insert that movie.
– JB Nizet
Nov 14 at 17:35
Sure. Design a class that matches with the JSON you want to send and receive, different from the Movie class, Let's call it a MovieCreationCommand. When you receive a MovieCreationCommand in your controller, create a Movie, initializing all its properties by getting them out of the command, and getting the genre from the database using the genre ID found in the command, then insert that movie.
– JB Nizet
Nov 14 at 17:35
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
You did put the JsonIdentity... annotations at the wrong place.
You need to put these annotations on your @Genre class:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idGenre")
@JsonIdentityReference(alwaysAsId = true)
public class Genre {
@Id
@Column(...)
private Long idGenre;
//....
}
and remove these annotations from the property Genre genre in your Movie class.
You also need to tell Jackson with @JsonProperty("idGenre") that you want this property
serialized with name "idGenre".
@ManyToOne(optional = false)
@JoinColumn(name = "ID_GENRE", referencedColumnName = "ID_GENRE")
@JsonProperty("idGenre")
private Genre genre;
Then the JSON output will be something like this:
{
"name": "MATRIX",
"synopsis": "NEO DOING THINGS",
"poster": "matrix.jpg",
"director": "WACHOWSKIS",
"idGenre": 3
}
Thank you very much, Thomas, but I want to give that JSON as input to add a new movie into the DB, and i get a JSON parse error when using thiscom.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for:
– mariotepro
Nov 15 at 11:31
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You did put the JsonIdentity... annotations at the wrong place.
You need to put these annotations on your @Genre class:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idGenre")
@JsonIdentityReference(alwaysAsId = true)
public class Genre {
@Id
@Column(...)
private Long idGenre;
//....
}
and remove these annotations from the property Genre genre in your Movie class.
You also need to tell Jackson with @JsonProperty("idGenre") that you want this property
serialized with name "idGenre".
@ManyToOne(optional = false)
@JoinColumn(name = "ID_GENRE", referencedColumnName = "ID_GENRE")
@JsonProperty("idGenre")
private Genre genre;
Then the JSON output will be something like this:
{
"name": "MATRIX",
"synopsis": "NEO DOING THINGS",
"poster": "matrix.jpg",
"director": "WACHOWSKIS",
"idGenre": 3
}
Thank you very much, Thomas, but I want to give that JSON as input to add a new movie into the DB, and i get a JSON parse error when using thiscom.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for:
– mariotepro
Nov 15 at 11:31
add a comment |
up vote
2
down vote
You did put the JsonIdentity... annotations at the wrong place.
You need to put these annotations on your @Genre class:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idGenre")
@JsonIdentityReference(alwaysAsId = true)
public class Genre {
@Id
@Column(...)
private Long idGenre;
//....
}
and remove these annotations from the property Genre genre in your Movie class.
You also need to tell Jackson with @JsonProperty("idGenre") that you want this property
serialized with name "idGenre".
@ManyToOne(optional = false)
@JoinColumn(name = "ID_GENRE", referencedColumnName = "ID_GENRE")
@JsonProperty("idGenre")
private Genre genre;
Then the JSON output will be something like this:
{
"name": "MATRIX",
"synopsis": "NEO DOING THINGS",
"poster": "matrix.jpg",
"director": "WACHOWSKIS",
"idGenre": 3
}
Thank you very much, Thomas, but I want to give that JSON as input to add a new movie into the DB, and i get a JSON parse error when using thiscom.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for:
– mariotepro
Nov 15 at 11:31
add a comment |
up vote
2
down vote
up vote
2
down vote
You did put the JsonIdentity... annotations at the wrong place.
You need to put these annotations on your @Genre class:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idGenre")
@JsonIdentityReference(alwaysAsId = true)
public class Genre {
@Id
@Column(...)
private Long idGenre;
//....
}
and remove these annotations from the property Genre genre in your Movie class.
You also need to tell Jackson with @JsonProperty("idGenre") that you want this property
serialized with name "idGenre".
@ManyToOne(optional = false)
@JoinColumn(name = "ID_GENRE", referencedColumnName = "ID_GENRE")
@JsonProperty("idGenre")
private Genre genre;
Then the JSON output will be something like this:
{
"name": "MATRIX",
"synopsis": "NEO DOING THINGS",
"poster": "matrix.jpg",
"director": "WACHOWSKIS",
"idGenre": 3
}
You did put the JsonIdentity... annotations at the wrong place.
You need to put these annotations on your @Genre class:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idGenre")
@JsonIdentityReference(alwaysAsId = true)
public class Genre {
@Id
@Column(...)
private Long idGenre;
//....
}
and remove these annotations from the property Genre genre in your Movie class.
You also need to tell Jackson with @JsonProperty("idGenre") that you want this property
serialized with name "idGenre".
@ManyToOne(optional = false)
@JoinColumn(name = "ID_GENRE", referencedColumnName = "ID_GENRE")
@JsonProperty("idGenre")
private Genre genre;
Then the JSON output will be something like this:
{
"name": "MATRIX",
"synopsis": "NEO DOING THINGS",
"poster": "matrix.jpg",
"director": "WACHOWSKIS",
"idGenre": 3
}
answered Nov 14 at 18:06
Thomas Fritsch
4,780121933
4,780121933
Thank you very much, Thomas, but I want to give that JSON as input to add a new movie into the DB, and i get a JSON parse error when using thiscom.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for:
– mariotepro
Nov 15 at 11:31
add a comment |
Thank you very much, Thomas, but I want to give that JSON as input to add a new movie into the DB, and i get a JSON parse error when using thiscom.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for:
– mariotepro
Nov 15 at 11:31
Thank you very much, Thomas, but I want to give that JSON as input to add a new movie into the DB, and i get a JSON parse error when using this
com.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for:– mariotepro
Nov 15 at 11:31
Thank you very much, Thomas, but I want to give that JSON as input to add a new movie into the DB, and i get a JSON parse error when using this
com.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for:– mariotepro
Nov 15 at 11:31
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%2f53305756%2fmap-an-object-property-in-a-pojo-given-an-id-from-a-json%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
1
Sure. Design a class that matches with the JSON you want to send and receive, different from the Movie class, Let's call it a MovieCreationCommand. When you receive a MovieCreationCommand in your controller, create a Movie, initializing all its properties by getting them out of the command, and getting the genre from the database using the genre ID found in the command, then insert that movie.
– JB Nizet
Nov 14 at 17:35