Java session.save不';t保存到数据库
以下是JUnit代码:Java session.save不';t保存到数据库,java,database,hibernate,Java,Database,Hibernate,以下是JUnit代码: package com.learn.hibernate.entities; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.Servi
package com.learn.hibernate.entities;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HibernateTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init() {
System.out.println("before");
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destory() {
System.out.println("after");
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testSessionFlush() {
News news = new News("Java2", "Sun2", new Date());
session.save(news);
System.out.println("end");
}
}
结果如下:
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
before
September 26, 2015 8:10:08 Afternoon org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
September 26, 2015 8:10:08 Afternoon org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.4.Final}
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
September 26, 2015 8:10:08 Afternoon org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/learn/hibernate/entities/News.hbm.xml
September 26, 2015 8:10:08 Afternoon org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:///hibernate5]
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
September 26, 2015 8:10:09 Afternoon org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
September 26, 2015 8:10:09 Afternoon org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
September 26, 2015 8:10:09 Afternoon org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
September 26, 2015 8:10:09 Afternoon org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate5.NEWS
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [date, author, id, title]
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
after
选择了JAVA工具选项:-javaagent:/usr/share/JAVA/jayatanaag.jar
之前
2015年9月26日8:10:08下午org.hibernate.annotations.common.Version
信息:HCANN000001:Hibernate Commons注释{4.0.2.Final}
2015年9月26日8:10:08下午org.hibernate.Version日志版本
信息:hh000412:Hibernate核心{4.2.4.Final}
2015年9月26日8:10:08下午org.hibernate.cfg.Environment
信息:HH000206:找不到hibernate.properties
2015年9月26日8:10:08下午org.hibernate.cfg.Environment buildBytecodeProvider
信息:HH000021:字节码提供程序名称:javassist
2015年9月26日8:10:08下午org.hibernate.cfg.Configuration
信息:HH000043:从资源配置:/hibernate.cfg.xml
2015年9月26日8:10:08下午org.hibernate.cfg.Configuration getConfigurationInputStream
信息:hh000040:配置资源:/hibernate.cfg.xml
2015年9月26日8:10:08下午org.hibernate.internal.util.xml.DTDEntityResolver解析实体
警告:HH000223:已识别过时的hibernate命名空间http://hibernate.sourceforge.net/. 使用名称空间http://www.hibernate.org/dtd/ 相反请参阅Hibernate 3.6迁移指南!
2015年9月26日8:10:08下午org.hibernate.cfg.Configuration addResource
信息:hh000221:从以下资源读取映射:com/learn/hibernate/entities/News.hbm.xml
2015年9月26日8:10:08下午org.hibernate.internal.util.xml.DTDEntityResolver解析实体
警告:HH000223:已识别过时的hibernate命名空间http://hibernate.sourceforge.net/. 使用名称空间http://www.hibernate.org/dtd/ 相反请参阅Hibernate 3.6迁移指南!
2015年9月26日8:10:08下午org.hibernate.cfg.Configuration doConfigure
信息:HH000041:配置的会话工厂:null
2015年9月26日8:10:08下午org.hibernate.service.jdbc.connections.internal.driverManager连接提供MPL配置
信息:HH000402:使用Hibernate内置连接池(不用于生产!)
2015年9月26日8:10:08下午org.hibernate.service.jdbc.connections.internal.driverManager连接提供MPL配置
信息:HH000115:休眠连接池大小:20
2015年9月26日8:10:08下午org.hibernate.service.jdbc.connections.internal.driverManager连接提供MPL配置
信息:HH000006:自动提交模式:错误
2015年9月26日8:10:08下午org.hibernate.service.jdbc.connections.internal.driverManager连接提供MPL配置
信息:HH000401:在URL[jdbc:mysql:///hibernate5]
2015年9月26日8:10:08下午org.hibernate.service.jdbc.connections.internal.driverManager连接提供MPL配置
信息:HH000046:连接属性:{user=root,password=**}
2015年9月26日8:10:09下午org.hibernate.dialogue.dialogue
信息:HH000400:使用方言:org.hibernate.dialogue.mysql5innodbdialogue
2015年9月26日8:10:09下午org.hibernate.engine.jdbc.internal.LobCreatorBuilder UseContexturationAllobCreation
信息:HH000423:禁用上下文LOB创建,因为JDBC驱动程序报告JDBC版本[3]小于4
2015年9月26日8:10:09下午org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
信息:HH000399:使用默认事务策略(直接JDBC事务)
2015年9月26日8:10:09下午org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory
信息:HH000397:使用ASTQueryTranslatorFactory
2015年9月26日8:10:09下午org.hibernate.tool.hbm2ddl.SchemaUpdate执行
信息:HH000228:正在运行hbm2ddl架构更新
2015年9月26日8:10:09下午org.hibernate.tool.hbm2ddl.SchemaUpdate执行
信息:HH000102:正在获取数据库元数据
2015年9月26日8:10:09下午org.hibernate.tool.hbm2ddl.SchemaUpdate执行
信息:HH000396:正在更新架构
2015年9月26日8:10:09下午org.hibernate.tool.hbm2ddl.TableMetadata
信息:HH000261:找到的表:hibernate5.NEWS
2015年9月26日8:10:09下午org.hibernate.tool.hbm2ddl.TableMetadata
信息:HH000037:列:[日期、作者、id、标题]
2015年9月26日8:10:09下午org.hibernate.tool.hbm2ddl.TableMetadata
信息:HH000108:外键:[]
2015年9月26日8:10:09下午org.hibernate.tool.hbm2ddl.TableMetadata
信息:HH000126:索引:[主要]
2015年9月26日8:10:09下午org.hibernate.tool.hbm2ddl.SchemaUpdate执行
信息:HH000232:架构更新完成
之后
正如您所看到的,虽然我要求在save()
之后输出end
,但结果没有包含它。而数据库没有更改,为什么?请尝试以下操作:
@Test
public void test() {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
try {
System.out.println("before");
News news = new News("Java2", "Sun2", new Date());
session.save(news);
System.out.println("after");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
System.out.println("end");
transaction.commit();
session.close();
sessionFactory.close();
}
}