2016-06-01 6 views
0

hibernate 5.1とmysqlデータベースを使用して簡単なホステル管理システムを作成しようとしています。私は次のコードの助けを借りて私のテーブルHostelHomeからテーブル全体(すべての行)を表示することができません。私はテーブル全体から1行だけ表示することができます。これは私が望むものではありません。hibernate 5.1.xでテーブル全体を表示できません

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

     } 

私の休止状態の設定ファイルがある

<?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> 

私の出力は、次のとおりです。

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

手動で設定ファイルの場所を指定する必要がありますように... – Andremoniy

+0

設定ファイルは問題ではありません見える、それが正常に動作している....私はちょうどあなたがそれを見ることができるように休止状態でテーブル全体を表示しますテーブルから1行しか表示されない – nitishpisal

+0

動作しません... JNDI名が誤って設定されているという例外を見てください。 – Andremoniy

答えて

0

何あなたはすべてのHostelHomeだけではない照会

:-)あなたが起こることを期待してい1つ(キー== 1のもの)。このオブジェクトは正しく返されます。あなたはあなたが求めたものを手に入れます。

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

を試してみて、結果のリストを反復処理します。

はHibernateセッションのドキュメントと内に記述クエリー機能を参照してください:https://docs.jboss.org/hibernate/orm/5.1/javadocs/org/hibernate/Session.html

0

私はこれをしなかったし、それが魅力のように働きました!

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("----------------------"); 
       }  
関連する問題