Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


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


Advertisement
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
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
Advertisements
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 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 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 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 Queries - Hibernate
Hibernate Criteria Queries  Can I use the Hibernate Criteria Query...().buildSessionFactory(); session = sessionFactory.openSession(); Criteria crit... Configuration(); // configuring hibernate SessionFactory
Hibernate Criteria Or
In this section we are going to discuss Hibernate Criteria Or with example
Hibernate Criteria
In this tutorial we are going to discuss Hibernate Criteria with example
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 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 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 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 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 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 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
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 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 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
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 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 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
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 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
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 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 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
Advertisements
 

 

 

DMCA.com