-1

I searched for this exception. Most answer suggest to directly or indirectly fetch data eagerly. I dont want the child data. How do I tell the json mapping to return only the parent data and not to look for the child data.

Here is the method in the controller :

@RequestMapping(value="/user/{id}", method = RequestMethod.GET )
public @ResponseBody User getUserById(@PathVariable("id") Integer id) {
    User user = userService.getUserById(id);
    return user;
}

DAO has the following method. Note that the Hibernate.initialize() didnot bring any difference in the output. The println is able to print the user name.

public User getUserById(int id) {
    User user = hibernateTemplate.get(User.class, id);
    System.out.println("hello " + user.getUserName());
    Hibernate.initialize(user);
    return user;
}

WebAppInitializer has this filter

@Override
protected Filter[] getServletFilters() {

    return new Filter[]{ new OpenEntityManagerInViewFilter() };
}

User.java :

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "tm_user", catalog = "tm", uniqueConstraints = {     @UniqueConstraint(columnNames = "email"),
    @UniqueConstraint(columnNames = "phone") })
public class User implements java.io.Serializable {

private Integer userId;
private String userName;
private String phone;
private String email;
private String password;
private Date dateOfBirth;
private String userNotes;
private Set<AttendanceUser> attendanceUsers = new HashSet<AttendanceUser>(0);
private Set<ProjectUser> projectUsers = new HashSet<ProjectUser>(0);
private Set<RoleUser> roleUsers = new HashSet<RoleUser>(0);

public User() {
}

public User(String userName) {
    this.userName = userName;
}

public User(String userName, String phone, String email, String  password, Date dateOfBirth, String userNotes,
        Set<AttendanceUser> attendanceUsers, Set<ProjectUser> projectUsers, Set<RoleUser> roleUsers) {
    this.userName = userName;
    this.phone = phone;
    this.email = email;
    this.password = password;
    this.dateOfBirth = dateOfBirth;
    this.userNotes = userNotes;
    this.attendanceUsers = attendanceUsers;
    this.projectUsers = projectUsers;
    this.roleUsers = roleUsers;
}

@Id
@GeneratedValue(strategy = IDENTITY)

@Column(name = "user_id", unique = true, nullable = false)
public Integer getUserId() {
    return this.userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

@Column(name = "user_name", nullable = false, length = 200)
public String getUserName() {
    return this.userName;
}

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

@Column(name = "phone", unique = true, length = 30)
public String getPhone() {
    return this.phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

@Column(name = "email", unique = true, length = 100)
public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

@Column(name = "password", length = 50)
public String getPassword() {
    return this.password;
}

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

@Temporal(TemporalType.DATE)
@Column(name = "date_of_birth", length = 10)
public Date getDateOfBirth() {
    return this.dateOfBirth;
}

public void setDateOfBirth(Date dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

@Column(name = "user_notes", length = 4000)
public String getUserNotes() {
    return this.userNotes;
}

public void setUserNotes(String userNotes) {
    this.userNotes = userNotes;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<AttendanceUser> getAttendanceUsers() {
    return this.attendanceUsers;
}

public void setAttendanceUsers(Set<AttendanceUser> attendanceUsers) {
    this.attendanceUsers = attendanceUsers;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<ProjectUser> getProjectUsers() {
    return this.projectUsers;
}

public void setProjectUsers(Set<ProjectUser> projectUsers) {
    this.projectUsers = projectUsers;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<RoleUser> getRoleUsers() {
    return this.roleUsers;
}

public void setRoleUsers(Set<RoleUser> roleUsers) {
    this.roleUsers = roleUsers;
}

}

Any help is appreciated.

Jeya Balaji
  • 165
  • 1
  • 2
  • 11

2 Answers2

0

You can do one of the following two things if you don't want to load the roleUsers collection:

Add @JsonIgnoreProperties({"roleUsers"}) at the top of your class. For ex.

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties({"roleUsers"})
class User { ...

The import might need to change based on which version you are using. This will tell jackson to ignore that property while serializing. There are different ways of ignoring properties using jackson. For some examples, take a look at this blog. Another source could be this SO post that tells you how to ignore a property if you don't have control over the source code of the class.

Or, you can create a kind of a DTO or a Map containing only the properties you need to serialize, and then use jackson to serialize an instance of that DTO or Map

Community
  • 1
  • 1
Bhashit Parikh
  • 3,121
  • 23
  • 24
  • I dont want to change User.java as it is auto generated. Do you have any link for the second method (jackson serlalizing DTO or Map) – Jeya Balaji Nov 26 '16 at 09:26
0

Thanks to Bhashit. Those two links helped in fixing the issue.

I had to replace filter in WebAppInitializer. Instead of jpa filter, I changed to hibernate filter.

//      return new Filter[]{ new OpenEntityManagerInViewFilter() };
    return new Filter[]{ new OpenSessionInViewFilter() };
Jeya Balaji
  • 165
  • 1
  • 2
  • 11