【问题标题】:In my hibernate application,not getting session but not throwing any error在我的休眠应用程序中,没有获得会话但没有抛出任何错误
【发布时间】:2015-07-01 13:34:54
【问题描述】:

在我的 java hibernate(使用注释)应用程序中,我正在尝试使用以下代码将数据更新到数据库中。

     try {
          system.out.println("Creating configuration for hibernate");
          sessionFactory = new annotationConfiguration().configure().buildSessionFactory();
          Session session =getSessionFactory().getCurrentSession();
          system.out.println("Created");
          transaction = session.beginTransaction();
          session.saveOrUpdate(DATA);
          transaction.commit();
         }catch(Exception e) { 
          system.out.println(e);
         }

当执行我的代码时,得到 print "Creating configuration for hibernate" 。但没有收到打印“已创建”。也没有得到任何错误。我收到以下打印消息。

 Apr 2015 06:15:41,851  INFO SettingsFactory:134 - Automatic flush during beforeCompletion(): disabled
23 Apr 2015 06:15:41,851  INFO SettingsFactory:138 - Automatic session close at end of transaction: disabled 23 Apr 2015 06:15:41,851  INFO SettingsFactory:145 - JDBC batch size: 15
23 Apr 2015 06:15:41,851  INFO SettingsFactory:148 - JDBC batch updates for versioned data: disabled
23 Apr 2015 06:15:41,852  INFO SettingsFactory:153 - Scrollable result sets: enabled
23 Apr 2015 06:15:41,852 DEBUG SettingsFactory:157 - Wrap result sets: disabled
23 Apr 2015 06:15:41,853  INFO SettingsFactory:161 - JDBC3 getGeneratedKeys(): enabled
23 Apr 2015 06:15:41,853  INFO SettingsFactory:169 - Connection release mode: auto
23 Apr 2015 06:15:41,853  INFO SettingsFactory:193 - Maximum outer join fetch depth: 2
23 Apr 2015 06:15:41,854  INFO SettingsFactory:196 - Default batch fetch size: 1
23 Apr 2015 06:15:41,854  INFO SettingsFactory:200 - Generate SQL with comments: disabled
23 Apr 2015 06:15:41,854  INFO SettingsFactory:204 - Order SQL updates by primary key: disabled
23 Apr 2015 06:15:41,854  INFO SettingsFactory:369 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
23 Apr 2015 06:15:41,856  INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
23 Apr 2015 06:15:41,856  INFO SettingsFactory:212 - Query language substitutions: {}
23 Apr 2015 06:15:41,857  INFO SettingsFactory:217 - JPA-QL strict compliance: disabled
23 Apr 2015 06:15:41,857  INFO SettingsFactory:222 - Second-level cache: enabled
23 Apr 2015 06:15:41,857  INFO SettingsFactory:226 - Query cache: disabled
23 Apr 2015 06:15:41,857  INFO SettingsFactory:356 - Cache provider: org.hibernate.cache.NoCacheProvider
23 Apr 2015 06:15:41,858  INFO SettingsFactory:241 - Optimize cache for minimal puts: disabled
23 Apr 2015 06:15:41,858  INFO SettingsFactory:250 - Structured second-level cache entries: disabled
23 Apr 2015 06:15:41,859 DEBUG SQLExceptionConverterFactory:52 - Using dialect defined converter
23 Apr 2015 06:15:41,861  INFO SettingsFactory:270 - Echoing all SQL to stdout
23 Apr 2015 06:15:41,862  INFO SettingsFactory:277 - Statistics: disabled
23 Apr 2015 06:15:41,862  INFO SettingsFactory:281 - Deleted entity synthetic identifier rollback: disabled
23 Apr 2015 06:15:41,863  INFO SettingsFactory:296 - Default entity-mode: pojo
23 Apr 2015 06:15:41,884  INFO SessionFactoryImpl:161 - building session factory
23 Apr 2015 06:15:41,885 DEBUG SessionFactoryImpl:173 - Session factory constructed with filter configurations : {}

配置文件(hibernate.cfg.xml)如下。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
        <session-factory>
                <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                <property name="hibernate.connection.password"></property>
                <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Test</property>
                <property name="hibernate.connection.username">root</property>
                <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
                <property name="connection.pool_size">1</property>
                <property name="show_sql">true</property>
                <property name="hibernate.hbm2ddl.auto">create</property>
                <mapping class="TestPojo"/>
        </session-factory>
</hibernate-configuration

>

如果有人知道,请告诉我。

【问题讨论】:

    标签: java hibernate hibernate-mapping hibernate-annotations


    【解决方案1】:

    在您的 hibernate.cfg.cml 中添加以下配置属性

    <property name="hibernate.current_session_context_class">thread</property>
    

    通过这样做,Hibernate 为“当前”会话的范围提供自定义策略意味着您可以使用 sessionFactory.getCurrentSession(); 获取会话对象

    您可以在 link 上的 Hibernate 文档中找到有关上下文会话的更多信息

    现在改变你的代码如下

    sessionFactory = new annotationConfiguration().configure().buildSessionFactory();
              Session session =sessionFactory.getCurrentSession();   
    

    希望这能帮助您解决问题

    【讨论】:

    • 感谢您的回复。我试过了。但我仍然面临同样的问题
    • 不,我没有得到。即使我没有得到会话工厂对象。
    • 好的,请问您是在创建独立的 java 应用程序吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多