com.fasterxml.jackson.databind.JsonMappingException:懒得初始化角色

时间:2016-11-25 14:42:53

标签: hibernate spring-mvc

我搜索了这个例外。大多数答案建议直接或间接地获取数据。我不想要孩子的数据。如何告诉json映射只返回父数据而不是查找子数据。

以下是控制器中的方法:

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

DAO有以下方法。请注意,Hibernate.initialize()没有在输出中带来任何差异。 println能够打印用户名。

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

WebAppInitializer具有此过滤器

@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;
}

}

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

如果您不想加载roleUsers集合,可以执行以下两项操作之一:

在课程顶部添加@JsonIgnoreProperties({"roleUsers"})。对于前。

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

导入可能需要根据您使用的版本进行更改。这将告诉jackson在序列化时忽略该属性。使用jackson有不同的方法来忽略属性。有关示例,请查看this blog。另一个来源可能是this SO post,告诉您如果无法控制类的源代码,如何忽略属性。

或者,您可以创建一种仅包含序列化所需属性的DTOMap,然后使用jackson序列化DTO或{的实例{1}}

答案 1 :(得分:0)

感谢Bhashit。这两个链接有助于解决问题。

我不得不在WebAppInitializer中替换过滤器。而不是jpa过滤器,我改为休眠过滤器。

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