Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate: how to select all rows in a table

I try to do something like Select * from LogEntry with Hibernate. insert works fine:

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

[...]
protected EntityManager manager;

protected final String tableName = "LogEntry";

public DatabaseImpl(DB_TYPE db) {
    this.db = db;
    if (entityManagerFactory != null && entityManagerFactory.isOpen()) {
        entityManagerFactory.close();
    }
    entityManagerFactory = Persistence.createEntityManagerFactory(db.getPersUnit());
    manager = entityManagerFactory.createEntityManager();
}
public void insert(LogEntry entry) {

    manager.getTransaction().begin();
    manager.persist(entry);
    manager.getTransaction().commit();
}

But when I try to get the inserted values using this method: public LogEntryList getAll() {

    manager.getTransaction().begin();

    Query query = manager.createNativeQuery("SELECT * FROM " + tableName + ";");
    ArrayList<LogEntry> entries = (ArrayList<LogEntry>) query.getResultList();
    manager.getTransaction().commit();

    return new LogEntryList(entries);
}

I always get the Exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to de.motza.entities.LogEntry

I know the problem is the casting of the Query result to the object, but I can't find anywhere how to cast the objects properly, or how to get more than one row from the table.

does anybody have any advice? If requested, I can post my persistence.xml and more code

like image 838
Ruth Avatar asked Jul 23 '15 09:07

Ruth


People also ask

How fetch all records from a table in Hibernate?

JPQL provides a simple and straightforward way to get all entities from a table. Our Hibernate session's createQuery() method receives a typed query string as the first argument and the entity's type as the second. We execute the query with a call to the getResultList() method which returns the results as a typed List.

How can I get multiple records in Hibernate?

List<student> stlist=HibernateUtil. getSession(). createQuery("from student"). list(); and you will get all records from Student table.

Which of the following method is used to retrieve records from table in Hibernate?

Query Object Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects.


2 Answers

Becuase you have used nativeQuery so you need to transfer result by using setResultTransormer method.

Query query = manager.createNativeQuery("SELECT * FROM " + tableName + ";");
query.setResultTransformer(Transformers.aliasToBean(LogEntry.class))
ArrayList<LogEntry> entries = (ArrayList<LogEntry>) query.getResultList();
like image 136
Sunil Avatar answered Sep 22 '22 12:09

Sunil


For starters, you should try to take advantage of HQL, Hibernate Query Language. In the example you gave above, you are trying to execute a native SQL query. The reason you are getting the ClassCastException is that the native query is circumventing the framework and returning raw Objects instead of the type you want.

Try using this code instead for your SELECT *:

String hql = "from LogEntry";
Session session = entityManagerFactory.openSession();
Query query = session.createQuery(hql);
List<LogEntry> logEntries = query.list();      // no ClassCastException here
like image 38
Tim Biegeleisen Avatar answered Sep 21 '22 12:09

Tim Biegeleisen