自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Hibernate創(chuàng)建和持久化Product

開發(fā) 后端
這里介紹Hibernate創(chuàng)建和持久化Product,我們用Hibernate創(chuàng)建一個Product,然后將其持久化;我們將搜索并加載一個已經(jīng)持久化Product,并確保其可以使用;我們將會更新和刪除Product。

在向大家詳細(xì)介紹持久化Product之前,首先讓大家了解下Hibernate創(chuàng)建一個Product,然后全面介紹。

在我們假想的應(yīng)用程序中,基本的使用模式非常簡單:我們用Hibernate創(chuàng)建一個Product,然后將其持久化(或者換句話說,保存它);我們將搜索并加載一個已經(jīng)持久化Product,并確保其可以使用;我們將會更新和刪除Product。

Hibernate創(chuàng)建和持久化Product

現(xiàn)在我們終于用到Hibernate了。使用的場景非常簡單:
1. Hibernate創(chuàng)建一個有效的Product。
2. 在應(yīng)用程序啟動時使用net.sf.Hibernate.cfg.Configuration獲取net.sf.Hibernate.SessionFactory。
3. 通過調(diào)用SessionFactory#openSession(),打開net.sf.Hibernate.Session。
4. 保存Product,關(guān)閉Session。

正如我們所看到的,這里沒有提到JDBC、SQL或任何類似的東西。非常令人振奮!下面的示例遵循了上面提到的步驟:

  1. package test;  
  2.  
  3. import net.sf.hibernate.Session;  
  4. import net.sf.hibernate.SessionFactory;  
  5. import net.sf.hibernate.Transaction;  
  6. import net.sf.hibernate.cfg.Configuration;  
  7. import test.hibernate.Product;  
  8.  
  9. // 用法:  
  10. // java test.InsertProduct name amount price  
  11. public class InsertProduct {  
  12.  
  13. public static void main(String[] args)  
  14. throws Exception {  
  15.  
  16. // 1. 創(chuàng)建Product對象  
  17. Product p = new Product();  
  18. p.setName(args[0]);  
  19. p.setAmount(Integer.parseInt(args[1]));  
  20. p.setPrice(Double.parseDouble(args[2]));  
  21.  
  22. // 2. 啟動Hibernate  
  23. Configuration cfg = new Configuration()  
  24.  .addClass(Product.class);  
  25. SessionFactory sf = cfg.buildSessionFactory();  
  26.  
  27. // 3. 打開Session  
  28. Session sess = sf.openSession();  
  29.  
  30. // 4. 保存Product,關(guān)閉Session  
  31. Transaction t = sess.beginTransaction();  
  32. sess.save(p);  
  33. t.commit();  
  34. sess.close();  
  35. }  

當(dāng)然,INFO行指出我們需要一個Hibernate.properties配置文件。在這個文件中,我們配置要使用的數(shù)據(jù)庫、用戶名和密碼以及其他選項。使用下面提供的這個示例來連接前面提到的Hypersonic數(shù)據(jù)庫:

  1. hibernate.connection.username=sa 
  2. hibernatehibernate.connection.password=  
  3. hibernate.connection.url=jdbc:hsqldb:/home/davor/hibernate/orders  
  4. hibernate.connection.driver_class=org.hsqldb.jdbcDriver  
  5. hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect 

適當(dāng)?shù)剡M(jìn)行修改(例如,可能需要修改Hibernate.connection.url),并保存到classpath中。這很容易,但那個 test/Hibernate/Product.hbm.xml資源是什么呢?它是一個XML文件,定義了Java對象如何被持久化(映射)到一個數(shù)據(jù)庫。在該文件中,我們定義數(shù)據(jù)存儲到哪個數(shù)據(jù)庫表中,哪個字段映射到數(shù)據(jù)庫表的哪個列,不同的對象如何互相關(guān)聯(lián),等等。讓我們來看一下 Product.hbm.xml。

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE hibernate-mappingPUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
    > 
  3.  
  4. <hibernate-mapping> 
  5. <class name="test.hibernate.Product"table="products"> 
  6.  
  7. <id name="id" type="string"unsaved-value="null"> 
  8. <column name="id" sql-type="char(32)"not-null="true"/> 
  9. <generator class="uuid.hex"/> 
  10. </id> 
  11. <property name="name"> 
  12. <column name="name" sql-type="char(255)"not-null="true"/> 
  13. </property> 
  14. <property name="price"> 
  15. <column name="price" sql-type="double"not-null="true"/> 
  16. </property> 
  17. <property name="amount"> 
  18. <column name="amount" sql-type="integer"not-null="true"/> 
  19. </property>   
  20. </class> 
  21. </hibernate-mapping> 

【編輯推薦】

  1. Hibernate對數(shù)據(jù)索引進(jìn)行緩存
  2. 剖析Hibernate主鍵生成幾種常用方式
  3. 淺析Hibernate實現(xiàn)實體對象延遲加載
  4. Hibernate集合類型的延遲加載特性
  5. 概括Hibernate屬性延遲加載
責(zé)任編輯:佚名 來源: 51CTO.com
相關(guān)推薦

2009-09-29 16:46:01

創(chuàng)建Hibernate

2009-09-24 15:42:44

Hibernate對象

2009-09-24 16:39:20

Hibernate傳播

2009-09-25 17:19:28

Hibernate持久

2009-09-29 16:11:45

Hibernate實現(xiàn)

2009-09-28 15:38:12

Hibernate P

2009-09-25 09:30:33

Hibernate持久

2009-06-17 16:00:03

Hibernate自定

2009-09-23 17:00:07

Hibernate持久

2009-09-21 17:46:34

Hibernate持久

2009-09-29 10:57:25

設(shè)置Hibernate

2009-09-29 10:37:29

Hibernate持久

2022-09-21 23:29:15

Python點云數(shù)據(jù)

2009-09-27 09:55:38

Hibernate持久

2023-09-06 17:02:53

2011-03-24 11:37:41

Hibernate

2009-06-26 16:27:10

Hibernate創(chuàng)建

2013-09-12 14:56:02

iOS持久化

2009-06-16 14:11:36

Hibernate優(yōu)點Hibernate構(gòu)架

2010-08-05 13:23:05

NFS協(xié)議
點贊
收藏

51CTO技術(shù)棧公眾號