1、創建Hibernate配置文件(hibernate.cfg.xml) <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configura ...
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.xml中 class中的路徑出現錯誤 沒有找到'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 答:插入的數據已經存在 |