在我的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);
}
执行我的代码时,获取打印"为hibernate创建配置" 。但没有收到印刷品#34; Created"。也没有得到任何错误。我收到以下打印消息。
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
&GT;
如果有人知道,请告诉我。
答案 0 :(得分:0)
在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();
希望这可以帮助您解决问题