-->
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: Restricting the field selection
PostPosted: Wed May 31, 2006 5:42 pm 
Beginner
Beginner

Joined: Fri May 26, 2006 7:15 am
Posts: 20
Location: Vandavasi, TamilNadu
Hibernate version: 3.1

Hi all,

I am using Criteria API to run a qry. Is there any way to restrict the fields in the select stmt(say only 4 fields out of 10 fields)? I don't want to have all the fields in the select statement.


Thanks

_________________
gajini
------------------


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 8:40 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you mean fields in the where clause, that already happens.

If you mean fields in the select clause, then no. Criteria query for objects, not result sets: to build an object, hibernate needs all of its values. You can't build an object from some of the fields: that's just a partial object.

The exception to this are projection Criteria: count, max, group by, etc.
The best ways to get just a few fields from a few rows is to use SQL or HQL. Criteria aren't suited to this task.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 7:00 am 
Beginner
Beginner

Joined: Fri May 26, 2006 7:15 am
Posts: 20
Location: Vandavasi, TamilNadu
ok

I am trying to restrict the field selection using data-transfer JavaBean objects (http://www.javalobby.org/articles/hiber ... /#advanced). But getting error. Below are the details.

Thanks
gajini
--------------------------------$-----------Hibernate version: 3.1 -------

TblEvent
-------
id
title
date

titleDTO.java : temp Java Bean
-----------
title : String


Between Trans:
------------------
String qry1 = "select new titleDTO(evt.title) from Event evt";
List result = session.createQuery("qry1").list();
Object evt = (Object)result.get(1);
S.o.p( evt.toString() );

Error
-------
Initial SessionFactory creation failed.org.hibernate.HibernateException: Errors in named queries: eventQry
java.lang.ExceptionInInitializerError
at util.HibernateUtil.<clinit>(HibernateUtil.java:17)
at events.EventManager.main(EventManager.java:22)
Caused by: org.hibernate.HibernateException: Errors in named queries: eventQry
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:339)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at util.HibernateUtil.<clinit>(HibernateUtil.java:13)
... 1 more
Exception in thread "main"
--------------------------------$------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 8:09 am 
Beginner
Beginner

Joined: Fri May 26, 2006 7:15 am
Posts: 20
Location: Vandavasi, TamilNadu
titleDTO.java
-----------------------

public class titleDTO implements Serializable{

private String title;

public titleDTO(){}

public titleDTO(String title){
this.title=title;
}

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 8:54 am 
Beginner
Beginner

Joined: Fri May 26, 2006 7:15 am
Posts: 20
Location: Vandavasi, TamilNadu
hey

got it


thanks

_________________
gajini
------------------


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.