我有以下两个实体:
users:
public class User implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Long userId;
@Column(nullable = false)
private String userName;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
@JoinColumn(name="userId", referencedColumnName = "userId")
private Address address;
}
Address:
public class Address {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long addressId;
private String streetName;
private Long userId;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "userId", referencedColumnName="userId", insertable = false, updatable = false, nullable = false)
private User user;
}
我正在做userrepository.save(user);用户对象如下:
{
"userName" : "test",
"Address" : {
"streetName" : "street"
}
}
但是在地址表中,userid是空的,即使有一个forign-key关系在那里,当我得到用户实体地址是空的时候,任何人都可以告诉我我哪里出错了。
提前谢谢。
1条答案
按热度按时间ktecyv1j1#
如果我把上面的代码改成这样:
效果不错。