Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4092870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:32:47+00:00 2026-05-20T19:32:47+00:00

Given the model Activity containing a bag with models of type Report (one to

  • 0

Given the model Activity containing a bag with models of type Report (one to many). I would like to get a list of all activities, containing the number of reports of each activity. This two queries don’t lead to any good, the counter is always 1 (which is wrong):

select act, (select count(r) from act.Reports r) from Activity act

Or:

select act, count( elements(act.Reports) ) from Activity act group by act.ActivityId, act.Title

Is it possible to write a proper query in HQL to solve this easy task?

Thx for any tipps!
sl3dg3

Edit:
Following the mappings. Activity:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class
        name="Core.Models.Config.Activity, Core"
        table="Activity"
    >

        <!-- primary key -->
        <id name="ActivityId" type="Int32" unsaved-value="0" access="property">
            <column name="ActivityId" not-null="true"/>
            <generator class="identity" />
        </id>

        <!-- Properties -->
        <many-to-one name="Title" fetch="join" cascade="all"/>

        <!-- One-To-Many Reports -->
        <bag name="Reports" inverse="true" fetch="join">
            <key column="ReportId" />
            <one-to-many class="Core.Models.Report"/>
        </bag>

    </class>
</hibernate-mapping>

Mapping Report:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class
        name="Core.Models.Report, Core"
        table="Report"
    >

    <!-- primary key -->
    <id name="ReportId" type="Int32" unsaved-value="0" access="property">
        <column name="ReportId" not-null="true"/>
        <generator class="identity" />
    </id>

    <!-- Properties (shortened - there are more many-to-one and one bag -->
    <property name="Created" />
    <many-to-one name="Activity" column="ActivityId" />

</class>

Class Activity:

public class Activity
{

    public Activity()
    {
        Title = new Translation();
    }

    public virtual int ActivityId { get; set; }

    public virtual Translation Title { get; set; }

    public virtual IList<Report> Reports { get; set; }
}

Class Report:

public class Report
{

    /// <summary>
    /// HNIBERNATE ONLY
    /// </summary>
    public Report() 
    { }

    /// <summary>
    /// Init Report
    /// </summary>
    public Report(User author)
    {
        // ... Shortened
        Activity = new Activity();
    }

    public virtual Activity Activity { get; set; }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:32:47+00:00Added an answer on May 20, 2026 at 7:32 pm

    Your first HQL query has the wrong syntax. Try this instead:

    select act, (select count(*) from act.Reports) from Activity act
    

    Your second HQL query cannot work, because you would need all the columns in the GROUP BY clause. Try this instead:

    select act.ActivityId, act.Title, count( elements(act.Reports) ) 
    from Activity act 
    group by act.ActivityId, act.Title
    

    Edit:

    Ah, I think this might be the bug:

    <bag name="Reports" inverse="true" fetch="join">
        <key column="ActivityId" /> <-- instead of ReportId
        <one-to-many class="Core.Models.Report"/>
    </bag>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to, when given a particular model, return all the related models it
Given an object like: class M(models.Model): test = models.BooleanField() created_date = models.DateTimeField(auto_now_add=True) Given sample
Given a Django model, I'm trying to list all of its fields. I've seen
My object model is given below and would like your inputs on the number
Given the following Core Data Model: -> : to-one releationship ->>: to-many relationship Entity
Given a model's instance object, how can I get the database table's name? I
Given a controller method like: def show @model = Model.find(params[:id]) respond_to do |format| format.html
Given a class: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) Is it
Given: from django.db import models class Food(models.Model): Food, by name. name = models.CharField(max_length=25) class
To provide an activity log in my SQLAlchemy-based app, I have a model like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.