在使用关联表的Groovy createCriteria时出现的问题。

huangapple go评论82阅读模式
英文:

Groovy createCriteria issue with joined table

问题

以下是您要翻译的内容:

我有一个领域类 Coach,它与另一个领域类 CoachProperty 之间存在一对多的关系。

Hibernate/Grails 在数据库中创建了第三个联接表。

在下面的示例中,我试图获取那些其文本值既包含 foo 又包含 bar 的教练。我在 Grails 中尝试了不同的解决方案,使用了 'or' 和 'and',但要么返回一个空列表,要么返回一个同时包含 foo 和 bar 的列表。

Coach:

class Coach {

    static hasMany = [ coachProperties : CoachProperty ]

CoachProperty:

class CoachProperty {

    String text
    boolean active = true
    
    static constraints = {
        
        text(unique: true, nullable: false, blank: false)
    }
}

数据库中自动创建的联接表,并且我填充了一些数据。在这个示例中,我试图获取教练 372,因为该教练既有 1 又有 2,即 foo 和 bar:

+---------------------------+-------------------+
| coach_coach_properties_id | coach_property_id |
+---------------------------+-------------------+
|                       150 |                 2 |
|                       372 |                 1 |
|                       372 |                 2 |
|                        40 |                 3 |
+---------------------------+-------------------+

Coach.createCriteria().list() 中,还有其他的过滤条件。这应该返回教练 372,但却返回空:

def tempList = ["foo", "bar"]			
    coachProperties{
        for(String temp: tempList){
            and {
                log.info "temp = " + temp
                ilike("text", temp)
            }
        }						
    }
英文:

I have a domain class Coach which has a has many relationship to another domain class CoachProperty.

Hibernate/Grails is creating a third joined table in the database.

In the example below I am trying to fetch the coaches which both have foo AND bar for their text value. I have tried different solutions with 'or' and 'and' in Grails which either returns an empty list or a list with BOTH foo and bar.

Coach:

class Coach {

static hasMany = [ coachProperties : CoachProperty ]

CoachProperty:

class CoachProperty {

String text
boolean active = true

static constraints = {
	
	text(unique: true, nullable: false, blank: false)
}
}

Joined table which is being auto-created and I populated with some data, in this example I am trying to fetch coach 372 since that coach has both 1 and 2 i.e foo and bar:

+---------------------------+-------------------+
| coach_coach_properties_id | coach_property_id |
+---------------------------+-------------------+
|                       150 |                 2 |
|                       372 |                 1 |
|                       372 |                 2 |
|                        40 |                 3 |
+---------------------------+-------------------+

Inside Coach.createCriteria().list() among with other filters. This should return coach 372 but return empty:

def tempList = ["foo", "bar"]			
					coachProperties{
						for(String temp: tempList){
							and {
								log.info "temp = " + temp
								ilike("text",temp)
							}
						}						
					}

答案1

得分: 0

我好像记得这个错误。好像是关于不能同时使用可为 null 和空白。尝试只用 'nullable:true'。

英文:

I seem to remember this error. Was something about not being able to use both nullable & blank at the same time.Try with just 'nullable:true'

答案2

得分: 0

我不得不使用executeQuery创建一个解决方法,其中ids是包含我试图提取的coach属性的id的列表。

def coaches = Coach.executeQuery '''
    select coach from Coach as coach 
    join coach.coachProperties as props 
    where props.id in :ids 
    group by coach
    having count(coach) = :count''', [ids: ids.collect { it.toLong() }, count: ids.size().toLong()]
    
or {
    coaches.each {
        eq("id", it.id)
    }
}
英文:

I had to create a workaround with executeQuery where ids is the list containing the id's of the coachproperties i was trying to fetch.

	def coaches = Coach.executeQuery '''
					select coach from Coach as coach 
					join coach.coachProperties as props 
					where props.id in :ids 
					group by coach
					having count(coach) = :count''', [ids: ids.collect { it.toLong() 
                    }, count: ids.size().toLong()]
						
					
					or{
						coaches.each{ 
							eq("id", it.id)
						}
					}

huangapple
  • 本文由 发表于 2020年10月16日 20:37:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64389355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定