Grails GORM - Find all objects with an excellent property value

I am using Grails, and I am trying to figure out how to get all the different values โ€‹โ€‹of a property of a domain class. In other words, I am looking for all the individual values โ€‹โ€‹for a column. However, I'm not sure how to translate this into a GORM statement, the preferred criterion.

I am open to anyone that gives me a list of domain classes (each of which has a different meaning for the property in question), and I collect all the property values. Or something that gives me various property values โ€‹โ€‹directly.

+5
source share
2 answers

I think all you have to do is use projections and distinct . According to the documentation, this will provide you with a list of different property values โ€‹โ€‹from your domain class.

 def results = MyDomain.withCriteria { projections { distinct("theDistinctProperty") } } 
+16
source

I use the forecast criteria and groupProperty to get the different values โ€‹โ€‹that I use. This code is now in production.

 def criteria = Item.createCriteria() def items = criteria { projections { groupProperty "product", "product" } order "product" //remeber import import org.hibernate.transform.AliasToEntityMapResultTransformer in order to get alias property resultTransformer(AliasToEntityMapResultTransformer.INSTANCE) } 
0
source

Source: https://habr.com/ru/post/1207691/


All Articles