logo
down
shadow

Grails named queries: find parents of child A and child B


Grails named queries: find parents of child A and child B

Content Index :

Grails named queries: find parents of child A and child B
Tag : grails , By : Joe
Date : November 28 2020, 11:01 PM

wish help you to fix your issue I have this: , So I now understand and I recreated and made an answer for you
idParent | idChild
---------------
1        | 1 
2        | 2
*3        | 1
*3        | 2
Peter [test.Child : 1]
Joe [test.Child : 2]
Ann [test.Child : 1, test.Child : 2]
list is [[test.Parent : 3, test.Child : 2, test.Parent : 3, test.Child : 1]]
String query="""select new map(p.name as parentName,
            case when (select count(*) from p.children as pc where pc.name in (:children)) >= 2 then 'true' 
            else 'false' end as childMatch) from Parent p
            """
        def inputs=[:]
        inputs.children=['Tom','Sam']
        def listing = Parent.executeQuery(query,inputs,[readonly:true]) 
println "list is $listing"
list is [[childMatch:false, parentName:Peter], 
[childMatch:false, parentName:Joe], 
[childMatch:true, parentName:Ann]]
def listing = Parent.executeQuery(query,inputs,[readonly:true])?\
.findAll{it.childMatch=='true'}
    println "list is $listing"
list is [[childMatch:true, parentName:Ann]]
String query="""from Parent p1 left join p1.children pc1 , 
        Parent p left join p.children pc where 
        pc.name =:input1 and pc1.name= :input2 and 
        p1.id=p.id group by p.name"""

def inputs=[:]
inputs.input1='Tom'
inputs.input2='Sam'
def list = Parent.executeQuery(query,inputs,[readonly:true])

println "list is ${list}"
String query="""select new map(p1.name as parentName, pc1.name as childName, pc.name as childName2) from Parent p1 left join p1.children pc1 , 
Parent p left join p.children pc where pc.name =:input1 and pc1.name= :input2 and p1.id=p.id """ //group by p1.name, p.name"""
def inputs=[:]
inputs.input1='Tom'
inputs.input2='Sam'
def list = Parent.executeQuery(query,inputs,[readonly:true])
def list1=list.groupBy{it.parentName}
println "list is ${list} vs ${list1}"
Joe [test.Child : 2]
Ann [test.Child : 2, test.Child : 1]
list is [[childName:Sam, childName2:Tom, parentName:Ann]] vs [Ann:[[childName:Sam, childName2:Tom, parentName:Ann]]]
// Find all the parents that have 1 of our matches 
// Tom but all that also have children greater than 1 
def parents = Parent.findAll{children.name=='Tom' && children.size()>1}
//This already returns our only other option 3
println "found ${parents}"


// Now to confirm that the childrens the 
// parent has contains our other scenario:

// Lets build our own list from it
def finalList=[]
//Iteration through our listing of parents
parents?.each { Parent p ->
  // If the child name matches other case
  if (p.children.name.contains('Sam')) {
    // add this to our finalList
    finalList << p
  }             
}
// We have correct results in this list
println "we now have ${finalList}"
found [test.Parent : 3]
we now have [test.Parent : 3]
def parents = Parent.findAll{children.name=='Tom' && children.size()>1}.collect{[name:it.name, children:it.children]}.findAll{it.children.name.contains('Sam')}
found [[name:Ann, children:[test.Child : 1, test.Child : 2]]]
def parents = Parent.findAll{children.name=='Tom' && children.size()>1}.collect{[name:it.name, children:it.children.name]}.findAll{it.children.contains('Sam')}
found [[name:Ann, children:[Tom, Sam]]]

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

how to get parents id while fetching child's information - Grails?


Tag : grails , By : Erwin
Date : March 29 2020, 07:55 AM
Hope this helps I am still not sure which part you dont know. Assuming userAccount has the firstName and lastName field to access them from status try this:
status is the instance of your Status class. The one you need to get the userAccount off of it.
status.userAccount.firstName
status.userAccount.lastName

How do you make one child div determine the parents height and force a second child div to fit inside the parents height


Tag : html , By : MJRider
Date : March 29 2020, 07:55 AM
To fix the issue you can do We can not this task with only css. For this We need to get left child div height with js and assign it to right child max-height and assign to parent div height.
jQuery(document).ready(function(){
   $('textarea').mouseup(function(){
      var height = $(this).parent().height();

      $(this).parents().find('.parent').height(height);
      $(this).parent().next().css({'max-height' : height});
   });
   });
 .parent{ max-width:700px; border:solid 3px #ff0000; float:left; width:100%; position:relative;   }
    .right-child , .child-left{ width:40%; float:left;}
    .right-child{ overflow:auto;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<div class="parent">
<div class="child-left">Lorem Ipsum is simply dummy text of the printing and    <textarea style="margin: 0px; height: 144px; width: 100%;"></textarea></div>
<div class="right-child">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div>

</div>

Remove parent node altogether if the child is duplicate with other parents' child using xslt 2.0


Tag : xslt , By : pdkent
Date : March 29 2020, 07:55 AM
this one helps. Input xml- Parent node Block re-occurs thrice. I need to remove the duplicate nodes altogether, even if it occurs more than 7 times in the same xml without creating a new xml
 <xsl:template match="Blocks">
 <Blocks>
   <xsl:for-each-group select="Block" group-by="Name">
     <xsl:sequence select="."/>
   </xsl:for-each-group>
 </Blocks>

Fetch child data with parents in Django queries


Tag : python , By : Navin
Date : March 29 2020, 07:55 AM
seems to work fine You already have ProductBundle objects as a Related objects reference for each Product.
Assuming you do products = Product.objects.all() you can access each product's bundles by doing:
for product in products:
    product_bundles = product.productbundle_set.all()
{% for product in products %}
    <h1>{{product.title}}</h1>
    <h1>Bundles:</h1>
    {% for bundle in product.productbundle_set.all %}
        <h2>{{bundle.quantity}}</h2>
    {% endfor %}
{% endfor %}

Active Record query to find parents with child within a date range and child prior to that date range


Tag : ruby-on-rails , By : Noah
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can do this in following ways,
Simple way, get the ids for parent from the first query and apply the filter on parent id in second query,
 q1_pids = Parent.joins(:children)
                 .where("children.created_at BETWEEN ? AND ?", start, end)
                 .ids
 count = Parent.joins(:children)
               .where("children.created_at < ?", start)
               .where(id: q1_pids)
               .count("parents.id")
q1 = Parent.joins(:children)
           .where("children.created_at BETWEEN ? AND ?", start, end)

q2 = Parent.joins(:children)
           .where("children.created_at < ?", start)

count = (q1 & q1).count
Related Posts Related QUESTIONS :
shadow
Privacy Policy - Terms - Contact Us © scrbit.com