EndlessZh 2018-04-23 09:19 采纳率: 33.3%
浏览 836
已采纳

慕课网案例 新手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条回答 默认 最新

  • jc_cong 2018-04-24 08:49
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器
  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)