这是我的创建方法:
public Person create(Person person) {
if (person.getId() != null) {
throw new IllegalArgumentException("Person already has an id.");
}
if (person == null) {
throw new IllegalArgumentException("Person is null.");
}
if (person.getUniqueNumber() == null) {
throw new IllegalArgumentException("Unique number is null.");
}
if (person.getUniqueNumber().length() == 0) {
throw new IllegalArgumentException("Incorrect unique number.");
}
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.getTransaction().commit();
return person;
}
这些是 Person 属性:
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable=false, unique=true)
private String uniqueNumber;
}
如何检查 create 方法中的 uniqueNumber 是否(如果没有两个人具有相同的唯一编号)?