为什么 OneToOne 关系没有按预期工作?
Why OneToOne relation doesn't works as expected?
我有 2 个实体。用户:
@Table(name = "USERS")
@Entity
public class User {
@Column(name = "user_id")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String email;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
private Authentication authentication;
}
和身份验证:
@Table(name = "AUTHENTICATIONS")
@Entity
public class Authentication {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "login_id")
private String loginId;//openId login
@JsonIgnore
private String password;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;
}
而且我有为新用户提供注册的服务:
@Override
@Transactional
public User createUser(UserRegistrationForm form) {
Authentication authentication = new Authentication();
authentication.setPassword(form.getPassword());
User user = new User();
user.setAuthentication(authentication);
user.setEmail(form.getEmail());
user.setName(form.getLogin());
authentication.setUser(user);
return userRepository.save(user);
}
我的问题是方法 userRepository.save() returns 无限嵌套对象:
{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"
我做错了什么?帮助理解它应该如何工作。
您的问题是:
user.setAuthentication(authentication);
...
authentication.setUser(user);
您在 user
和 authentication
之间有一个嵌套引用
json returns 嵌套对象...不是您的存储库!
您使用的是杰克逊?
在一侧添加@JsonManagedReference,在另一侧添加@JsonBackReference
我有 2 个实体。用户:
@Table(name = "USERS")
@Entity
public class User {
@Column(name = "user_id")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String email;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
private Authentication authentication;
}
和身份验证:
@Table(name = "AUTHENTICATIONS")
@Entity
public class Authentication {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "login_id")
private String loginId;//openId login
@JsonIgnore
private String password;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;
}
而且我有为新用户提供注册的服务:
@Override
@Transactional
public User createUser(UserRegistrationForm form) {
Authentication authentication = new Authentication();
authentication.setPassword(form.getPassword());
User user = new User();
user.setAuthentication(authentication);
user.setEmail(form.getEmail());
user.setName(form.getLogin());
authentication.setUser(user);
return userRepository.save(user);
}
我的问题是方法 userRepository.save() returns 无限嵌套对象:
{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"myName","email":"myemail@gmail.com","authentication":{"id":1,"loginId":null,"user":{"id":1,"name":"
我做错了什么?帮助理解它应该如何工作。
您的问题是:
user.setAuthentication(authentication);
...
authentication.setUser(user);
您在 user
和 authentication
json returns 嵌套对象...不是您的存储库!
您使用的是杰克逊? 在一侧添加@JsonManagedReference,在另一侧添加@JsonBackReference