Hibernate学习Hello World与及hibernate.dialect not set

1、新建一个Dynamic Web Project,导入需要的开发jar包(Hibernate下载的开发文件夹下的\lib\required下的jar包与及数据库驱动jar包),本例使用的数据库Mysql 5.6.

                             

2、在src建立hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!--JDBC驱动程序类-->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/MyDB?useUnicode=true&amp;characterEncoding=utf-8</property><!--配置使用的MySQL数据库连接的url,以及使用的编码为UTF-8->
        <property name="hibernate.connection.username">root</property><!--配置使用的MySQL数据库的用户名-->
        <property name="hibernate.connection.password">123456</property><!--配置使用的MySQL数据库的密码-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property><!--配置Hibernate使用的SQL方言-->

        <property name="show_sql">true</property><!--配置Hibernate执行时显示所使用的SQL语句-->
    </session-factory>
</hibernate-configuration>

3、在src建立一个包com.hibernate01.test,在包下建立Person.java

public class Person {
    private Integer personID;
    private String personName;
    public Integer getPersonID() {
        return personID;
    }
    public void setPersonID(Integer personID) {
        this.personID = personID;
    }
    public String getPersonName() {
        return personName;
    }
    public void setPersonName(String personName) {
        this.personName = personName;
    }
}

4、在com.hibernate01.test包下建立Person.hbm.xml,可以运用Hibernate工具进行建立

<?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 2015-8-17 14:38:18 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.hibernate01.test.Person" table="PERSON">
        <id name="personID" type="java.lang.Integer">
            <column name="PERSONID" />
            <generator class="assigned" /><!--指定主键生成方式由用户自己定义-->
        </id>
        <property name="personName" type="java.lang.String">
            <column name="PERSONNAME" />
        </property>
    </class>
</hibernate-mapping>

5、将Person.hbm.xml添加到Hibernate.cfg.xml下

<mapping resource="com/hibernate01/test/Person.hbm.xml"/>

6、在com.hiberante01.test下编写HibernateUtil.java

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();
    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure().buildSessionFactory(
                new StandardServiceRegistryBuilder().build() );
        }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

7、在com.hiberante01.test下编写Test1.java,它是一个JUnit测试类

public class Test1 {
    @Test
    public void test() {
        Session session=HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Person person=new Person();
        person.setPersonID(4);
        person.setPersonName("张三");
        session.save(person);
        session.getTransaction().commit();
        HibernateUtil.getSessionFactory().close(); 
    }
}

程序抛出异常:Initial SessionFactory creation failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
但是hibernate.dialect又配置了,原来问题出在了HibernateUtil.java里,于是重写HibernateUtil.java

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();
    private static SessionFactory buildSessionFactory() {
        try {
            Configuration cfg = new Configuration().configure(); 
            StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()  
                                .applySettings(cfg.getProperties()).build();  
             return   cfg.buildSessionFactory(serviceRegistry);
        }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        } 
    }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

重新运行Test1中的Test结果如图:

查询数据库结果如图:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值