简体   繁体   English

休眠UniqueConstraint

[英]Hibernate UniqueConstraint

For example I have some entity like: 例如,我有一些实体,例如:

@Entity
public class WorkingScheduleOverride implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Version
    private Integer version;

    @Column
    private Date validFrom;

    @Column
    private Date validThru;

    @Column
    private Date timestamp;

    @Column(unique = true)
    private Date date;

To check that the entity is unique by field date I added (unique = true) . 为了在实地日期之前检查该实体是否唯一,我添加了(unique = true) This works fine, but only if not to use "closable" approach to entities. 这很好,但前提是不对实体使用“可关闭”方法。 It is for the cases if you want keep track the history of these objects. 如果您想跟踪这些对象的历史,就可以使用。 So I'm using 'validThru' field to specify till what time the entity is valid. 因此,我使用“ validThru”字段来指定实体何时生效。

So my question is: is it possible by using any custom validations/annotations to check on uniqueness ONLY VALID entities (validThru == null || validThru < new Date()) . 所以我的问题是:是否可以通过使用任何自定义验证/注释来检查唯一性有效的实体(validThru == null || validThru < new Date())

Incorrect data in database (two records has the same date and both are valid): 数据库中的数据不正确(两个记录具有相同的日期并且都有效):

-----------------------------------------------------------------
id  |version    |timestamp  |valid_from |valid_thru |date       |
-----------------------------------------------------------------
1   |1          |2012-01-01 |2012-01-01 |null       |2013-01-01 |
-----------------------------------------------------------------
2   |1          |2012-01-01 |2012-01-01 |null       |2013-01-01 |
-----------------------------------------------------------------

Correct data in database (two records has the same date but second record is invalid): 数据库中的数据正确(两个记录具有相同的日期,但第二个记录无效):

-----------------------------------------------------------------
id  |version    |timestamp  |valid_from |valid_thru |date       |
-----------------------------------------------------------------
1   |1          |2012-01-01 |2012-01-01 |null       |2013-01-01 |
-----------------------------------------------------------------
2   |1          |2012-01-01 |2012-01-01 |2012-06-01 |2013-01-01 |
-----------------------------------------------------------------

Incorrect data in database (two records has the same date and both are valid, second record becomes invalid only 1th of January 2014): 数据库中的数据不正确(两条记录具有相同的日期并且都有效,第二条记录仅在2014年1月1日才失效):

-----------------------------------------------------------------
id  |version    |timestamp  |valid_from |valid_thru |date       |
-----------------------------------------------------------------
1   |1          |2012-01-01 |2012-01-01 |null       |2013-01-01 |
-----------------------------------------------------------------
2   |1          |2012-01-01 |2012-01-01 |2014-01-01 |2013-01-01 |
-----------------------------------------------------------------

Thanks 谢谢

I'm not sure to understand your question but. 我不确定您的问题,但是。

@PreUpdate
@PrePersist
public void checkEntity() {
    if ( validThru != null && validThru.getTime() > new Date().getTime() ) {
        throw new SomeException("Invalid entity");
    }
}

may do the job. 可以胜任。


When you use unique=true the constraint is not at the code level, it is at the db level. 当您使用unique=true ,约束不在代码级别,而是在数据库级别。 If you want to ensure more advanced constraints, create a trigger that will reject the inserts and updates. 如果要确保更高级的约束,请创建一个将拒绝插入和更新的触发器。

If a trigger can't be coded, you have to enforce your entity consistency with applicative code. 如果无法对触发器进行编码,则必须使用应用代码来增强实体的一致性。 Create a service with a save(entity) that will reject the bad entities with an exception. 创建一个具有save(entity)的服务,该服务将拒绝异常实体,并带有异常。

Instead of date,You can use TimeStamp.and unique column constraint on verion and id.So that any of the situation Your record could unique. 您可以使用TimeStamp。而不是日期,而是在verion和id上使用唯一列约束。这样,您的记录中的任何情况都可以唯一。 Another way you can use status as a column Where praticular record Valid-V and Invalid -I.you can show only valid or invalid records based on the selection 可以将状态用作列的另一种方式,其中记录有效-V和无效-I。根据选择,您只能显示有效或无效记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM