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: Query with criteria
PostPosted: Wed Oct 27, 2004 12:49 am 
Newbie

Joined: Tue Oct 26, 2004 11:42 am
Posts: 3
Cannot query with criteria is like below

Hibernate version:

Mapping documents:
<class name="Contents" table="T_CONTENTS" proxy="Contents">
<id name="id" column="CONTENTS_ID" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<property name="title" not-null="true" column="F_TITLE"/>
<property name="brief" column="F_BRIEF"/>
<property name="content" column="F_CONTENT"/>
<property name="createDate"
column="F_CREATED"
type="java.util.Date"
not-null="true"/>
<property name="status" column="F_STATUS"/>
<many-to-one
name="category"
column="CATEGORY_ID"
class="Category"
not-null="true"
/>
<!--
<set name="images"
lazy="true"
table="T_IMAGES"
order-by="F_TITLE asc">
<key column="CONTENTS_ID"/>
<composite-element class="Image">
<property name="title" column="F_TITLE"/>
<property name="description" column="F_DESCRIPTION"/>
<property name="path" column="F_PATH"/>
<property name="thumbnail" column="F_THUMBNAIL"/>
<property name="createDate"
column="F_CREATED"
type="java.util.Date"
not-null="true"/>
</composite-element>
</set>
-->
</class>


Code between sessionFactory.openSession() and session.close():
public Collection findByCategory(String categoryID)
throws InfrastructureException {
Collection result=new ArrayList();
Session session = HibernateUtil.getSession();
Category cat = null;

try {
cat = (Category) session.load(Category.class, categoryID);
} catch (HibernateException e) {
throw new InfrastructureException(e);
}
Criteria crit = HibernateUtil.getSession().createCriteria(
Contents.class);
Contents c = new Contents();
c.setCreateDate(null);
System.out.print(cat.getId());
c.setCategory(cat);
try {
result= crit.add(Example.create(c)).list();
} catch (HibernateException e1) {
throw new InfrastructureException(e1);
}
System.out.print(result.size());
return result;

}



The generated SQL (show_sql=true):
Hibernate: select this.CONTENTS_ID as CONTENTS1_0_, this.F_TITLE as F_TITLE0_, t
his.F_BRIEF as F_BRIEF0_, this.F_CONTENT as F_CONTENT0_, this.F_CREATED as F_CRE
ATED0_, this.F_STATUS as F_STATUS0_, this.CATEGORY_ID as CATEGORY7_0_ from T_CON
TENTS this where 1=1

the query seem wrong

How do I can query by category ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 12:15 pm 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
Example criteria will not work with associations. Only fields in the base object.

I tried to add this, but Gavin had reasons not to let it in....


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.