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.