[英]Hibernate - Spring Data JPA One To Many unique constraint across table
我有一個部門和員工表。 我需要部門中的部門名稱和員工是唯一的,以便為具有相同員工集的部門提供一個新條目,它應該拋出一個唯一的約束違規錯誤。
我不知道如何在休眠中解決這個問題。
我需要跨多個表有唯一的約束。
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(
name="dept"
)
public class DeptEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(nullable = false)
private String name;
@OneToMany(mappedBy = "dep", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Employee> attributes = new ArrayList<>();
}
導入 javax.persistence.*;
@Entity
@Table(name="emp"
)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
private DeptEntity dep;
}
你可以使用像:
@Column(unique= true)
private String name;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.