-->
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.  [ 6 posts ] 
Author Message
 Post subject: how to make a distinct
PostPosted: Thu Jul 03, 2008 5:17 am 
Newbie

Joined: Thu Jul 03, 2008 5:02 am
Posts: 5
Hi everybody.

I'm a new user of hibernate and jsp and i have this problem:

I Have this method in my DAOBean class:

public Set listScheda() throws Exception {
try {
Set<Scheda> ret = new HashSet<Scheda>();
ret.addAll(manager.createQuery("from Scheda scheda").getResultList());
return ret;
} catch (Exception e) {
log.error("listScheda() - Failed to find scheda: "+e);
throw e;
}
}

I want a query like this :

select distinct taglia_scheda from scheda.

I try to do :

public Set listScheda() throws Exception {
try {
Set<Scheda> ret = new HashSet<Scheda>();
ret.addAll(manager.createQuery("select distinct scheda.taglia_scheda from Scheda scheda").getResultList());
return ret;
} catch (Exception e) {
log.error("listScheda() - Failed to find scheda: "+e);
throw e;
}
}

But it doesn't works

In particular it sends me an error like this:

java.lang.ClassCastException: java.lang.String
it.yacme.mystiquexml.business.YacmeBean.listTaglie(YacmeBean.java:158)

The method listTaglie is this one:

public Set listTaglie() throws Exception{
Set <Scheda> ret = new HashSet<Scheda>();
try{
Set<Scheda> list = yacmeDAO.listTaglie();
for (Iterator<Scheda> iterator = list.iterator(); iterator.hasNext();) {
Scheda s = iterator.next();
ret.add(s);
}
}catch (Exception e){
log.error("listTaglie() - Failed to list Taglie: "+e);
throw e;
}
return ret;

}

Can you Help me please?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 8:32 am 
Newbie

Joined: Mon Mar 26, 2007 9:22 am
Posts: 9
Quote:
The distinct and all keywords may be used and have the same semantics as in SQL.


http://www.hibernate.org/hib_docs/refer ... ryhql.html

so your query could look like that:

Code:
public List<Scheda> listScheda() throws Exception {
return manager.createQuery("select distinct s from Scheda s").getResultList();
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 9:13 am 
Newbie

Joined: Thu Jul 03, 2008 5:02 am
Posts: 5
Thanks but this is not what i want.

This HQL query is like SQL query: select distinct * from scheda;

I want: select distinct taglia_scheda from scheda;

i try to do:

public List listScheda() throws Exception {
return manager.createQuery("select distinct s.taglia_scheda from Scheda s").getResultList();
}

but this is the result:

java.lang.ClassCastException: java.lang.String
it.yacme.mystiquexml.business.YacmeBean.listTaglie(YacmeBean.java:174)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 2:23 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The query

Code:
select distinct s.taglia_scheda from Scheda s


only selects the taglia_scheda column, not complete Scheda objects. From the error message you get I assume that taglia_scheda is a string column. Then, your code should be like this:

Code:

Set<String> ret = new HashSet<String>();
ret.addAll(manager.createQuery("select distinct scheda.taglia_scheda from Scheda scheda").getResultList());


Another option is to use a TreeSet instead of a HashSet and then use a Comparator that works on the taglia_scheda column. Eg. something like this:

Code:
Set<Scheda> ret = new TreeSet<Scheda>(new TagliaSchedaComparator());
ret.addAll(manager.createQuery("select scheda from Scheda scheda").getResultList());


Top
 Profile  
 
 Post subject: Cast Exception
PostPosted: Thu Jul 31, 2008 12:00 pm 
Newbie

Joined: Thu Jul 31, 2008 11:49 am
Posts: 2
getting java.lang.CastException

for(Iterator it=list.iterator();it.hasNext();){
[color=darkred]//***Getting exception at this line ********[/color] TestPayroll payroll = (TestPayroll) it.next();
}

Method below gets TestPayroll
public List getRecords(te....,....){
List list = (List)this.getHibernateTemplate().executeFind(
new HibernateCallback(){
public Object doInHibernate(Session session) throws
HibernateException, SQLException{
try{
Query query = session.createQuery("select distinct
payrollPK.accountId as accountID , payrollPK.empId as empId," +
"(select sum(e2.amount) from ErsxpPayroll e2 where e2.payrollPK.accountId = e1.payrollPK.accountId) as amount " +
"from TestPayroll e1 " +
"where client_code=:clientCode " +
"and rpcode=:rpCode " +
"and begdate >= :begDate " +
"and enddate <= :endDate " +
"and empid in (:empIdList) " +
"group by e1.payrollPK.accountId, e1.payrollPK.empId,e1.amount "
//,payrollPK.clientCode, begDate, payrollPK.endDate,rpCode,rpType,postDate
)


return query.List

_________________
Life is journey not a destination


Top
 Profile  
 
 Post subject: Cast Exception
PostPosted: Thu Jul 31, 2008 12:00 pm 
Newbie

Joined: Thu Jul 31, 2008 11:49 am
Posts: 2
getting java.lang.CastException

for(Iterator it=list.iterator();it.hasNext();){
[color=darkred]//***Getting exception at this line ********[/color] TestPayroll payroll = (TestPayroll) it.next();
}

Method below gets TestPayroll
public List getRecords(te....,....){
List list = (List)this.getHibernateTemplate().executeFind(
new HibernateCallback(){
public Object doInHibernate(Session session) throws
HibernateException, SQLException{
try{
Query query = session.createQuery("select distinct
payrollPK.accountId as accountID , payrollPK.empId as empId," +
"(select sum(e2.amount) from ErsxpPayroll e2 where e2.payrollPK.accountId = e1.payrollPK.accountId) as amount " +
"from TestPayroll e1 " +
"where client_code=:clientCode " +
"and rpcode=:rpCode " +
"and begdate >= :begDate " +
"and enddate <= :endDate " +
"and empid in (:empIdList) " +
"group by e1.payrollPK.accountId, e1.payrollPK.empId,e1.amount "
//,payrollPK.clientCode, begDate, payrollPK.endDate,rpCode,rpType,postDate
)


return query.List

_________________
Life is journey not a destination


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