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?










share|improve this question




















  • 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















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?










share|improve this question




















  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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
}





share|improve this answer





















  • 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













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%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

























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
}





share|improve this answer





















  • 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

















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
}





share|improve this answer





















  • 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















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
}





share|improve this answer












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
}






share|improve this answer












share|improve this answer



share|improve this answer










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


















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




















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%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





















































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

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?