Hibernate criteria - how to use criteria to return only one element of an object instead the entire object.

Hibernate criteria - how to use criteria to return only one element of an object instead the entire object.

How to use criteria to return only one element of an object instead the entire object?

View Answers

June 19, 2012 at 5:38 PM

You can use projection to select one element of object. Here is an example to retrieve only name of employee.

Example :

package net.roseindia.main;

import java.util.List;

import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;

public class CriteriaById{
    public static void main(String []args){
        SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
        Session session=sessionFactory.openSession();
        Criteria criteria=session.createCriteria(Employee.class).setProjection(Projections.property("name"));
        List<String> employeeList = (List<String>)criteria.list();

        for(String employee: employeeList){
            System.out.println(employee);
        }
    }
}

Output :

Hibernate: select this_.name as y0_ from employee this_
Mandy
Som
Mandy
Roxi

If you want value under condition,you can add restriction.

Criteria criteria=session.createCriteria(Employee.class).add(Restrictions.eq("id",2)).setProjection(Projections.property("name"));

Output:

Hibernate: select this_.name as y0_ from employee this_ where this_.emp_id=?
Som









Related Tutorials/Questions & Answers:
.When to use detached criteria in hibernate?
.When to use detached criteria in hibernate?  When to use detached criteria in hibernate?   Detached Criteria query is a facility provide by Hibernate to write criteria queries in detached mode, where hibernate session
Hibernate criteria restrictions.in
Hibernate criteria restrictions.in In this example, you will see the use... database. Here, you will see the use of in constraint. Syntax : Criteria..., object[] values); The in constraint has two argument one is property name
Advertisements
Hibernate criteria uniqueResult.
Hibernate criteria uniqueResult. Here, we will introduce you to about the uniqueResult() method of Criteria class of hibernate. Hibernate Criteria class... will return a null value. Syntax : Criteria criteria = session.createCriteria
Criteria in Hibernate
Criteria in Hibernate  What is Criteria in Hibernate?   Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like "search" screens
Hibernate Criteria Query Example
Hibernate Criteria Query Example       The Criteria interface allows to create and execute... for Criteria instances. Here is a simple example of Hibernate Criterial Query:ADS
Hibernate criteria disjunction.
Hibernate criteria disjunction.  What is criteria disjunction in hibernate
Hibernate Criteria Join
by criterion objects. There are many options available in hibernate to use criteria query, one of them is Hibernate Criteria Join which performs join operation...Hibernate Criteria is an interface that is used for search functionality
Hibernate criteria for date. Example
Hibernate criteria for date. Example  What to use the Hibernate criteria for date in Hibernate based application? I want example of Hibernate criteria for date in Hibernate orm. Thanks   Example of Hibernate criteria
Hibernate Criteria Queries - Hibernate
Hibernate Criteria Queries  Can I use the Hibernate Criteria Query...().buildSessionFactory(); session = sessionFactory.openSession(); Criteria crit... Configuration(); // configuring hibernate SessionFactory
Hibernate Criteria Examples
in hibernate to use criteria query. Hibernate Criteria Query Example Hibernate...Hibernate Criteria Examples In this section we are giving many example of using Hibernate Criteria query. With the help of criteria query you can
Hibernate criteria conjunction..
Hibernate criteria conjunction..  What is criteria conjunction in Hibernate?    In Hibernate, Criteria Conjunction works as logical... class Employee.java?Hibernate uses the Plain Old Java Object (POJO) classes to map
Hibernate Criteria And Or
Hibernate Criteria And Or The Hibernate Criteria And Or is same...().openSession(); Object object; try { // Creating a Criteria instance... 'Or' you can write as follows. Criteria Or operation Criteria
Hibernate Criteria And Or Example
Hibernate Criteria And Or Example  Hibernate Criteria And Or Example I want example of Hibernate Criteria And Or in Java Thanks   Example of Hibernate Criteria And Or Criteria Check the tutorial Hibernate Criteria
Hibernate Criteria
In this tutorial we are going to discuss Hibernate Criteria with example
Hibernate Criteria Or
In this section we are going to discuss Hibernate Criteria Or with example
Hibernate Criteria Query
Hibernate Criteria Query In this tutorial you will learn about the Hibernate Criteria Query. Hibernate provides an API for writing the dynamic query in an elegant way to execute . Except the use of HQL in Hibernate, Criteria query
Hibernate Criteria And Restrictions
Hibernate Criteria Restriction The Hibernate Criteria API contains Restriction...(), in(), gt(), lt() etc. An example of Hibernate Criteria Restriction class is given..." + ((Student) object).getAddress()); } // Criteria Greater
Hibernate Criteria Transformer Example
Hibernate Criteria Transformer Example  I want example of Hibernate Criteria Transformer to use in my Java project. share the good link. Thanks   Here I am giving you the link of Hibernate Criteria Transforme example
Hibernate Criteria Count Example
Hibernate Criteria Count Example  Example of Hibernate Criteria Count. Check the tutorial Hibernate Criteria Count. Check the Latest tutorials, articles and examples of Hibernate Framework. Thanks
Hibernate criteria date Example
Hibernate criteria date Example  Example of Hibernate criteria date. Check the tutorial hibernate criteria date. Check the Latest tutorials, articles and examples of Hibernate Framework. Thanks
Hibernate Criteria Join
Hibernate Criteria Join API Hibernate Criteria JOIN API allows users...().openSession(); Object object; try { // Creating a Criteria instance... this statement using Criteria in a very simple way Criteria criteria
Hibernate criteria restrictions.between
Hibernate criteria restrictions.between In this tutorial, you will see the use of between constraints of Restricatrions class in java. It is available in org.hibernate.criteria package.  Syntax : Criteria crit
What are criteria Queries in Hibernate?
What are criteria Queries in Hibernate?  Just explain me what is Criteria Query in Hibernate with examples? Thanks   Criteria queries are very helpful in hibernate for queries based on certain criteria. Check
Hibernate criteria to sql.
Hibernate criteria to sql. In this tutorial, you will see the use of sql...().openSession(); Criteria criteria = session.createCriteria(StudentBean.class).add... = obList.iterator(); while (obIter.hasNext()) { StudentBean object
Hibernate criteria query using Max()
Hibernate criteria query using Max()  What is Hibernate criteria query using Max()?   You can find out maximum value in hibernate criteria... in ?org.hibernate.criterion?. For using Projection object in our Criteria, we have to call
Hibernate criteria sqlRestriction.
Hibernate criteria sqlRestriction. In this tutorial, we will introduce you to about the sqlRestriction() of Restrictions class.  Syntax : Criteria criteria = session.createCriteria(StudentBean.class); criteria.add
Hibernate Criteria With Collections Example
Hibernate Criteria With Collections Example  Good code of Hibernate... Hibernate Criteria With Collections in your Java project. Check the tutorial Hibernate Criteria With Collections . Check the Latest tutorials, articles
Hibernate criteria restrictions.or
Hibernate criteria restrictions.or In this tutorial, you will see the use... in org.hibernate.criteria package.  Syntax : Criteria crit...(); Criteria criteria = session.createCriteria(EmpInfo.class).add
What is Hibernate Criteria Transformer?
What is Hibernate Criteria Transformer?  Hi, Can anyone explain me about the Hibernate Criteria Transformer? I need good example of Hibernate Criteria Transformer . Thanks   Hi, check the example Hibernate Criteria
Hibernate Criteria Distinct
Hibernate Criteria Distinct The Hibernate Criteria Distinct API search... object; try { // Creating a Criteria instance Criteria criteria... student WHERE roll_no=2; The above SQL will return the  distinct name
Hibernate-Criteria - Hibernate
Hibernate-Criteria  Hi friends, I am new to hibernate. Im learning it. When im using Criteria im getting exception tht "Criteria cannot... for more information. http://www.roseindia.net/hibernate/hibernate-between
Hibernate criteria avg.
Hibernate criteria avg. In this example, you will see the use of avg() method of Criteria class. It returns average value of the given column.  Syntax : Criteria criteria=session.createCriteria(Pojo.class
Hibernate Detached Criteria Example
Hibernate Detached Criteria Example  can anyone share me the source code of using detached Criteria in Hibernate? Thanks   Hi, If you are looking for the Example program of using Hibernate Detached Criteria
Hibernate criteria setFirstResult.
Hibernate criteria setFirstResult. In this tutorial, you will see the use of seFirstResult() method of criteria class. The setFirstResult() tell hibernate from which row the data should be read. Syntax : Criteria criteria
Hibernate Detached Criteria
This section is good discussion about hibernate detached criteria
Hibernate Criteria Aggregate Functions
Hibernate Criteria Aggregate Functions The Hibernate Criteria Aggregate...(). These functions are of Projections class. Following are the way to use these function in Hibernate Criteria. Criteria criteria = session.createCriteria
Hibernate Criteria
Hibernate Criteria org.hibernate.Criteria is an interface which is very.... It is used where search on multi criteria required, becouse Hibernate Query... the first criteria to include the WHERE sysntax. Hibernate Criteria is very convenient
Hibernate criteria query
Hibernate criteria query  Hi... I need criteria query like query... a.countryId=c.Chrono and a.groupId=b.Chrono and a.type =1 "); like this in hibernate criteria example. Thanks in advance.   Have a look at the following link
object chaining? - Hibernate
object chaining?  daoobject.getsession().begainTransaction; can any one explain the code, i am not understanding this line when iam working with swt and hibernate
Hibernate Criteria Between
Hibernate Criteria Between The Criteria Between method provides some numeric...=criteria.list(); An example of Hibernate Criteria Between is given below... { // Creating Criteria Object Criteria criteria = session.createCriteria(Student.class
Hibernate criteria setMaxResult.
Hibernate criteria setMaxResult. In this tutorial, you will see the use of setMaxResult() method of criteria class. It specifies to total number of result which will be access. Syntax : Criteria criteria=session.createCriteria
Hibernate Criteria Not In
Hibernate Criteria Not In It is reverse of the Hibernate Criteria In, It take a argument of list or collections. Criteria criteria... = HibernateUtil.getSessionFactory().openSession(); Criteria criteria = session.createCriteria
Hibernate Criteria Transformer Example code
Hibernate Criteria Transformer Example code  Hello, I am trying to find the example of Hibernate Criteria Transformer. Tell me the good tutorial of Hibernate Criteria Transformer with source code. Thanks   Hi, Check
Hibernate Criteria Grouping Example
Hibernate Criteria Grouping Example In this tutorial you will learn about the Hibernate Criteria Projection Grouping. As we were using a 'group by'... of projection are defined. Click Here for Hibernate Criteria Ordering tutorialADS
Hibernate criteria sum.
Hibernate criteria  sum. Here, we will introduce you to about the sum() function . It returns the sum of total value of column. Syntax : Criteria criteria=session.createCriteria(Pojo.class); criteria.setProjection
Hibernate Criteria Group By
Hibernate Criteria Group By The Hibernate Criteria Group By is used to display... records in Group By in a following way Criteria criteria... = HibernateUtil.getSessionFactory().openSession(); Criteria criteria = session.createCriteria
Hibernate Criteria Transformer
Hibernate Criteria Transformer A Hibernate Criteria Transformer is an interface which allow you to transform any result of Hibernate Criteria element. Criteria criteria = session.createCriteria(Student.class); criteria.add
what is object chaining? - Hibernate
what is object chaining?   daoobject.getsession().begainTransaction; can any one explain the code, i am understanding this line when iam working with swt and hibernate
Hibernate criteria date between Example
Hibernate criteria date between Example  Hibernate criteria date between Example I want example of hibernate criteria date between with source code. Share me the url. Thanks   Example of Hibernate criteria date
Hibernate Criteria Fetchmode Eager Example
Hibernate Criteria Fetchmode Eager Example   I am searching for the Hibernate Criteria Fetchmode Eager Example. It will be helpful for me if you share the code of Hibernate Criteria Fetchmode Eager Example . Thanks   

Ads