我正在尝试使用hibernate 5.1和Mysql数据库创建一个简单的旅馆管理系统。我无法在下面的代码帮助下显示我的表HostelHome中的整个表(所有行)。我只能显示整个表中的一行,这不是我想要的。

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

最佳答案

您期望发生什么:-)

您查询的不是所有HostelHome,而是查询一个(键== 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

关于java - 无法在 hibernate 5.1.x中显示整个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37579195/

10-11 22:40
查看更多