2016-06-01 3 views
0

Ich versuche, ein einfaches Hostel-Management-System mit Hibernate 5.1 und Mysql-Datenbank zu erstellen. Ich kann nicht die gesamte Tabelle (alle Zeilen) von meinem Tisch HostelHome mit Hilfe des folgenden Stück Code anzeigen. Ich kann nur eine einzelne Zeile aus der gesamten Tabelle anzeigen, die nicht das ist, was ich möchte.Nicht in der Lage, die gesamte Tabelle in Hibernate 5.1.x anzuzeigen

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(); 
      } 

     } 

Meine Hibernate-Konfigurationsdatei ist

<?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 the 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 the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 
    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property> 
    <!-- Drop and re-create the database schema on startup create deletes all prev records/ update updates table without deleting it--> 
    <property name="hbm2ddl.auto">update</property> 
    <mapping class="HostelHibernate.HostelHome"/> 
</session-factory> 
</hibernate-configuration> 

Meine Ausgabe ist:

org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [] 
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:124) 
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.bind(JndiServiceImpl.java:140) 
    at org.hibernate.internal.SessionFactoryRegistry.addSessionFactory(SessionFactoryRegistry.java:88) 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:522) 
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) 
    at HostelHibernate.HostelHome.Display(HostelHome.java:75) 
    at HostelHibernate.HostelHome.main(HostelHome.java:62) 
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial 
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) 
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313) 
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350) 
    at javax.naming.InitialContext.getNameParser(InitialContext.java:505) 
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:118) 
    ... 8 more 

Hibernate: 
    select 
     hostelhome0_.room_no as room_no1_0_0_, 
     hostelhome0_.vacancy as vacancy2_0_0_ 
    from 
     hostel hostelhome0_ 
    where 
     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 a total of 0 entities and 0 collections); 
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections) 
} 
+0

Sieht aus wie Sie manuell auf Position Konfigurationsdatei angeben ... – Andremoniy

+0

config-Datei ist kein Problem, es funktioniert gut .... Ich möchte nur in Hibernate gesamte Tabelle angezeigt werden, wie Sie es sehen können nur eine Zeile aus der Tabelle angezeigt – nitishpisal

+0

Es funktioniert nicht ... schau auf die Ausnahme, es heißt, dass Sie JNDI-Namen falsch konfiguriert haben. – Andremoniy

Antwort

0

Was tun Sie :-)

Sie passieren erwarten Abfrage nicht alle, sondern nur HostelHome eine (die mit Schlüssel == 1). Dieses Objekt wird ordnungsgemäß zurückgegeben. Sie bekommen, wonach Sie gefragt haben.

Versuchen

List<HostelHome> hostels = (List<HostelHome>)session.createQuery("select HostelHome").list(); 

und über die resultierende Liste iterieren.

Siehe die Sitzung Dokumentation Hibernate und die Abfragefunktionen beschrieben in: https://docs.jboss.org/hibernate/orm/5.1/javadocs/org/hibernate/Session.html

0

ich das tat und es funktionierte wie ein Zauber!

Query qry = session.createQuery("from HostelHome p"); 

       List l =qry.list(); 
       System.out.println("Hostel Records : "+l.size()); 
       Iterator it = l.iterator(); 
       System.out.println("Room No Vacancy"); 
       while(it.hasNext()) 
       { 
        Object o = (Object)it.next(); 
        HostelHome p = (HostelHome)o; 
        System.out.print(p.getRoom_no()+"   "); 
        System.out.println(+p.getVacancy()); 
        System.out.println("----------------------"); 
       }  
Verwandte Themen