休眠 - 双向@OneToOne [英] Hibernate - bidirectional @OneToOne
本文介绍了休眠 - 双向@OneToOne的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class User {$ b $}我有两个类:User和UserPicture,它们具有1:1的关系。 b @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id,nullable = false,unique = true)
private int id;
私人字符串名字;
私人字符串姓氏;
$ b $ @ @OneToOne
@JoinColumn(name =picture)//数据库中名为picture的字段
private UserPicture userPicture;
public class UserPicture {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id,nullable = false,unique = true)
private int id;
私人Blob图片;
@OneToOne
@JoinColumn(name =user)
用户用户;
User'User'中的'user'将会被加载,但user'userPicture'不会被加载 - 我做错了什么?
编辑
必须添加,我只是创建一个UserPicture并插入它们(与现有的userId) - 也许我需要级联'User'in UserPicture?
解决方案
您必须映射您的类。
public class User {
...
@OneToOne(mappedBy =user)
private UserPicture userPicture;
...
}
public class UserPicture {
...
@OneToOne
@JoinColumn(name =user)
私人用户用户;
...
}
I have 2 classes: User and UserPicture which have a 1:1 relationship.
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable = false, unique = true)
private int id;
private String firstname;
private String lastname;
@OneToOne
@JoinColumn(name = "picture") //field named "picture" in the database
private UserPicture userPicture;
..
}
public class UserPicture {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable = false, unique = true)
private int id;
private Blob image;
@OneToOne
@JoinColumn(name = "user")
User user;
'user' in UserPicture will be loaded but 'userPicture' in User not - what did Im wrong?
EDIT Have to add that Im just create a UserPicture and insert them (with existing userId) - maybe I need to cascade 'user' in UserPicture?
解决方案
You have to map your classes.
public class User {
...
@OneToOne (mappedBy="user")
private UserPicture userPicture;
...
}
public class UserPicture {
...
@OneToOne
@JoinColumn (name="user")
private User user;
...
}
这篇关于休眠 - 双向@OneToOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文