使用findAll()时的无限循环Spring JPA PostgreSQL

时间:2016-03-13 11:27:14

标签: java spring hibernate postgresql jpa

这是我的权利:

@Entity
@Table(name = "remind")

public class Remind {

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private long id;

@Column(name = "title", nullable = false, length = 50)
private String title;

@Column(name = "remind_date", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date remindDate;


public Remind() {
}

public long getId() {
    return id;
}

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

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Date getRemindDate() {
    return remindDate;
}

public void setRemindDate(Date remindDate) {
    this.remindDate = remindDate;
}

 //---Dependency---->

@OneToOne(optional = false)
@JoinColumn(name="id_user", unique = true, nullable = true, updatable = false)
private Users user;

public void setUser(Users user) {
    this.user = user;
}

public Users getUser() {
    return user;
}



}

这是另一个实体:

@Entity
@Table(name = "users")

public class Users {

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private long id;

@Column(name = "name", nullable = false, length = 50)
private String name;

public Users() {
}

public long getId() {
    return id;
}

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

public String getName() {
    return name;
}

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



 //---Dependency---->

@OneToOne(optional = false, mappedBy="user")
public Remind remind;

public Remind getRemind() {
    return remind;
}

public void setRemind(Remind remind) {
    this.remind = remind;
}

以下是Idea向我展示的图表:     diagram of db

但是当我在RemindController.class中使用时:

@RequestMapping(value = "/get", method = RequestMethod.GET)
@ResponseBody
public List<Remind> getReminder() {
    List<Remind> list = remindRepository.findAll();
    return list;
}

我有这个结果......无限循环:

enter image description here

我做错了什么?看起来好像是图表。请帮帮我(

1 个答案:

答案 0 :(得分:1)

这不是jpa问题。将您的实体序列化为json是一个问题。您需要将关系明确标记为双向,以便序列化程序可以跳过一端。

对于春天,我假设您正在使用杰克逊。

查看this问题的答案。