我遇到了关于hibernate和外键约束的问题。
我得到了以下场景(伪java代码)。我有一个父类(部门)
.Range("D" & (lastRowD + 1) & ":D" & lastRowE).Value = WBname
和子类(人)
@Entity
public class Department {
@Id
private Long id;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "department", cascade = CascadeType.ALL)
List<Person> persons;
public void addToPersons(Person p) {
if (this.persons == null) {
this.persons = new ArrayList<>();
}
this.persons.add(p);
p.setDepartment(this);
}
public void setId(Long id) {
this.id = id;
}
}
完成后,存储库类
@Entity
public class Person {
@Id
private String name;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Person> followers;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "followers")
private List<Person> following;
@ManyToOne(fetch = FetchType.LAZY)
private Department department;
public void addToFollowers(Person p) {
if (this.followers == null) {
this.followers = new ArrayList<>();
}
if (!this.followers.contains(p)) {
this.followers.add(p);
}
}
public void addToFollowing(Person p) {
if (this.following == null) {
this.following = new ArrayList<>();
}
if (!this.following.contains(p)) {
this.following.add(p);
}
}
public void setDepartment(Department department) {
this.department = department;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person that = (Person) o;
return name != null ? name.equals(that.name) : that.name == null;
}
}
导致异常的控制器
public interface DepartmentRepository extends CrudRepository<Department, Long> {}
我猜它会引发违规异常,因为部门d2尚未保存,但是可以在没有明确保存之前所有实体的情况下这样做吗?!
谢谢!
更新
完成代码。例外原因:
@RestController
public class MainController {
@Autowired
private DepartmentRepository departmentRepository;
@RequestMapping("/")
@ResponseBody
public String index() {
Department d1 = new Department();
d1.setId(1L);
Department d2 = new Department();
d2.setId(2L);
Person john = new Person();
john.setName("John");
Person alice = new Person();
alice.setName("Alice");
john.addToFollowers(alice);
alice.addToFollowing(john);
d1.addToPersons(john);
d2.addToPersons(alice);
departmentRepository.save(d1); // <-- throws violation exception
return "foo";
}
}
答案 0 :(得分:0)
@Entity(name = "department")
class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int idx
@OneToMany(mappedBy = "department", cascade = { CascadeType.ALL })
List<Person> person;
// getter, setter...
}
@Entity(name = "Person")
class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int idx;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
// getter, setter...
}
Department department = new Department();
Person p1 = new Person();
Person p2 = new Person();
p1.setDepartment(department);
p2.setDepartment(department);
personRepository.save(p1);
personRepository.save(p2);
答案 1 :(得分:0)
在d.addToPersons(p1);
实施中,请确保将部门设置为人,
public void addToPersons(Person person){
person.setDepartment(this);
this.persons.add(person);
}
同样适用于其他关系