搞了两天了,一直卡在这。百度了好多也没找到解决方案,希望大家帮我看看。
编程环境
- Eclipse Neon.3 Release (4.6.3)
- hibernate(5.3.0)
- JDBC8.0.11
- MySQL5.6
报错信息
Apr 23, 2018 5:06:42 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.0.CR1}
Apr 23, 2018 5:06:42 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Apr 23, 2018 5:06:42 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.2.Final}
Apr 23, 2018 5:06:42 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Apr 23, 2018 5:06:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Apr 23, 2018 5:06:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?serverTimezone=UTC]
Apr 23, 2018 5:06:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Apr 23, 2018 5:06:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Apr 23, 2018 5:06:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Apr 23, 2018 5:06:43 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Apr 23, 2018 5:06:44 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@24be2d9c] 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.
项目内容
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 1.设置四本一言 -->
<!-- 驱动,连接URL,用户名,密码,方言 -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="current_session_context_class">thread</property>
<!-- 2.全局配置信息 -->
<!--是否显示SQL语句 -->
<property name="show_sql">true</property>
<!--是否格式化SQL语句 -->
<property name="format_sql">true</property>
<!--执行DDL的类别:
create:每次都删除新建
update:存在就修改,不存在就新建 -->
<property name="hbm2ddl.auto">update</property>
<!--3.加载配置文件 -->
<!--基于xml映射文件: 映射文件加载。路径 -->
<mapping resource="Student.hbm.xml"/>
<!-- <mapping class="Students"/> -->
</session-factory>
</hibernate-configuration>
Student.java
import java.util.Date;
public class Student {
private int sId;
private String sName;
private String gender;
private Date birthday;
private String address;
public Student() {
}
public Student(int sId, String sName, String gender, Date birthday, String address) {
this.sId = sId;
this.sName = sName;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
@Override
public String toString() {
return "Student [sId=" + sId + ", sName=" + sName + ", gender=" + gender + ", birthday=" + birthday
+ ", address=" + address + "]";
}
public int getsId() {
return sId;
}
public void setsId(int sId) {
this.sId = sId;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Student.test.java
import java.util.Date;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StudentTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void Init()
{
// 1.加载配置文件
Configuration configuration = new Configuration().configure();
// 2.创建Session工厂
SessionFactory factory = configuration.buildSessionFactory();
// 3.创建Session对象
Session session = factory.openSession();
// 4.开启事务
transaction = session.beginTransaction();
}
@Test
public void testSaveStudents()
{
//生成对象
Student student=new Student(1,"张三丰","男",new Date(),"武当山");
//保存对象进数据库
session.save(student);
}
@After
public void destory()
{
//提交事务
transaction.commit();
//关闭session会话
session.close();
//关闭sessionFactory会话工厂
sessionFactory.close();
}
}
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 21, 2018 5:32:53 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="Student" table="STUDENT">
<id name="sId" type="int" access="field">
<column name="SID" />
<generator class="assigned" />
</id>
<property name="sName" type="java.lang.String" access="field">
<column name="SNAME" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER" />
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>