Hi,
I have three tables, video, photo and content.
video and photo are sub classes of content.
Now I want to select the count of all content items using a criteria:
Code:
public int findContentCount()
{
Criteria crit = this.getSession().createCriteria(Content.class);
crit.setProjection(Projections.rowCount());
Integer tmp = (Integer)crit.uniqueResult();
return tmp != null ? tmp : 0;
}
Following SQL command is generated:
Code:
select count(*) as y0_
from public.content this_
left outer join public.video this_1_ on this_.contentid=this_1_.contentid
left outer join public.photo this_2_ on this_.contentid=this_2_.contentid
I get the same result without all of those left outer joins, just faster (half of the time).
Is there a way to prevent hibernate to add the left outer joins when doing a select count?
Best regards
Humppa!
PS:
Here is the mapping file of the content class:
Code:
<hibernate-mapping schema="public">
<class name="app.Hibernate.Tables.content.Content" table="content"
lazy="true">
<id name="contentid" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">content_contentid_seq</param>
</generator>
</id>
<property name="views" not-null="true" />
<property name="dateadded" not-null="true" />
<property name="allowcomment" not-null="true" />
<property name="reported" not-null="true" />
<property name="adminchecked" />
<property name="adminlockid" />
<!-- Many-to-one mapping: Ban -->
<many-to-one name="ban" cascade="all" column="banid" lazy="false"
fetch="join" class="app.Hibernate.Tables.ban.Ban" />
<!-- Many-to-one mapping: Parent Description -->
<many-to-one name="description" column="descriptionid"
cascade="all" class="app.Hibernate.Tables.comment.Comment"
lazy="false" fetch="join" />
<!-- Many-to-one mapping: User -->
<many-to-one name="user" column="userid" cascade="none"
fetch="join" lazy="false" class="app.Hibernate.Tables.user.User"
not-null="true" />
<!-- Many-to-many mapping: Positions -->
<bag name="positions" table="contentposition" cascade="all-delete-orphan">
<key column="contentid" />
<one-to-many
class="app.Hibernate.Tables.content.contentposition.ContentPosition" />
</bag>
<!-- Many-to-many mapping: Flags -->
<bag name="flags" table="flagged" cascade="all-delete-orphan">
<key column="contentid" />
<one-to-many
class="app.Hibernate.Tables.flagged.Flagged" />
</bag>
<!-- Many-to-one mapping: ContentPosition -->
<many-to-one name="position" cascade="save-update" column="contentpositionid"
lazy="false" fetch="join"
class="app.Hibernate.Tables.content.contentposition.ContentPosition">
</many-to-one>
<!-- Many-to-many mapping: Comments -->
<bag name="comments" table="contentcomments" cascade="none">
<key column="contentid" />
<many-to-many class="app.Hibernate.Tables.comment.Comment"
column="commentid" />
</bag>
<!-- Many-to-many mapping: Categories -->
<bag name="categories" table="contentcategories" cascade="none">
<key column="contentid" />
<many-to-many class="app.Hibernate.Tables.category.Category"
column="categoryid" />
</bag>
<!-- Many-to-many mapping: Ratings -->
<bag name="ratings" table="contentratings" cascade="none" inverse="false">
<key column="contentid" />
<many-to-many class="app.Hibernate.Tables.rating.Rating"
column="ratingid" />
</bag>
<!-- Many-to-many mapping: Keywords -->
<bag name="keywords" table="contentkeywords" cascade="save-update">
<key column="contentid" />
<many-to-many class="app.Hibernate.Tables.keyword.Keyword"
column="keyword" />
<sql-insert callable="true" check="none">{call
insert_contentkeyword( ?, ? )}</sql-insert>
<sql-update callable="true" check="none">{call
insert_contentkeyword( ?, ? )}</sql-update>
</bag>
<joined-subclass name="app.Hibernate.Tables.content.video.Video"
table="video">
<key column="contentid" foreign-key="true" not-null="true"
unique="true">
</key>
<property name="videoid" unique-key="true" not-null="true"
insert="false" update="false">
</property>
<property name="url" not-null="true" />
<property name="thumbnailurl" />
<property name="recorddate" />
<property name="hosterid" />
<property name="hoster" />
<property name="lastapiupdate" />
</joined-subclass>
<joined-subclass name="app.Hibernate.Tables.content.photo.Photo"
table="photo">
<key column="contentid" foreign-key="true" not-null="true"
unique="true">
</key>
<property name="photoid" unique-key="true" not-null="true"
insert="false" update="false">
</property>
<property name="recorddate" />
<property name="serverip" />
<property name="originalname" />
<property name="md5" />
</joined-subclass>
</class>
</hibernate-mapping>