替代grails多列唯一约束(乐观插入) [英] alternative to grails multicolumn unique constraint (optimistic inserts)

查看:110
本文介绍了替代grails多列唯一约束(乐观插入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重用的域名类 Relationship ,它看起来与下面的类很相似

  class关系{
元素源
元素目标
类型类型

//其他属性省略

静态约束= {
type unique:['source','destination']
}
}

我有一个服务创建这个领域类的新实例,它检查现有的实例,如果发现重用它们,但我希望只在数据库中实现具有唯一约束的乐观插入,因为


  1. GORM独特的约束效率非常低(对于一个简单的任务,我只需检查约束和三分之一的时间就可以获得13 000个点击)



  2. $ b
    找到现有实体在批量运行时很昂贵(每个查询刷新当前会话并花费约40毫秒) p >所以我们的想法是让保存方法失败,并重新使用现有的实体

      try {
    relationshipInstance.save()
    } catch(DataIntegrityViolationException | ConstraintViolationException e){
    //查找并重用
    }

    我试图找到实体,我得到了休眠异常

      AssertionFailure:null关系入口中的id(不要刷新会话后发生异常)




    1. 有什么办法可以从异常中恢复 relationshipInstance.discard()没有帮助,因为它没有 EntityKey )?

    2. 您还推荐哪种解决方案用于根据需求进行唯一性检查而不触发flush?
    3. >您可以在调用save时禁用验证,如下所示:

        relationshipInstance.save(validate:false)

      请参阅有关保存方法的文档以获取更多信息。

      I have a heavy used domain class Relationship which looks pretty similar to following one

      class Relationship {
          Element source
          Element destination
          Type type
      
          // other properties omitted
      
          static constraints = {
               type unique: ['source', 'destination']
          }
      }
      

      I have a service creating new instances of this domain class which checks for existing instances and if found reuses them but I would like to implement sort of optimistic inserts with the unique constraint only in the database because

      1. GORM unique constraints are highly inefficient (for a simple task I got 13 000 hits just checking the constraints and one third of the time spent)
      2. finding the existing entity is expensive while running in batch (each query flushes the current session and costs about 40ms)

      So the idea is to let the save method fail and than reuse the existing entity

      try {
          relationshipInstance.save()
      } catch (DataIntegrityViolationException | ConstraintViolationException e) {
          // find and reuse
      }
      

      but than when I try to find the entity I got hibernate exception

      AssertionFailure: null id in Relationship entry (don't flush the Session after an exception occurs)
      

      1. Is there any way how to recover from the exception (relationshipInstance.discard() does not help as it does not have EntityKey)?
      2. What other solution would you recommend for on demand uniqueness checking without triggering flush?

      解决方案

      You can disable the validation when calling save, like this:

      relationshipInstance.save(validate:false)
      

      See documentation for save method for further info.

      这篇关于替代grails多列唯一约束(乐观插入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆