-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Howto count instances of several subclasses using one query?
PostPosted: Mon May 09, 2005 4:16 am 
Beginner
Beginner

Joined: Fri Feb 11, 2005 12:03 pm
Posts: 48
Location: Kiel, Germany
Hi,

I have an abstract class Account and three Subclasses of Account: RealAccount, AliasAccount and MailList.
Mapping works fine.

How can I select the count of RealAccount objects and AliasAccount objects with one query?

In plain SQL I would do something like
Code:
select count(*) from account where account_type in ('RA','AA') and domain_id=18


Hibernate version:

2.1

Mapping documents:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
    <class
        name="de.splendid.cca.modules.email.model.beans.Account"
        table="account"
    >

        <id
            name="accountId"
            column="account_id"
            type="int"
        >
            <generator class="identity">
              <!--
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Account.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <discriminator
            column="account_type"
        />

        <property
            name="creationDate"
            type="java.util.Date"
            column="creation_date"
            not-null="true"
        />

        <property
            name="description"
            type="java.lang.String"
            column="description"
            not-null="true"
        />

        <many-to-one
            name="domain"
            class="de.splendid.cca.modules.email.model.beans.Domain"
            cascade="save-update"
            outer-join="auto"
            column="domain_id"
            not-null="true"
        />

        <property
            name="localpart"
            type="java.lang.String"
            column="localpart"
            not-null="true"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Account.xml
            containing the additional properties and place it in your merge dir.
        -->
        <subclass
            name="de.splendid.cca.modules.email.model.beans.AliasAccount"
            discriminator-value="AA"
        >

        <many-to-one
            name="realAccount"
            class="de.splendid.cca.modules.email.model.beans.RealAccount"
            cascade="none"
            outer-join="auto"
            column="real_account_id"
        />

            <!--
                To add non XDoclet property mappings, create a file named
                hibernate-properties-AliasAccount.xml
                containing the additional properties and place it in your merge dir.
            -->

        </subclass>
        <subclass
            name="de.splendid.cca.modules.email.model.beans.RealAccount"
            discriminator-value="RA"
        >
        <property
            name="owner"
            type="java.lang.String"
            column="owner"
        />

        <property
            name="size"
            type="int"
            column="size"
        />

        <property
            name="used"
            type="int"
            column="used"
        />

        <property
            name="usedUpdated"
            type="long"
            column="used_updated"
        />

            <!--
                To add non XDoclet property mappings, create a file named
                hibernate-properties-RealAccount.xml
                containing the additional properties and place it in your merge dir.
            -->

        </subclass>
        <subclass
            name="de.splendid.cca.modules.email.model.beans.MailList"
            discriminator-value="ML"
        >

        <set
            name="accountSet"
            table="maillist_account"
            lazy="false"
            cascade="none"
            sort="unsorted"
        >

              <key
                  column="maillist_id"
              >
              </key>

              <many-to-many
                  class="de.splendid.cca.modules.email.model.beans.Account"
                  column="account_id"
                  outer-join="auto"
               />

        </set>

        <set
            name="externalAccountSet"
            table="maillist_externalaccount"
            lazy="true"
            cascade="none"
            sort="unsorted"
        >

              <key
                  column="maillist_id"
              >
              </key>

              <many-to-many
                  class="de.splendid.cca.modules.email.model.beans.ExternalAccount"
                  column="external_account_id"
                  outer-join="auto"
               />

        </set>

        <property
            name="name"
            type="java.lang.String"
            column="maillistname"
        />

            <!--
                To add non XDoclet property mappings, create a file named
                hibernate-properties-MailList.xml
                containing the additional properties and place it in your merge dir.
            -->

        </subclass>

    </class>

</hibernate-mapping>


Name and version of the database you are using:

MySQL 4.0.23


[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 5:49 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
check the .class keywork in HQL

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.