java - Not able to display entire table in hibernate 5.1.x -
i trying create simple hostel management system using hibernate 5.1 , mysql database. not able display entire table (all rows ) table hostelhome of following piece of code. able display single row entire table not want.
public static void display(){ sessionfactory sessionfactory = new configuration().configure().buildsessionfactory(); session session = sessionfactory.opensession(); try{ hostelhome h1 = (hostelhome)session.get(hostelhome.class, new integer(1)); system.out.print("room number: "+h1.getroom_no()+" "); system.out.print("vacancy: "+h1.getvacancy()+" "); session.close(); }catch(exception e){ e.printstacktrace(); } }
my hibernate config file
<?xml version="1.0"?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name=""> <!-- database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- jdbc connection pool (use built-in) --> <property name="connection.pool_size">1</property> <!-- sql dialect --> <property name="dialect">org.hibernate.dialect.mysqldialect</property> <!-- enable hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- disable second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.nocacheprovider</property> <!-- echo executed sql stdout --> <property name="show_sql">true</property> <!-- drop , re-create database schema on startup create deletes prev records/ update updates table without deleting it--> <property name="hbm2ddl.auto">update</property> <mapping class="hostelhibernate.hostelhome"/> </session-factory> </hibernate-configuration>
my output :
org.hibernate.engine.jndi.jndiexception: error parsing jndi name [] @ org.hibernate.engine.jndi.internal.jndiserviceimpl.parsename(jndiserviceimpl.java:124) @ org.hibernate.engine.jndi.internal.jndiserviceimpl.bind(jndiserviceimpl.java:140) @ org.hibernate.internal.sessionfactoryregistry.addsessionfactory(sessionfactoryregistry.java:88) @ org.hibernate.internal.sessionfactoryimpl.<init>(sessionfactoryimpl.java:522) @ org.hibernate.boot.internal.sessionfactorybuilderimpl.build(sessionfactorybuilderimpl.java:465) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:708) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:724) @ hostelhibernate.hostelhome.display(hostelhome.java:75) @ hostelhibernate.hostelhome.main(hostelhome.java:62) caused by: javax.naming.noinitialcontextexception: need specify class name in environment or system property, or applet parameter, or in application resource file: java.naming.factory.initial @ javax.naming.spi.namingmanager.getinitialcontext(namingmanager.java:662) @ javax.naming.initialcontext.getdefaultinitctx(initialcontext.java:313) @ javax.naming.initialcontext.geturlordefaultinitctx(initialcontext.java:350) @ javax.naming.initialcontext.getnameparser(initialcontext.java:505) @ org.hibernate.engine.jndi.internal.jndiserviceimpl.parsename(jndiserviceimpl.java:118) ... 8 more hibernate: select hostelhome0_.room_no room_no1_0_0_, hostelhome0_.vacancy vacancy2_0_0_ hostel hostelhome0_ hostelhome0_.room_no=? room number: 1 vacancy: 2 jun 01, 2016 5:33:27 pm org.hibernate.engine.internal.statisticalloggingsessioneventlistener end info: session metrics { 763136 nanoseconds spent acquiring 1 jdbc connections; 0 nanoseconds spent releasing 0 jdbc connections; 18852614 nanoseconds spent preparing 1 jdbc statements; 967203 nanoseconds spent executing 1 jdbc statements; 0 nanoseconds spent executing 0 jdbc batches; 0 nanoseconds spent performing 0 l2c puts; 0 nanoseconds spent performing 0 l2c hits; 0 nanoseconds spent performing 0 l2c misses; 0 nanoseconds spent executing 0 flushes (flushing total of 0 entities , 0 collections); 0 nanoseconds spent executing 0 partial-flushes (flushing total of 0 entities , 0 collections) }
what expect happen :-)
you query not hostelhome
1 (the 1 key == 1). object returned. asked for.
try
list<hostelhome> hostels = (list<hostelhome>)session.createquery("select hostelhome").list();
and iterate on resulting list.
see hibernate session documentation , query functions described within: https://docs.jboss.org/hibernate/orm/5.1/javadocs/org/hibernate/session.html