出错了,能解决的我就采纳+3

来源:1-11 通过Hibernate API编写访问数据库的代码

夜空中最亮de星

2016-11-15 21:47

http://img.mukewang.com/582b11ba0001629319020839.jpg

出错了,看看吧

写回答 关注

5回答

  • 夜空中最亮de星
    2016-11-16 17:26:31

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

        <property name="connection.username">root</property>

        <property name="connection.password">scz123</property>

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

       

        <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=utf-8</property>

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

       

        <property name="show_sql">true</property>

        <property name="format_sql">true</property>

        <property name="hbm2ddl.auto">create</property>

       

        <mapping resource="Students.hbm.xml"/>

        </session-factory>

    </hibernate-configuration>


  • 夜空中最亮de星
    2016-11-16 17:25:26

    import java.util.Date;


    import org.hibernate.Session;

    import org.hibernate.SessionFactory;

    import org.hibernate.Transaction;

    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

    import org.hibernate.cfg.Configuration;

    import org.hibernate.service.ServiceRegistry;

    import org.junit.After;

    import org.junit.Before;

    import org.junit.Test;



    //测试类

    public class StudentsTest {

    private SessionFactory sessionFactory;

    private Session session;

    private Transaction transaction;

    @Before

    public void init(){

    //创建配置对象

    Configuration config=new Configuration().configure();

    //创建服务注册对象

    ServiceRegistry serviceRegistry= new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();

    //创建会化工厂会话对象

    sessionFactory=config.buildSessionFactory(serviceRegistry);

    //会话对象

    session= sessionFactory.openSession();

    //开启事务

    transaction =session.beginTransaction();

    }

    @After

    public void destory(){

    transaction.commit();

    //提交事务

    session.close();//关闭会话

    sessionFactory.close();//关闭会话工厂

    }

    @Test

    public void testSaveStudents(){

    //生成学生对象

    Students s1 =new Students(1, "张三丰","男",new Date(),"武当山");

    session.save(s1);//保存对象进入数据库

    }


    }


  • 饮尽天下鸡汤
    2016-11-16 17:07:03

    看你左侧的错误提示,是初始化的时候类型转换时抛异常了 ClassCastException

    夜空中最亮d... 回复饮尽天下鸡汤

    贴了。

    2016-11-16 17:32:05

    共 3 条回复 >

  • 好差劲
    2016-11-16 10:28:22

    看不到

  • weibo_你知道罗菜花吗_0
    2016-11-15 21:51:06

    诶,还没回答我的问题诶


Hibernate初探之单表映射

Java持久化框架Hibernate入门教程,掌握Hibernate基本概念

74810 学习 · 793 问题

查看课程

相似问题