-->
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.  [ 8 posts ] 
Author Message
 Post subject: problem with ordered results using criteria
PostPosted: Thu Mar 03, 2005 6:08 am 
Newbie

Joined: Thu Mar 03, 2005 5:09 am
Posts: 7
First i'm not good at English so I hope you understood my mistake.

I want to get some ordered results by hibernate Creteria.

First native query that I want to ordered results is

select * from foo order by regist_date;

1. table DDL is


# Table: 'foo'
#
CREATE TABLE `foo` (
`sort_id` bigint(20) NOT NULL default '0',
`regist_date` varchar(7) NOT NULL default '',
`foo_id` varchar(64) default '',
`foo_name` varchar(100) default '',
PRIMARY KEY (`sort_id`,`reg_date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
;


2. Generated DTO is
Foo.java, FooPK.java

3. config is

<class
name="Foo"
table="foo"
>

<composite-id name="comp_id" class="FooPK">
<key-property
name="sortId"
column="sort_id"
type="java.lang.Long"
length="20"
/>
<key-property
name="registDate"
column="regist_date"
type="java.lang.String"
length="7"
/>
</composite-id>

<property
name="FooId"
type="java.lang.String"
column="foo_id"
length="64"
/>
......


above is my env.
and then How do I get ordered results use criteria..

I using criteria like this :

Session session = HibernateUtil.getSession();
List zines = session.createCriteria(ZinePopular.class)
.addOrder(Order.desc("regDate"))
.setMaxResults(5)
.list();

how can I??
thanks your help


Top
 Profile  
 
 Post subject: Re: problem with ordered results using criteria
PostPosted: Thu Mar 03, 2005 6:09 am 
Newbie

Joined: Thu Mar 03, 2005 5:09 am
Posts: 7
titlet wrote:
First i'm not good at English so I hope you understood my mistake.

I want to get some ordered results by hibernate Creteria.

First native query that I want to ordered results is

select * from foo order by regist_date;

1. table DDL is


# Table: 'foo'
#
CREATE TABLE `foo` (
`sort_id` bigint(20) NOT NULL default '0',
`regist_date` varchar(7) NOT NULL default '',
`foo_id` varchar(64) default '',
`foo_name` varchar(100) default '',
PRIMARY KEY (`sort_id`,`reg_date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
;


2. Generated DTO is
Foo.java, FooPK.java

3. config is

<class
name="Foo"
table="foo"
>

<composite-id name="comp_id" class="FooPK">
<key-property
name="sortId"
column="sort_id"
type="java.lang.Long"
length="20"
/>
<key-property
name="registDate"
column="regist_date"
type="java.lang.String"
length="7"
/>
</composite-id>

<property
name="FooId"
type="java.lang.String"
column="foo_id"
length="64"
/>
......


above is my env.
and then How do I get ordered results use criteria..

I using criteria like this :

Session session = HibernateUtil.getSession();
List zines = session.createCriteria(Foo.class)
.addOrder(Order.desc("registDate"))
.setMaxResults(5)
.list();

how can I??
thanks your help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 6:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Where is your problem?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 7:35 am 
Newbie

Joined: Thu Mar 03, 2005 5:09 am
Posts: 7
michael wrote:
Where is your problem?


my problem is

Session session = HibernateUtil.getSession();
List zines = session.createCriteria(Foo.class)
.addOrder(Order.desc("registDate"))
.setMaxResults(5)
.list();

this code occured error...

net.sf.hibernate.QueryException: could not resolve property: ZinePopularPK.regDate of: com.haansoft.kaatal.model.dto.ZinePopular
at net.sf.hibernate.persister.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:50)
at net.sf.hibernate.expression.AbstractCriterion.getColumns(AbstractCriterion.java:35)
at net.sf.hibernate.expression.Order.toSqlString(Order.java:37)
at net.sf.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:78)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3595)
at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:238)


of course this error is occured why Foo.java class have not 'registDate' variable that is located in FooPK.java class. [registDate is Primary Key]

so I can't order by registDate....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 7:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can use registDate as in the mapping but you are using regDate (see the exception)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 8:04 am 
Newbie

Joined: Thu Mar 03, 2005 5:09 am
Posts: 7
michael wrote:
You can use registDate as in the mapping but you are using regDate (see the exception)


oh sorry it's my mistacke i convert regDate to registDate to this board...

original source code and message is all using regDate........
[it's not error ]

My question is how can I ordering use Primary Key cloumn.
[Condition:
1. Using Criteria class and composite-id tag
2. There is two DTO class one is Foo.java(have FooPK class getter & setter) other is FooPK.java (just have PK)
]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 10:01 am 
Newbie

Joined: Mon Feb 28, 2005 12:53 pm
Posts: 18
Session session = HibernateUtil.getSession();
List zines = session.createCriteria(Foo.class)
.addOrder(Order.desc("comp_id.registDate"))
.setMaxResults(5)
.list();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 11:07 am 
Newbie

Joined: Thu Mar 03, 2005 5:09 am
Posts: 7
dpmihai wrote:
Session session = HibernateUtil.getSession();
List zines = session.createCriteria(Foo.class)
.addOrder(Order.desc("comp_id.registDate"))
.setMaxResults(5)
.list();


thanks your help ^^;

have nice today...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.