Hibernate.cfg.xml如下
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useSSL=false</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">19981008</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">4</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!--<property name="current_session_context_class">thread</property>-->
<!--以格式良好的方式显示SQL语句-->
<property name="format_sql">true</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="entities/ProductEntity.hbm.xml"/>
<mapping class="entities.ProductEntity"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
AddProduct.java代码如下
public class AddProduct {
public static void main(String[] args){
Session session = null;
ProductEntity productEntity = new ProductEntity();
productEntity.setId(1001);
productEntity.setName("Java web从入门到精通");
productEntity.setPrice(79.9);
productEntity.setFactory("曹威");
productEntity.setRemark(2);
try{
session = HibernateUtil.getSession();
session.beginTransaction(); //开启事务
session.save(productEntity); //执行数据库添加操作
session.getTransaction(); //事务提交
System.out.println("添加成功");
}catch (Exception e){
session.getTransaction().rollback();//事务回滚
System.out.println("数据添加失败");
e.printStackTrace();
}
}
}
运行以后控制台输出如下
七月 13, 2019 3:47:56 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.3.Final}
七月 13, 2019 3:47:57 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
七月 13, 2019 3:47:58 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
七月 13, 2019 3:47:58 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test?useSSL=false]
七月 13, 2019 3:47:58 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
七月 13, 2019 3:47:58 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
七月 13, 2019 3:47:58 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 4 (min=1)
七月 13, 2019 3:47:58 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
七月 13, 2019 3:47:59 下午 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@3a095ec0] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
七月 13, 2019 3:47:59 下午 org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
但是数据库并未插入数据
请求解答