2

Here is my entitiy:

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



}

And here is another entity:

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

Here is diagram that Idea shows me: diagram of db

But when I use in RemindController.class:

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

I have this result... infinity loop:

enter image description here

What am I do wrong? It seems like diagram is ok. Help me please(

Tom Wally
  • 542
  • 3
  • 8
  • 20

1 Answers1

2

It's not jpa problem. It's a problem with serializing your entity to json. You need to explicitly mark the relation as bidirectional so the serializer can skip one end.

For Spring I'm assuming you're using jackson.

Take a look at answers to this question.

Community
  • 1
  • 1
Tuan Pham
  • 1,171
  • 2
  • 15
  • 27
  • @TomWally I'm glad it helped, but be aware that using `@JsonIgnore` will cause your field to always be ignored. It that's not something you want, take a look at `@JsonManagedReference` and `@JsonBackReference` or `@JsonIdentity`. – Tuan Pham Mar 13 '16 at 12:53