JPA - 休眠 - OneToOne
JPA - Hibernate - OneToOne
我在 tables 用户和配置文件之间有一对一关系,定义如下:
@Entity
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name="users", uniqueConstraints = {
@UniqueConstraint(columnNames = "username"),
@UniqueConstraint(columnNames = "email")
}
)
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idUser;
private String username;
private String email;
private String password;
@OneToOne(cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="id_profile", referencedColumnName="idProfile")
private Profile profile;
@ManyToMany(fetch = FetchType.EAGER)
private Set<Role> roles = new LinkedHashSet<Role>();
}
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name="profile",
uniqueConstraints = @UniqueConstraint(columnNames = "discordId")
)
public class Profile implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idProfile;
private Date birthday;
private String discordId;
private String description;
@ElementCollection(fetch=FetchType.EAGER)
private Set<String> spokenLanguages = new LinkedHashSet<String>();
@OneToMany(fetch=FetchType.EAGER)
private Set<ProfileGame> profileGames = new LinkedHashSet<>();
@OneToOne(mappedBy="profile")
private User user;
@ManyToOne
private TimeSlot timeSlot;
}
然后我 运行 测试在我的数据库中制作一些装置:
Profile profile = this.profileRepository.save(Profile.builder()
.birthday(new Date())
.discordId("discord-" + i)
.description("Description de user-" + i)
.spokenLanguages(spokenLanguages)
.timeSlot(timeSlot)
.build());
user.setProfile(profile);
System.err.println(user);
此处一切正常,System.err.println(用户)returns 正确值。
问题来自数据库,它没有在我的用户 table:
中“应用”id_profile
我错过了什么?
尝试做这样的事情:
Profile profile = Profile.builder()
.birthday(new Date())
.discordId("discord-" + i)
.description("Description de user-" + i)
.spokenLanguages(spokenLanguages)
.timeSlot(timeSlot)
.user(user)
.build();
user.setProfile(profile);
profile = this.profileRepository.save(profile);
您使用双向 @OneToOne
关联,因此您应确保双方始终保持同步。
此外,由于您需要将持久状态传播给用户,您应该将 cascade = CascadeType.ALL
添加到 Profile.user
,如下所示:
@Entity
public class Profile implements Serializable {
// ...
@OneToOne(mappedBy="profile", cascade=CascadeType.ALL)
private User user;
}
Class 用户:
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL,
fetch = FetchType.LAZY, optional = false)
private Profile profile;
Class个人资料:
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="id_user")
private User user;
我在 tables 用户和配置文件之间有一对一关系,定义如下:
@Entity
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name="users", uniqueConstraints = {
@UniqueConstraint(columnNames = "username"),
@UniqueConstraint(columnNames = "email")
}
)
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idUser;
private String username;
private String email;
private String password;
@OneToOne(cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="id_profile", referencedColumnName="idProfile")
private Profile profile;
@ManyToMany(fetch = FetchType.EAGER)
private Set<Role> roles = new LinkedHashSet<Role>();
}
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name="profile",
uniqueConstraints = @UniqueConstraint(columnNames = "discordId")
)
public class Profile implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idProfile;
private Date birthday;
private String discordId;
private String description;
@ElementCollection(fetch=FetchType.EAGER)
private Set<String> spokenLanguages = new LinkedHashSet<String>();
@OneToMany(fetch=FetchType.EAGER)
private Set<ProfileGame> profileGames = new LinkedHashSet<>();
@OneToOne(mappedBy="profile")
private User user;
@ManyToOne
private TimeSlot timeSlot;
}
然后我 运行 测试在我的数据库中制作一些装置:
Profile profile = this.profileRepository.save(Profile.builder()
.birthday(new Date())
.discordId("discord-" + i)
.description("Description de user-" + i)
.spokenLanguages(spokenLanguages)
.timeSlot(timeSlot)
.build());
user.setProfile(profile);
System.err.println(user);
此处一切正常,System.err.println(用户)returns 正确值。
问题来自数据库,它没有在我的用户 table:
中“应用”id_profile我错过了什么?
尝试做这样的事情:
Profile profile = Profile.builder()
.birthday(new Date())
.discordId("discord-" + i)
.description("Description de user-" + i)
.spokenLanguages(spokenLanguages)
.timeSlot(timeSlot)
.user(user)
.build();
user.setProfile(profile);
profile = this.profileRepository.save(profile);
您使用双向 @OneToOne
关联,因此您应确保双方始终保持同步。
此外,由于您需要将持久状态传播给用户,您应该将 cascade = CascadeType.ALL
添加到 Profile.user
,如下所示:
@Entity
public class Profile implements Serializable {
// ...
@OneToOne(mappedBy="profile", cascade=CascadeType.ALL)
private User user;
}
Class 用户:
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL,
fetch = FetchType.LAZY, optional = false)
private Profile profile;
Class个人资料:
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="id_user")
private User user;