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
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.
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.
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.
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.
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.
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.
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.
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With