慕课网案例 新手Hibernate配置问题,单表操作

搞了两天了,一直卡在这。百度了好多也没找到解决方案,希望大家帮我看看。
编程环境

  • 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>

Navicat中显示,仅生成了表。
图片说明

7个回答

在Student.hbm.xml中,id和sName的配置中,你使用了access="filed",表示的是使用po类的字段,但是你后面有配置了<column ...../>估计是两者之间有冲突

stack_queue
EndlessZh 回复jc_cong: 谢谢,请问应该把access="field"去掉吗?我刚试了下把它去掉仍然报错
一年多之前 回复
jc_cong
jc_cong field
一年多之前 回复

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
INFO: HHH000206: hibernate.properties not found
hibernate 的某个属性没找到吧。。

将transaction.commit();放到session.save(student);下面试试。

stack_queue
EndlessZh 好像没什么变化?
一年多之前 回复

刷新一下试试session.fulsh()

stack_queue
EndlessZh 在commit之前手动flush()了,报错没变
一年多之前 回复

图片说明

hi__study
hi__study 在studentTest.java的第57行的代码是?
一年多之前 回复

Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();

配置有问题,仔细检查一遍,这种问题基本上就可以解决。

stack_queue
EndlessZh 检查两天了,都是按着百度一条一条配置的
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!