简体   繁体   中英

In my hibernate application,not getting session but not throwing any error

In my java hibernate(using annotations) applications, i am trying to update data in to the data base using the below code.

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

when execute my code, getting print "Creating configuration for hibernate" . but not receiving print "Created". Also not getting any error. i am getting the following print messages.

 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 : {}

configuration file(hibernate.cfg.xml) is given below.

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

>

Please let me know if any one knows .

Add below config property in your hibernate.cfg.cml

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

By doing so Hibernate Supply a custom strategy for the scoping of the "current" Session means you can get session object using sessionFactory.getCurrentSession();

You can find more information on Contextual sessions in Hibernate Documentation on this link

Now change your code as below

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

hope this will help you resolve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM