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

 
 


ratna rathor
Hibernate criteria - how to use criteria to return only one element of an object instead the entire object.
1 Answer(s)      a year ago
Posted in : Hibernate

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 Pages:
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
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 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
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 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
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 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 tutorial
Hibernate Criteria Associations
Hibernate Criteria Associations In this section you will learn about Criteria Query Association in Hibernate. Association in Criteria Query may be done... createCriteria() method is used it returns a new instance of Criteria interface
hibernate criteria 'And' 'or' condition Example
hibernate criteria 'And' 'or' condition Example In this Example, We will discuss about hibernate criteria query, In this example we create a criteria instance and implement the factory methods for obtaining certain built
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 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:  
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 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 RowCount Example
Hibernate Criteria RowCount Example In this Example, We will discuss about hibernate criteria query, The class org.hibernate.criterion.Projections .... In this example we create a criteria instance and implement the Projections
projection criteria hibernate Example
projection criteria hibernate Example In this Example, We will discuss about hibernate criteria query, The class org.hibernate.criterion.Projections.... In this example we create a criteria instance and implement the Projections
Hibernate Criteria GroupBy Example
Hibernate Criteria GroupBy Example In this Example, We will discuss about hibernate criteria query, The class org.hibernate.criterion.Projections .... In this example we create a criteria instance and implement the Projections class
hibernate criteria or condition Example
hibernate criteria or condition Example In this Example, We will discuss about hibernate criteria query, In this example we create a criteria instance... a Criteria instance Criteria crit = session.createCriteria(User.class
Hibernate Criteria Query Example
Hibernate Criteria Query Example In this Example, We will discuss about hibernate criteria query, The interface org.hibernate.Criteria is used to create the criterion for the search. In this example we create a criteria instance
Hibernate Criteria setFirstResult Example
Hibernate Criteria setFirstResult Example In this Example, We will discuss about hibernate criteria query, The interface org.hibernate.Criteria... a criteria instance and implement the setFirstResult method. In This example set
hibernate criteria disjunction
hibernate criteria Disjunction In this Example, We will discuss about hibernate criteria query, In this example we create a criteria instance and implement...; try { // Creating a Criteria instance Criteria crit
hibernate criteria date between
hibernate criteria Date Between Example In this Tutorial, We will discuss about hibernate criteria query, The interface org.hibernate.criterion.Restrictions... create a criteria instance and implement the factory methods for obtaining
Hibernate Criteria Equal Example
Hibernate Criteria Equal Example In this Example, We will discuss about hibernate criteria query, The interface.... In this example we create a criteria instance and implement the factory methods
Hibernate Criteria NotEqual Example
Hibernate Criteria NotEqual Example In this Example, We will discuss about hibernate criteria query, The interface.... In this example we create a criteria instance and implement the factory methods
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 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 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 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
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
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 Unique Result Example
hibernate criteria Unique Result Example In this Example, We will discuss about hibernate criteria query, In this example we create a criteria instance... method. In This example search the result according to Criteria
Hibernate Criteria Like and Between Example
Hibernate Criteria Like and Between Example In this Example, We will discuss about hibernate criteria query, The interface.... In this example we create a criteria instance and implement the factory methods
Hibernate Creating criteria instance
Hibernate Creating criteria instance In this section you will learn about the creating of criteria instance in Hibernate. An instance of Criteria is created... for instantiating the Criteria. Example : An example is being given below
Hibernate Criteria Dynamic Association Fetching
Hibernate Criteria Dynamic Association Fetching In this tutorial you will learn about the dynamic association fetching in Criteria Query. In a Criteria Query...; this.lastname = lastname; } public String getFirstname() { return firstname
Hibernate Criteria restrictions Query Example
Hibernate Criteria restrictions Query Example In this Example, We will discuss about hibernate criteria query, The interface org.hibernate.Criteria... a criteria instance and implement the restrictions class method. These methods
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 Ordering Example
Hibernate Criteria Ordering Example In this tutorial you will learn about the Hibernate Criteria Projection Ordering. In Hibernate Criteria query to arrange... = orderedGoodsName; } public int getPriceOfGoods() { return priceOfGoods; } public
Hibernate Criteria isNotNull Example
Hibernate Criteria isNotNull Example In this Example, We will discuss about hibernate criteria query, The interface org.hibernate.criterion.Restrictions.... In this example we create a criteria instance and implement the factory methods
.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 MatchMode Exact Example
hibernate criteria MatchMode Exact Example In this Example, We will discuss about hibernate criteria query, In this example we create a criteria instance... the entire string to the pattern. Here is the simple Example code files
Hibernate Criteria Ordering Query Example
Hibernate Criteria Ordering Query Example In this Example, We will discuss about hibernate criteria query, The interface org.hibernate.Criteria is used to create the criterion for the search. In this example we create a criteria
Hibernate Criteria Projections
Hibernate Criteria Projections The Hibernate Criteria Projection contains all... = HibernateUtil.getSessionFactory().openSession(); Criteria criteria...()) { System.out.println(""); Object[] objects = (Object[]) iterator.next(); for (int i
hibernate criteria Distinct Result Example
hibernate criteria Distinct Result Example In this Example, We will discuss about hibernate criteria query, In this example we create a criteria instance... method. In This example search the result according to Criteria
hibernate criteria Distinct_Root_Entity
hibernate criteria Distinct_Root_Entity In this Tutorial, We will discuss about hibernate criteria query, In this example we create a criteria instance... method. In This example search the result according to Criteria
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 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 Order by
Hibernate Criteria API Order By The Order By API of hibernate criteria is used... query can be written in hibernate as follows Criteria criteria...(); An Example of Criteria order by is given below OrderByExample.java package
Hibernate Two Condition Criteria Example
Hibernate Two Condition Criteria Example In this Example, We will discuss about hibernate criteria query, The interface org.hibernate.criterion.Restrictions.... In this example we create a criteria instance and implement the factory methods
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 criteria Max Min Average Result Example
hibernate criteria Max Min Average Result Example In this Example, We will discuss about hibernate criteria query, The class... of Projection. In this example we create a criteria instance and implement
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