samedi 7 février 2015

loop to get json spring for Many-To-Many relation with Rest API


Vote count:

0




I have two ORM object (person , jobtitle) and they have ManayToMany Relation.like this



@Entity(name = "person")//JQL
@Table(name = "PERSON")//JDBC
@EntityListeners(value = PersonManager.class)
public class Person implements Serializable {
private Long id;
private String name;
private String family;
private String mobile;
private String userName;
private String password;
private List<JobTitle> jobTitle;
public Person() {
}

public Person(Long id) {
this.id = id;
}

public Person(String name, String family, String mobile, String userName, String password) {
this.name = name;
this.family = family;
this.mobile = mobile;
this.userName = userName;
this.password = password;
}

@Id
@Column(name = "ID",columnDefinition = "int")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Basic
@Column(name = "NAME", columnDefinition = "varchar(100)")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Basic
@Column(name = "FAMILY", columnDefinition = "varchar(100)")
public String getFamily() {
return family;
}

public void setFamily(String family) {
this.family = family;
}

@Basic
@Column(name = "MOBILE", columnDefinition = "varchar(11)")
public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

@Basic
@Column(name = "USERNAME", columnDefinition = "varchar(30)")
public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

@Basic
@Column(name = "PASSWORD", columnDefinition = "varchar(128)")
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "PER_TITLE", joinColumns = {
@JoinColumn(name = "PERSON_ID", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "JOBTITLE_ID",
nullable = false, updatable = false) })
public List<JobTitle> getJobTitle() {
return jobTitle;
}

public void setJobTitle(List<JobTitle> jobTitle) {
this.jobTitle = jobTitle;
}



}


and jobtilte class



@Entity(name = "jobtitle")
@Table(name = "JOBTITLE")
public class JobTitle implements Serializable {
private Long id;
private String title;
private java.util.List<Person> personList;

public JobTitle() {
}


public JobTitle(String title) {
this.title = title;
}


@Id
@Column(name = "ID", columnDefinition = "int")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "MySeq2")
@SequenceGenerator(name = "MySeq2", sequenceName = "MYGEN2")
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Basic
@Column(name = "NAME", columnDefinition = "varchar(100)")
public String getTitle() {
return title;
}

public void setTitle(String title) {

this.title = title;
}

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "jobTitle")
public java.util.List<Person> getPersonList() {
return personList;
}

public void setPersonList(java.util.List<Person> personList) {

this.personList = personList;
}


}


and I selected person with this code



public Person selectPerson(Long id){

Person person = (Person) getHibernateTemplate().get(Person.class, id);
return person;
}


but when I get person with Rest API, json response show me person with repeatedly info like this:



"id": 8,
"name": "ترانه",
"family": "علیدوستی",
"mobile": "6556656",
"userName": "111111",
"password": "123456",
"jobTitle": [{
"id": 5,
"title": "?????",
"personList": [{
"id": 8,
"name": "ترانه",
"family": "علیدوستی",
"mobile": "6556656",
"userName": "111111",
"password": "123456",
"jobTitle": [{
"id": 5,
"title": "?????",
"personList": [{
"id": 8,
"name": "ترانه",
"family": "علیدوستی",
"mobile": "6556656",
"userName": "111111",
"password": "123456",
"jobTitle": [{
"id": 5,
"title": "?????",
"personList": [{
"id": 8,
"name": "ترانه",
"family": "علیدوستی",
"mobile": "6556656",
"userName": "111111",
"password": "123456",
"jobTitle": [{
"id": 5,
"title": "?????",
"personList": [{
"id": 8,
"name": "ترانه",
"family": "علیدوستی",
"mobile": "6556656",
"userName": "111111",
"password": "123456",
"jobTitle": [{
"id": 5,
"title": "?????",
"personList": [{
"id": 8,
"name": "ترانه",
"family": "علیدوستی",
"mobile": "6556656",
"userName": "111111",
"password": "123456",
"jobTitle": [{
"id": 5,
"title": "?????",
"personList": [{
"id": 8,
"name": "ترانه",
"family": "علیدوستی",
"mobile": "6556656",
"userName": "111111",
"password": "123456",
"jobTitle": [{
"id": 5,
"title": "?????",
"personList": [{


how to solve this problem?



asked 1 min ago







loop to get json spring for Many-To-Many relation with Rest API

Aucun commentaire:

Enregistrer un commentaire