gpt4 book ai didi

java - 在我的 hibernate 应用程序中,没有获取 session 但没有抛出任何错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:57 25 4
gpt4 key购买 nike

在我的 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);
}

当执行我的代码时,得到打印“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

>

如果有人知道请告诉我。

最佳答案

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

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

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

获取 session 对象

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

现在更改您的代码如下

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

希望这能帮助您解决问题

关于java - 在我的 hibernate 应用程序中,没有获取 session 但没有抛出任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29822213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com