02、Hibernate开发步骤

1、创建Hibernate配置文件(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>

    

     <!-- 配置连接数据库的基本信息 -->

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

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

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

<property name="connection.url">jdbc:mysql:///hibernate5</property>

 

<!-- 配置 hibernate 的基本信息 -->

<!-- hibernate 指定数据库所使用的 SQL 方言 -->

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

 

<!-- 指定程序运行时是否在控制台输出 SQL 语句 -->

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

 

<!-- 指定是否对输出 SQL 语句进行格式化 -->

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

 

<!-- 指定程序运行时是否在数据库自动生成数据表 -->

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

    

     <!-- 指定关联的 .hbm.xml 文件 -->

     <mapping resource="csah/com/cnblogs/www/News.hbm.xml" />

    

    </session-factory>

</hibernate-configuration>

 

1)问题:生成cfg.xml时候弹出右下角内容,然后按finish一直无反应怎么办??

答:hibernate版本问题,就是第三行Hibernate version那个选择低一点的版本 我看Jar包是4.3.x的,我选了4.3的就OK

2、创建持久化类

package csah.com.cnblogs.www;

 

import java.util.Date;

 

public class News {

private Integer id;

private String title;

private String author;

private Date date;

public Date getDate() {

return date;

}

public News(String title, String author, Date date) {

super();

this.title = title;

this.author = author;

this.date = date;

}

public void setDate(Date date) {

this.date = date;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public News() {

}

@Override

public String toString() {

return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";

}

}

 

3、创建对象-关系映射文件(*.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 2020-4-25 2:28:16 by Hibernate Tools 3.5.0.Final -->

<hibernate-mapping package="csah.com.cnblogs.www">

    <class name="News" table="NEWS">

        <id name="id" type="java.lang.Integer">

            <column name="ID" />

            <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->

            <generator class="native" />

        </id>

        <property name="title" type="java.lang.String">

            <column name="TITLE" />

        </property>

        <property name="author" type="java.lang.String">

            <column name="AUTHOR" />

        </property>

        <property name="date" type="java.util.Date">

            <column name="DATE" />

        </property>

    </class>

</hibernate-mapping>

 

1)问题:ids for this class must be manually assigned before calling save(): csah.com.cnblogs.www.News

答:我们只需要将<generator class="assigned " />设置为<generator class="native" />

4、通过Hibernate API编写访问数据库代码

package csah.com.cnblogs.www;

 

import static org.junit.jupiter.api.Assertions.*;

 

import java.sql.Date;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.hibernate.service.ServiceRegistryBuilder;

import org.junit.jupiter.api.Test;

 

import csah.com.cnblogs.www.*;

 

class HibernateTest {

 

@Test

public void test() {

 

System.out.println("test1...");

 

//1. 创建一个 SessionFactory 对象

SessionFactory sessionFactory = null;

System.out.println("test2...");

 

//1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息

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

System.out.println("test3...");

 

//4.0 之前这样创建

// sessionFactory = configuration.buildSessionFactory();

 

//2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象

//hibernate 的任何配置和服务都需要在该对象中注册后才能有效.

ServiceRegistry serviceRegistry =

new ServiceRegistryBuilder().applySettings(configuration.getProperties())

                           .buildServiceRegistry();

System.out.println("test4...");

 

//3).

sessionFactory = configuration.buildSessionFactory(serviceRegistry);

System.out.println("test5...");

 

//2. 创建一个 Session 对象

Session session = sessionFactory.openSession();

System.out.println("test6...");

 

//3. 开启事务

Transaction transaction = session.beginTransaction();

System.out.println("test7...");

 

//4. 执行保存操作

News news = new News("java", "ATGUIGU", new Date(new java.util.Date().getTime()));

System.out.println("test8...");

 

session.save(news);

System.out.println("test9...");

 

//5. 提交事务

transaction.commit();

System.out.println("test10...");

 

//6. 关闭 Session

session.close();

System.out.println("test11...");

 

//7. 关闭 SessionFactory 对象

sessionFactory.close();

System.out.println("test12...");

}

 

}

   注意:上面的多个system.out.println()可以测试代码运行到哪一部分中断,如如果System.out.println("test9...");没有输出,那么我们只要找session.save(news)的问题即可。

1)问题:org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/cnblogs/com/CSAH/News.hbm.xml

java.lang.ClassNotFoundException: com.nblogs.com.CSAH.News

答:在News.hbm.xmlclass中的路径出现错误 没有找到'com.nblogs.com.CSAH.News'

 

2)问题:org.hibernate.MappingNotFoundException: resource: com/cnblogs/com/CSAH/News.hbm.xml not found

答:指定关联的 .hbm.xml 文件路径没有找到

 

3)问题:Duplicate entry 'java' for key 2

答:插入的数据已经存在

posted @ 2020-04-25 02:57  HOsystem  阅读(393)  评论(0编辑  收藏  举报