Understanding @JsonIgnore with JPA Hibernate

Understanding @JsonIgnore with JPA Hibernate

@JsonIgnore

  • Ignores from serialization and deserialization to and from JSON. Which means if you are calling REST API from anywhere then your attribute will not be present in JSON request/responses.
  • But it will be persisted by JPA persistence.

 

I have Coupon and Category Model with Many to Many mapping:

Here in coupon model I have category model with many to many mapping along with JsonIgnore annotation. So Here when I try to save coupon model then categories also saved, but When I try to retrieve coupon model then the categorylist is missing in the json response.

If you remove @JsonIgnore annotation from categoryList field then the category lists attribute will be available inside every coupons.

 

Coupon.java:

@Entity
public class Coupon implements Serializable{

public Coupon(){
}

@ManyToMany
@JsonIgnore
private Set categoryList;

public Set getCategoryList() {
return categoryList;
}

public void setCategoryList(Set categoryList) {
this.categoryList = categoryList;
}
}

 

Category.java:

 

@Entity
public class Category implements Serializable{
public Category(){
}

@ManyToMany(mappedBy="categoryList")
private Set coupon;

public Set getCoupon() {
return coupon;
}

public void setCoupon(Set coupon) {
this.coupon = coupon;
}
}

 

Now If I try to get the category list through coupon, then I am not getting anything in the JSON response, due to @JsonIgnore annotation.

So removed @JsonIgnore and the final coupon model is:

Coupon [After Removing @JsonIgnore for categoryList]

@Entity
public class Coupon implements Serializable{

public Coupon(){
}

@ManyToMany
private Set categoryList;

public Set getCategoryList() {
return categoryList;
}

public void setCategoryList(Set categoryList) {
this.categoryList = categoryList;
}
}

When to use @JsonIgnore ?

When you want to hide attributes from the Jackson Parser then use @JsonIgnore to the field which you do not want. You can also use @JsonIgnoreProperties for the same.

My Coupon Response with @JsonIgnore:

Here categoryList attribute does not appear in the JSON due to @JsonIgnore annotation for categoryList attribute.

[{"couponId":2043,"couponTitle":"Buy 3 Tshirts At Rs. 279/-","created_date":1504882385000}]

 

 

My Coupon Response without @JsonIgnore:

Here categoryList attribute appears in the JSON, here still no value, but atleast that attribute or field appears.

[{"couponId":2043,"couponTitle":"Buy 3 Tshirts At Rs. 279/-","categoryList":[],"created_date":1504882385000}]

 

I am trying to understand more details and differences between @JsonIgnore and @JsonIgnoreProperties, I will let you guys know once I found!

1,388 total views, 9 views today

About Mirthbees 681 Articles
Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.