gpt4 book ai didi

Hibernate - 双向@OneToOne

转载 作者:行者123 更新时间:2023-12-02 21:56:22 27 4
gpt4 key购买 nike

我有 2 个类:User 和 UserPicture,它们具有 1:1 关系。

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;

将加载 UserPicture 中的“user”,但不会加载 User 中的“userPicture” - 我错了什么?

编辑必须补充一点,我只是创建一个 UserPicture 并插入它们(使用现有的 userId) - 也许我需要在 UserPicture 中级联“用户”?

最佳答案

你必须映射你的类。

public class User {
...
@OneToOne (mappedBy="user")
private UserPicture userPicture;
...
}

public class UserPicture {
...
@OneToOne
@JoinColumn (name="user")
private User user;
...
}

关于Hibernate - 双向@OneToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13044567/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com