Hibernate注释@Table @UniqueConstraint继承

时间:2014-11-24 18:34:26

标签: hibernate unique-constraint hibernate-annotations

我有一个用于Hibernate的Java模型,其中映射是通过注释完成的 我有一个抽象@MappedSuperClass,其中包含需要唯一的属性,因此我使用了@Table注释和@UniqueConstraint

我的问题是这些约束不是由孩子们继承的。 如果我在每个子类中复制/粘贴唯一约束,但它在我看来应该存在更清晰的解决方案时效果很好:)

以下是相关代码:

@MappedSuperClass
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"firstname", "lastname"}))
public abstract class Person {
  private String firstname;
  private String lastname;

  // Constructors/Getters/Setters
}

@Entity
@Table(name = "employee")
public class Employee extends Person {
  // Some specific properties, constructors, getters/setters
}

@Entity
@Table(name = "partner")
public class Partner extends Person {
  // Some specific properties, constructors, getters/setters
}


public void testCase() {
  Employee employee1 = new Employee("John", "Doe");
  Employee employee2 = new Employee("John", "Doe");

  this.employeeDAO.save(employee1);
  this.employeeDAO.save(employee2); // I'd like an exception here!
}

有没有办法让这个约束工作而不会在每个子类中重复它?

感谢您的帮助!

0 个答案:

没有答案
相关问题