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