Hibernate 唯一性字段
我有一个名为 Iteraction 的类,此迭代有许多特殊字段,我想一起验证三个字段(startDate、endDate 和 Project)的唯一性,Project 是一个类。
我尝试做类似的事情:
@Table(uniqueConstraints = {@UniqueConstraint(columnNames={"startDate", "endDate", "project_id"})})
但不起作用,我想对我的验证说的是:我只能数据库中有一个具有相同 startDade、endDate 和 project_id 的迭代寄存器:
在 Rails 代码中,我可以使用 Scope 来实现这一点,但使用 hibernate 我不知道怎么样,有人可以帮助我吗?
I have a class called Iteraction this iteration have many fields in special i want do a validation of uniqueness for three fields together (startDate, endDate and Project), Project is a class.
I try do something like that:
@Table(uniqueConstraints = {@UniqueConstraint(columnNames={"startDate", "endDate", "project_id"})})
But dont work, what a want say to my validation is: i can only have ONE register of iteration in database with the same startDade, endDate and project_id:
In Rails Code i can use Scope for that, but using hibernate i don't know how, can someone help me?
快速浏览一下文档就会发现语法有点不同:
希望有帮助。
还有一个答案是 JPA 中的唯一约束检查 - 请查看。
A quick look to the documentations shows that the syntax is a bit different:
Hope that helps.
One more answer was at unique constraint check in JPA — check it out.