Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate get List from database

Tags:

java

hibernate

In the following code I am trying to get a List of Products which contains all the products in the database:

public List<Products> getAllProducts() throws Exception{
    try{
     List<Products> products ;
    org.hibernate.Transaction tx = session.beginTransaction();
    products = session.createSQLQuery("SELECT * FROM Products").list();
    if(products.size() > 0)
    {
        return products;
    }
    return null;  
    }
    catch(Exception e)
    {
        throw e;
    }
}

however this exception is thrown:

[Ljava.lang.Object; cannot be cast to mediatek.Products 
like image 243
erbsoftware Avatar asked Jan 20 '13 10:01

erbsoftware


People also ask

How can we retrieve data from database using list in Hibernate?

Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.

How Hibernate stores list values?

The <key> element is the column in the CERTIFICATE table that holds the foreign key to the parent object i.e. table EMPLOYEE. The <list-index> element is used to keep the position of the element and map with the index column in the collection table. The index of the persistent list starts at zero.

What is setParameter in Hibernate?

setParameter. Bind a value to a named query parameter. The Hibernate type of the parameter is first detected via the usage/position in the query and if not sufficient secondly guessed from the class of the given object.

How do you pass a list of SQL queries in Java?

and then use setParameterList("ids", list) passing your list. Hibernate will do all the expansion for you! Show activity on this post. All you have to do is build the comma separated list of items and insert it in the string.

How do I map a list in hibernate?

Define Hibernate Mapping File Let us develop our mapping file, which instructs Hibernate how to map the defined classes to the database tables. The <list> element will be used to define the rule for List collection used. The index of list is always of type integer and is mapped using the <list-index> element.

How to get an entity from the database in hibernate?

In Hibernate, an entity (or a single record) can be obtained from the database using the following Session interface methods: Session.get (): This method returns a persistence object of the given class with the given identifier.

How to use Hibernate to find the object with ID?

In addition to the ID, hibernate also needs to know which class or entity name to use to find the object with that ID. After the load() method returns, we need to cast the returned object to suitable type of class to further use it. It’s all what load() method need from us to work it correctly.

How to insert data into hibernate from MySQL database?

Yes, To read the data we must have to insert the data into the database, here is a separate example to insert data into hibernate. Add dependency for Hibernate and MySQL connector in the project (pom.xml) Create Hibernate configuration file (hibernate.cfg.xml) Configure Session Factory and Session and start the transections


1 Answers

In Hibernate 5 the session.createCriteria methods are deprecated. You will need to use a CriteriaBuilder and query from there to get a generic list of Products instead of just List.

Imports

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;

Code

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Products> criteria = builder.createQuery(Products.class);
criteria.from(Products.class);
List<Products> products = session.createQuery(criteria).getResultList();
like image 112
svarog Avatar answered Sep 24 '22 08:09

svarog