Home > Database > Mysql Tutorial > body text

How to Specify Specific Columns in Hibernate Criteria Queries?

Linda Hamilton
Release: 2024-11-02 11:08:30
Original
871 people have browsed it

How to Specify Specific Columns in Hibernate Criteria Queries?

Specifying Specific Columns in Hibernate Criteria Queries

In Hibernate, the default Criteria Query retrieves all columns from the target table. However, it is often desirable to exclude certain columns for performance or security reasons. Here's how to accomplish this:

Using Projections

Projections allow you to specify the specific columns you wish to retrieve. This can be achieved by using the Projections.projectionList() method and adding the desired properties:

<code class="java">Criteria cr = session.createCriteria(User.class)
    .setProjection(Projections.projectionList()
      .add(Projections.property("id"), "id")
      .add(Projections.property("Name"), "Name"));</code>
Copy after login

Handling Alias Conflict

When using projections, it's necessary to handle alias conflicts since Hibernate generates aliases for the returned columns. To resolve this, you can use the setResultTransformer() method with Transformers.aliasToBean() to transform the results into an expected object:

<code class="java">cr.setResultTransformer(Transformers.aliasToBean(User.class));</code>
Copy after login

Query Example

Consider the updated query in your post to retrieve specific columns:

<code class="sql">select
    this_.TEMPLATE_ID,
    this_.TEMPLATE_NAME,
    this_.CREATE_DATE,
    this_.UPDATE_DATE,
    this_.STATUS_CODE,
    this_.USER_ID
from
    templates this_ 
where
    this_.STATUS_CODE = 1
    and this_.PRACTICE_ID = 1 
    and this_.USER_ID in (1, 2) 
order by
    this_.TEMPLATE_NAME asc
limit ?</code>
Copy after login

To translate this into a Hibernate Criteria Query, you can use the following code:

<code class="java">Criteria cr = session.createCriteria(Template.class)
    .setProjection(Projections.projectionList()
      .add(Projections.property("TEMPLATE_ID"), "TEMPLATE_ID")
      .add(Projections.property("TEMPLATE_NAME"), "TEMPLATE_NAME")
      .add(Projections.property("CREATE_DATE"), "CREATE_DATE")
      .add(Projections.property("UPDATE_DATE"), "UPDATE_DATE")
      .add(Projections.property("STATUS_CODE"), "STATUS_CODE")
      .add(Projections.property("USER_ID"), "USER_ID"))
    .add(Restrictions.eq("this_.STATUS_CODE", 1))
    .add(Restrictions.eq("this_.PRACTICE_ID", 1))
    .add(Restrictions.in("this_.USER_ID", Arrays.asList(1, 2)))
    .addOrder(Order.asc("this_.TEMPLATE_NAME"))
    .setResultTransformer(Transformers.aliasToBean(Template.class));</code>
Copy after login

The above is the detailed content of How to Specify Specific Columns in Hibernate Criteria Queries?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!