02、Hibernate开发步骤

1、创建Hibernate配置文件(hibernate.cfg.xml)

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    

    

    

root

root123

com.mysql.jdbc.Driver

jdbc:mysql:///hibernate5

 

org.hibernate.dialect.MySQLInnoDBDialect

 

true

 

true

 

update

    

    

    

    

    

 

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)

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    

        

            

            

            

        

        

            

        

        

            

        

        

            

        

    

 

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

答:我们只需要将设置为

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

答:插入的数据已经存在

你可能感兴趣的:(02、Hibernate开发步骤)