-->
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.  [ 7 posts ] 
Author Message
 Post subject: Criteria to select columns from a table [Solved]
PostPosted: Mon May 03, 2010 4:01 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
Hi everybody!
I need to create a Criteria using this sql select:
Code:
select code, title, city, date from book;

In this link i can not mount this what I wanted:
http://docs.jboss.org/hibernate/core/3. ... teria.html
Thanks!


Last edited by Guevara on Tue May 11, 2010 4:53 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Criteria to select columns from a table
PostPosted: Tue May 04, 2010 3:42 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
could anyone help?


Top
 Profile  
 
 Post subject: Re: Criteria to select columns from a table
PostPosted: Mon May 10, 2010 4:02 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
Up! ¬¬


Top
 Profile  
 
 Post subject: Re: Criteria to select columns from a table
PostPosted: Mon May 10, 2010 4:04 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
Up! ¬¬


Top
 Profile  
 
 Post subject: Re: Criteria to select columns from a table
PostPosted: Mon May 10, 2010 4:38 pm 
Newbie

Joined: Mon May 10, 2010 1:41 pm
Posts: 15
Not certain exactly what you mean:

If you need to SELECT only part of an object with criteria then you are looking to use Projections (Think of Projection as SELECT), for a working example please see:

http://www.roseindia.net/hibernate/proj ... ions.shtml

In the reference manual see: 16.7. Projections, aggregation and grouping


Top
 Profile  
 
 Post subject: Re: Criteria to select columns from a table
PostPosted: Tue May 11, 2010 4:08 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
If I have:
Code:
public List<Messages> list() {
      return session.createCriteria(Message.class, "m")
      .setProjection(Projections.projectionList()
            .add( Projections.property("m.subject").as("subject") )
            .add( Projections.property("m.message").as("message") )
            ).setResultTransformer(new AliasToBeanResultTransformer(Message.class)).list();
   }

Where put the FK of Contact ID?
Image
assunto = subject
mensagem = message
contato_id_contato = id contact

Thanks!


Top
 Profile  
 
 Post subject: Re: Criteria to select columns from a table
PostPosted: Tue May 11, 2010 4:52 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
Solved!
Code:
@SuppressWarnings("unchecked")
   public List<Mensagem> lista(Long idContato) {       
      return session.createCriteria(Mensagem.class, "m")
      .setProjection(Projections.projectionList()
            .add( Projections.property("m.assunto").as("assunto") )
            .add( Projections.property("m.mensagem").as("mensagem") )
            ).setResultTransformer(new AliasToBeanResultTransformer(Mensagem.class))
            .add(Restrictions.idEq(idContato))
            .setMaxResults(10).list();
    } 

Thanks!


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