当我加载我的初始测试数据时,我检索完整性约束违规异常。只有当我使用addShift方法添加2条以上的记录时才会发生这种情况。我在内存数据库中使用h2。
装载:
public void load() {
shiftService.addShift(1, LocalDate.now(), ShiftType.DAY, 1);
shiftService.addShift(2, LocalDate.now().plusDays(1), ShiftType.NIGHT, 1);
shiftService.addShift(2, LocalDate.now().plusDays(5), ShiftType.NIGHT, 2);
}
班次服务:
@Transactional
public void addShift(long userId, LocalDate shiftDate, ShiftType shiftType, long groupId) {
Optional<User> user = userService.findOne(userId);
if (user.isPresent()) {
Optional<Group> group = groupService.getOne(groupId);
if (group.isPresent()) {
Shift shift = new Shift(shiftDate, shiftType, group.get());
user.get().addShift(shift);
}
}
}
引起:
org.h2.jdbc.JdbcSQLException: Referential integrity constraint
violation: "FKG5R8KJRL3GG3EKESMHT77GO7L: PUBLIC.SHIFT FOREIGN KEY(ID)
REFERENCES PUBLIC.GROUPS(ID) (3)"; SQL statement:
insert into shift (id, group_id, shift_date, shift_type) values (null, ?, ?,
?) [23506-196]
型号:
切换:
@Entity
public class Shift {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private LocalDate shiftDate;
private ShiftType shiftType;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_shifts", joinColumns = @JoinColumn(name = "shift_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private Set<User> users = new HashSet<>();
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "group_id")
private Group group;
组:
@Entity
@Table(name = "groups") // Group is reserved name in SQL
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_group", joinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private Set<User> users = new HashSet<>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "moderator_group", joinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private Set<User> moderators = new HashSet<>();
@OneToMany(mappedBy = "id", cascade = CascadeType.ALL)
private Set<Shift> shifts;
用户:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String email;
private String password;
private String firstName;
private String lastName;
private String phone;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
name = "user_authority",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name")}
)
private Set<Authority> authorities = new HashSet<>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "user_group",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "group_id", referencedColumnName = "id")}
)
private Set<Group> usingGroups = new HashSet<>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "moderator_group",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "group_id", referencedColumnName = "id")}
)
private Set<Group> moderatingGroups = new HashSet<>();
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(
name = "user_shifts",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "shift_id", referencedColumnName = "id")}
)
private Set<Shift> shifts = new HashSet<>();
你知道为什么我有这个例外吗?
答案 0 :(得分:2)
您在Group
中的映射是错误的。
@OneToMany(mappedBy = "id", cascade = CascadeType.ALL)
private Set<Shift> shifts;
这应该是:
@OneToMany(mappedBy = "group", cascade = CascadeType.ALL)
private Set<Shift> shifts;
<强>解释
Hibernate正在id
中的shift
和id
中的group
之间添加外键约束。您可能正在添加2个组,并且在尝试添加2个以上的班次后,未满足约束,因为ID为3的组不存在。