有实体类user 和Info ,info对应的表中有user的主键作为外键,需求是查询info可以得到对应的user,查询user可以得到对应的info。package com.zxy.demo.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import javax.persistence.*;
@Entity
public class User {
@Id
@GeneratedValue
private Integer id;
private Integer age;
@OneToOne(mappedBy = "user",cascade = CascadeType.ALL)
private Info userInfo;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Info getUserInfo() {
return userInfo;
}
public void setUserInfo(Info userInfo) {
this.userInfo = userInfo;
}
}package com.zxy.demo.entity;
import javax.persistence.*;
@Entity
public class Info {
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false)
private String idCard;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(unique = true)
private User user;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Info{" +
"id=" + id +
", idCard='" + idCard + '\'' +
", user=" + user +
'}';
}
}当调用jpa的findOne方法时,hibernate会注入user中的info,然后又一次注入info中的user,这样很蠢。网上找到控制json解析的注解,这样做还不如直接单项关联,治标不治本。求一个解决方案!
添加回答
举报
0/150
提交
取消