aspose file tools*
The moose likes Object Relational Mapping and the fly likes distinct column records Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "distinct column records" Watch "distinct column records" New topic
Author

distinct column records

taraka ninu
Greenhorn

Joined: Mar 04, 2005
Posts: 5
hi all,

1) am new to hibernate. can anybody tell me how to get Distinct records (in my case distinct column) using hibernate query???
ex : i want something like we get in SQL
Select distinct column1 from table

2) is it possible to get only specific columns like in SQL query ?
ex : i want something like
Select column1 from table

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Both are easy to do:

in SQL would be something like:

You don't need to select a single column in your query - remember you are querying against the Object not the table, so the Object is always completely populated, and you choose which properties you are interested in.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
Originally posted by taraka ninu:

1) am new to hibernate. can anybody tell me how to get Distinct records (in my case distinct column) using hibernate query???
ex : i want something like we get in SQL
Select distinct column1 from table


If you are using the Criteria object, a more appropriate way to do it is

Criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

If you are using HQL or SQL, you can simply specify "distinct" in your query as suggested.

Originally posted by taraka ninu:

2) is it possible to get only specific columns like in SQL query ?
ex : i want something like
Select column1 from table


Hibernate (well, Gavin King) says that the time required to get one column as opposed to 10 columns for a row is not significant. So, Hibernate chooses to retrieve all the columns in a row.

If you are going to use only a subset of columns of a table in your application all the time, you can always map only those columns in your HBM file.


Cheers, Sathya Srinivasan - SCJP 1.2, SCWCD 1.2, SCMAD 1.0
Co-Author of Whizlabs SCMAD Certification Exam Simulator and SCMAD Exam Guide Book
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Sathya Srinivasan:
Hibernate (well, Gavin King) says that the time required to get one column as opposed to 10 columns for a row is not significant.
In general this is true. Compared to the cost of generating the SQL, sending the query to the database, having the database parse and execute the query, and pulling back the results, whether the results consist of 40 or 400 bytes will make no practicable difference.

I say in general because some people create tables of 400 columns. The other case is if the table contains CLOB or BLOB fields since they require more round trips to retrieve as they aren't returned in the result set.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: distinct column records