Map an Object Property in a POJO given an ID from a JSON












0















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 '18 at 17:35
















0















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 '18 at 17:35














0












0








0








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 '18 at 17:35









Thomas Fritsch

5,351122133




5,351122133










asked Nov 14 '18 at 17:29









mariotepromariotepro

335




335








  • 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 '18 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 '18 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 '18 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 '18 at 17:35












1 Answer
1






active

oldest

votes


















2














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 '18 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',
autoActivateHeartbeat: false,
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









2














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 '18 at 11:31


















2














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 '18 at 11:31
















2












2








2







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 '18 at 18:06









Thomas FritschThomas Fritsch

5,351122133




5,351122133













  • 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 '18 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 '18 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 '18 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 '18 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.




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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python