I have some JPA models: "Category" and "Article":
@Entity
@Table(name = "categories")
public class Category {
private int id;
private String caption;
private Category parent;
private List<Category> childrenList;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
@ManyToOne
@JoinColumn(name = "parent_id")
public Category getParent() {
return parent;
}
public void setParent(Category parent) {
this.parent = parent;
}
@OneToMany
@JoinColumn(name = "parent_id")
public List<Category> getChildrenList() {
return childrenList;
}
public void setChildrenList(List<Category> childrenList) {
this.childrenList = childrenList;
}
}
@Entity
@Table(name = "articles")
public class Article {
private int id;
private String caption;
private boolean isAvailable;
private String description;
private int price;
private Category category;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
@Column(name = "is_available")
@Type(type = "org.hibernate.type.NumericBooleanType")
public boolean getIsAvailable() {
return isAvailable;
}
public void setIsAvailable(boolean available) {
isAvailable = available;
}
@Column
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@ManyToOne
@JoinColumn(name = "category_id")
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
Also i have some REST controller with two methods: 1)In the first method i need to get and serialize last 10 Articles, but i don't need "childrenList" and "parent" field in Categegory. 2)In the second method i need to get the same but serialize "parent" field.
How can i solve this? If i will use @JsonIgnore annotation to these fields then they will be never serialized. Or should i use DTO classes?
How can i dynamically set field for ignoring?