-->
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.  [ 5 posts ] 
Author Message
 Post subject: Simple Question : How do you filter ?
PostPosted: Tue Sep 26, 2006 7:28 am 
Beginner
Beginner

Joined: Wed Sep 13, 2006 7:24 am
Posts: 26
I am not sure how this is done, but the documents talks of writing SQL by hand.

I have have a load of POJOs which map to tables, and the Web Developers want to send queries type request
(i.e. to filter the returned set).


So my question are

1. how do you filter for a particular class type
2. How do you get the a subset of the imformation (like for drop down boxes, no point sending all rows when only id and name is required ?).

please please help ... i will credit :)

_________________
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 8:11 am 
Regular
Regular

Joined: Mon May 08, 2006 6:00 am
Posts: 53
Location: India
If i have got your problem right, this is what you want

you have a pojo

TableBean{

int id;
int varColumn2;
int varColumn3;
int varColumn4;
int varColumn5;

}

Now you want to fetch only id and varColumn1.

This is definitely achievable using HQL. See following e.g, same has been given in book also on page no 271

Iterator i = session.createQuery(
"select new ItemRow( item.id, item.description, bid.amount ) " +
"from Item item join item.bids bid " +
"where bid.amount > 100"
)
.list()
.iterator();
while ( i.hasNext() ) {
ItemRow row = (ItemRow) i.next();
// Do something
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 8:40 am 
Beginner
Beginner

Joined: Wed Sep 13, 2006 7:24 am
Posts: 26
Am i right in assuming that "ItemRow" is a pre-created class that has the fields "id", "description" and "amount".

So dealing with the basic concept of a LabelBean

the SQL would be like
Code:
"Select new LabelBean(person.id,person.name) From Employee person"


would work so lone as a had a LableBean class with a construt of
Code:
LabelBean(String,String)


Also, are the field names, the ones in the table or the ones in the POJO object (Mapping Class).

_________________
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 12:59 pm 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
bengrant wrote:
Am i right in assuming that "ItemRow" is a pre-created class that has the fields "id", "description" and "amount".

So dealing with the basic concept of a LabelBean

the SQL would be like
Code:
"Select new LabelBean(person.id,person.name) From Employee person"


would work so long as a had a LableBean class with a construt of
Code:
LabelBean(String,String)


Yes and ItemRow does not need to be defined within the Hibernate configuration.
Quote:
Also, are the field names, the ones in the table or the ones in the POJO object (Mapping Class).

Property names in your POJO.

_________________
---- Don't forget to rate! ----


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 7:05 am 
Beginner
Beginner

Joined: Wed Sep 13, 2006 7:24 am
Posts: 26
so i attempted the code, but i have run in to some problems

so i try to run this
Code:
.
.
.
   Session sess = getCurrentSession();

        List i = sess.createQuery("SELECT new LabelBean(ag.groupId,ag.groupName) " +
                " FROM AudienceGroup ag JOIN ag.UsersSet user " +
                "WHERE user.userId ='system'").list();


and everything seems fine apart from this error

Code:
[2006-09-28 11:56:05,396] DEBUG [main] ErrorCounter: throwQueryException() : no errors
[2006-09-28 11:56:05,521] INFO  [Finalizer] DriverManagerConnectionProvider: cleaning up connection pool: jdbc:hsqldb:mem:Test
[2006-09-28 11:56:05,568] DEBUG [main] HqlSqlBaseWalker: query() << begin, level = 1
[2006-09-28 11:56:05,615] DEBUG [main] FromElement: FromClause{level=1} :  data.pojos.AudienceGroup (ag) -> audiencegr0_
[2006-09-28 11:56:05,615] DEBUG [main] FromReferenceNode: Resolved :  ag -> audiencegr0_.group_id
[2006-09-28 11:56:05,615] DEBUG [main] DotNode: getDataType() : UsersSet -> org.hibernate.type.SetType(data.pojos.AudienceGroup.UsersSet)
[2006-09-28 11:56:05,615] DEBUG [main] FromElementFactory: createEntityAssociation() : One to many - path = ag.UsersSet role = data.pojos.AudienceGroup.UsersSet associatedEntityName = elmo.data.pojos.User
[2006-09-28 11:56:05,615] DEBUG [main] FromElement: FromClause{level=1} :  data.pojos.User (user) -> usersset1_
[2006-09-28 11:56:05,615] DEBUG [main] FromClause: addJoinByPathMap() : ag.UsersSet -> ELMO_Users usersset1_
[2006-09-28 11:56:05,615] DEBUG [main] DotNode: dereferenceCollection() : Created new FROM element for ag.UsersSet : ELMO_Users usersset1_
[2006-09-28 11:56:05,615] DEBUG [main] FromReferenceNode: Resolved :  ag.UsersSet -> .
[2006-09-28 11:56:05,630] DEBUG [main] HqlSqlWalker: createFromJoinElement() : -- join tree --
\-[JOIN_FRAGMENT] FromElement: 'ELMO_Users usersset1_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=user,role=data.pojos.AudienceGroup.UsersSet,tableName=ELMO_Users,tableAlias=usersset1_,colums={audiencegr0_.group_id ,className=data.pojos.User}}

[2006-09-28 11:56:05,646] DEBUG [main] FromReferenceNode: Resolved :  ag -> audiencegr0_.group_id
[2006-09-28 11:56:05,646] DEBUG [main] DotNode: getDataType() : groupId -> org.hibernate.type.IntegerType@52c6b4
[2006-09-28 11:56:05,646] DEBUG [main] FromReferenceNode: Resolved :  ag.groupId -> audiencegr0_.group_id
[2006-09-28 11:56:05,646] DEBUG [main] FromReferenceNode: Resolved :  ag -> audiencegr0_.group_id
[2006-09-28 11:56:05,646] DEBUG [main] DotNode: getDataType() : groupName -> org.hibernate.type.StringType@738d08
[2006-09-28 11:56:05,646] DEBUG [main] FromReferenceNode: Resolved :  ag.groupName -> audiencegr0_.group_name
[2006-09-28 11:56:05,662] ERROR [main] PARSER: *** ERROR:  Unable to locate class [LabelBean]
[cause=java.lang.ClassNotFoundException: LabelBean]
[2006-09-28 11:56:05,662] DEBUG [main] ErrorCounter:  Unable to locate class [LabelBean]
[cause=java.lang.ClassNotFoundException: LabelBean]
Unable to locate class [LabelBean]
[cause=java.lang.ClassNotFoundException: LabelBean]
   at org.hibernate.hql.ast.ConstructorNode.resolveConstructor(ConstructorNode.java:115)
   at org.hibernate.hql.ast.ConstructorNode.prepare(ConstructorNode.java:86)
   at org.hibernate.hql.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:543)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1598)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1467)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1041)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:380)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
   at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
   at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at web.dao.UserDAO.getUserGroup(UserDAO.java:51)
   at web.dao.UserDAOTest.testGetUserGroup(UserDAOTest.java:26)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


i am stummped as to where i should be placeing the class ? do i need to define it somewhere for Hibernate to pick it up ?

_________________
Ben


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