【问题标题】:Query inside custom validator在自定义验证器中查询
【发布时间】:2011-04-07 13:10:52
【问题描述】:

我编写了接受方法名称作为参数的类级 JSR-303 约束注释。您设置为参数的方法名称应返回布尔值。

我有实体 Foo,其字段代码为 validFrom 和 validTill。代码每次都必须是唯一的。 例子: 两个实体 ('AAA',1.1.2010,31.12.2010) 和 ('AAA',1.5.2011,31.5.2011) 都可以 两个实体 ('AAA',1.1.2010,31.12.2010) 和 ('AAA',1.2.2010,31.3.2012) 是错误的

现在我需要编写验证方法来检查代码是否唯一。 我写道:

    public boolean isUnique() {
    if (logger.isDebugEnabled()) logger.debug("Before select");
    return entityManager.createQuery("select count(o) from Currency o where " +
            "o.code = :code and (" +
            "(o.validFrom between :validFrom and :validTill) or " +
            "(o.validTill between :validFrom and :validTill) or " +
            "(:validFrom between o.validFrom and o.validTill))",
            Long.class)
            .setParameter("code", getCode())
            .setParameter("validFrom", getValidFrom())
            .setParameter("validTill", getValidTill())
            .getSingleResult() == 0;
}

我陷入了无限循环

调试 http-8080-2 ch.laic.bsatrak.domain.base.BaseEntity - 选择之前 调试 http-8080-2 org.hibernate.event.def.AbstractFlushingEventListener - 处理刷新时间级联 调试 http-8080-2 org.hibernate.event.def.AbstractFlushingEventListener - 脏检查集合 调试 http-8080-2 org.hibernate.event.def.AbstractFlushingEventListener - 刷新:1 次插入,0 次更新,0 次删除到 1 个对象 调试 http-8080-2 org.hibernate.event.def.AbstractFlushingEventListener - 刷新:0(重新)创建,0 更新,0 删除到 0 个集合 调试 http-8080-2 org.hibernate.pretty.Printer - 列出实体: 调试 http-8080-2 org.hibernate.pretty.Printer - ch.laic.bsatrak.domain.base.Currency{id=50, createdBy=,editedBy=, createdAt=2011-04-07T14:54:52.233+02: 00,validFrom=2010-01-01T00:00:00.000+01:00,code=CODE,validTill=2010-12-31T00:00:00.000+01:00,suspended=false,version=0,editedAt=null} 调试 http-8080-2 org.hibernate.engine.ActionQueue - 更改必须刷新到空间:bs_currency 调试 http-8080-2 ch.laic.bsatrak.domain.base.BaseEntity - 选择之前 调试 http-8080-2 org.hibernate.event.def.AbstractFlushingEventListener - 处理刷新时间级联 调试 http-8080-2 org.hibernate.event.def.AbstractFlushingEventListener - 脏检查集合 调试 http-8080-2 org.hibernate.event.def.AbstractFlushingEventListener - 刷新:1 次插入,0 次更新,0 次删除到 1 个对象 调试 http-8080-2 org.hibernate.event.def.AbstractFlushingEventListener - 刷新:0(重新)创建,0 更新,0 删除到 0 个集合 调试 http-8080-2 org.hibernate.pretty.Printer - 列出实体: 调试 http-8080-2 org.hibernate.pretty.Printer - ch.laic.bsatrak.domain.base.Currency{id=50, createdBy=,editedBy=, createdAt=2011-04-07T14:54:52.233+02: 00,validFrom=2010-01-01T00:00:00.000+01:00,code=CODE,validTill=2010-12-31T00:00:00.000+01:00,suspended=false,version=0,editedAt=null}

我怎样才能正确地做到这一点?

【问题讨论】:

    标签: java hibernate spring jpa validation


    【解决方案1】:

    需要设置 FlushMode。默认为自动,在查询框架尝试刷新所有可能影响查询的实体之前。

    .setFlushMode(FlushModeType.COMMIT)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多