java - hibernate: how to select all rows in a table -
i try select * logentry
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 try inserted values using method: public logentrylist getall() {
manager.gettransaction().begin(); query query = manager.createnativequery("select * " + tablename + ";"); arraylist<logentry> entries = (arraylist<logentry>) query.getresultlist(); manager.gettransaction().commit(); return new logentrylist(entries); }
i exception: java.lang.classcastexception: [ljava.lang.object; cannot cast de.motza.entities.logentry
i know problem casting of query result object, can't find anywhere how cast objects properly, or how more 1 row table.
does have advice? if requested, can post persistence.xml , more code
becuase have used nativequery need transfer result using setresulttransormer
method.
query query = manager.createnativequery("select * " + tablename + ";"); query.setresulttransformer(transformers.aliastobean(logentry.class)) arraylist<logentry> entries = (arraylist<logentry>) query.getresultlist();
Comments
Post a Comment