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
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.
List<student> stlist=HibernateUtil. getSession(). createQuery("from student"). list(); and you will get all records from Student table.
Query Object Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects.
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();
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 Object
s 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
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