Itβs difficult for me to determine how to correctly identify and annotate models in my web application so that they can be used effectively both in the web interface and in the REST web service. Here is a simplified version of the relationship that is causing me problems:
Publication Model:
@Entity
@Table(name = "POST")
public class Post implements Serializable {
@Id
@Column(name = "POST_ID")
@GeneratedValue(strategy-GenerationType.AUTO)
private Integer postId;
@Column(name = "POST_BODY")
private String postBody;
@ManyToMany(fetch = FetchType.EAGER)
@Cascade({CascadeType.SAVE_UPDATE})
@JoinTable(name = "POST_TAGS",
joinColumns={@JoinColumn(name="POST_ID")},
inverseJoinColumns={@JoinColumn(name="TAG_ID")})
private Set<Tag> tags = new HashSet<Tag>();
}
Tag model
@Entity
@Table(name = "TAG")
public class Tag implements Serializable {
@Id
@Column(name = "TAG_ID")
@GeneratedValue(strategy-GenerationType.AUTO)
private Integer tagId;
@Column(name = "TAG_NAME")
private String tagName;
@ManyToMany(fetch = FetchType.EAGER, mappedBy="tags")
private Set<Post> posts = new HashSet<Post>();
}
I have one web services controller method for retrieving all messages and one method for retrieving all tags. Each method should ideally return a list of the target class plus reference classes. For instance:
[{
postId: 1,
postBody: "Hello world!",
tags: [{
tagId: 1,
tagName: "hello"
},{
tagId: 2,
tagName: "message"
}]
}, {
postId: 2,
....
}]
, , - , , .. . @JsonIgnore getter , . @JsonIdentityInfo , , , . , , , , .