亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 2374 | 回復(fù): 1
打印 上一主題 下一主題

Hibernate Annotation文檔整理(一) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-03-19 17:25 |只看該作者 |倒序?yàn)g覽
Hibernate Annotation文檔整理(一)






Setting up an annotations project
•HibernateUtil類(Annotation方式)
Java代碼
  1. 1.public class HibernateUtil {   
  2. 2.private static final SessionFactory sessionFactory;   
  3. 3.    static {   
  4. 4.        try {   
  5. 5.            sessionFactory = new AnnotationConfiguration()   
  6. 6.                    .configure().buildSessionFactory();   
  7. 7.        } catch (Throwable ex) {   
  8. 8.            // Log exception!   
  9. 9.            throw new ExceptionInInitializerError(ex);   
  10. 10.        }   
  11. 11.    }   
  12. 12.    public static Session getSession()   
  13. 13.            throws HibernateException {   
  14. 14.        return sessionFactory.openSession();   
  15. 15.    }   
  16. 16.}  
  17. public class HibernateUtil {
  18. private static final SessionFactory sessionFactory;
  19.     static {
  20.         try {
  21.             sessionFactory = new AnnotationConfiguration()
  22.                     .configure().buildSessionFactory();
  23.         } catch (Throwable ex) {
  24.             // Log exception!
  25.             throw new ExceptionInInitializerError(ex);
  26.         }
  27.     }
  28.     public static Session getSession()
  29.             throws HibernateException {
  30.         return sessionFactory.openSession();
  31.     }
  32. }
復(fù)制代碼
•需要添加hibernate.cfg.xml配置文件,內(nèi)容如:



Xml代碼
  1. 1.<!DOCTYPE hibernate-configuration PUBLIC   
  2. 2.    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
  3. 3.    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4. 4.<hibernate-configuration>  
  5. 5.  <session-factory>  
  6. 6.    <mapping package="test.animals"/>  
  7. 7.    <mapping class="test.Flight"/>  
  8. 8.    <mapping class="test.Sky"/>  
  9. 9.    <mapping class="test.Person"/>  
  10. 10.    <mapping class="test.animals.Dog"/>  
  11. 11.  
  12. 12.    <mapping resource="test/animals/orm.xml"/>  
  13. 13.  </session-factory>  
  14. 14.</hibernate-configuration>  
  15. <!DOCTYPE hibernate-configuration PUBLIC
  16.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  17.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  18. <hibernate-configuration>
  19.   <session-factory>
  20.     <mapping package="test.animals"/>
  21.     <mapping class="test.Flight"/>
  22.     <mapping class="test.Sky"/>
  23.     <mapping class="test.Person"/>
  24.     <mapping class="test.animals.Dog"/>

  25.     <mapping resource="test/animals/orm.xml"/>
  26.   </session-factory>
  27. </hibernate-configuration>  
復(fù)制代碼
hibernate 特定的屬性

Property
描述

hibernate.cache.default_cache_concurrency_strategy
當(dāng)使用注解@Cacheable @Cache時(shí),用來(lái)給的默認(rèn)org.hibernate.annotations.CacheConcurrencyStrategy設(shè)置名稱,@Cache(strategy="..") 可以覆蓋默認(rèn)設(shè)置。
  
hibernate.id.new_generator_mappings
值為true或者false,這個(gè)設(shè)置表示是否新建的IdentifierGenerator實(shí)現(xiàn)類的生成策略為AUTO、Table和Sequence。默認(rèn)為false,以保持向后兼容性。  

我們建議所有新項(xiàng)目使用hibernate.id.new_generator_mappings= true,新的生成器是更有效率和更密切的JPA規(guī)范語(yǔ)義。然而,他們不向后兼容現(xiàn)有的數(shù)據(jù)庫(kù)(如果ID生成一個(gè)序列或表)。

Mapping Entities
Marking a POJO as persistent entity




Java代碼
  1. 1.@Entity  
  2. 2.public class Flight implements Serializable {   
  3. 3.    Long id;   
  4. 4.  
  5. 5.    @Id  
  6. 6.    public Long getId() { return id; }   
  7. 7.  
  8. 8.    public void setId(Long id) { this.id = id; }   
  9. 9.}   
  10. 10.public class Flight implements Serializable {  
  11. @Entity
  12. public class Flight implements Serializable {
  13.     Long id;

  14.     @Id
  15.     public Long getId() { return id; }

  16.     public void setId(Long id) { this.id = id; }
  17. }
  18. public class Flight implements Serializable {  


  19. Defining the table


  20. @Table元素包含一個(gè)schema和catalog屬性,如果他們需要被定義。

  21. 你還可以使用@ UniqueConstraint 給表定義唯一約束(建議使用@Column.unique方法。)



  22. Java代碼  
  23. 1.@Table(name="tbl_sky",   
  24. 2.    uniqueConstraints = {@UniqueConstraint(columnNames={"month", "day"})}   
  25. 3.)  
  26. @Table(name="tbl_sky",
  27.     uniqueConstraints = {@UniqueConstraint(columnNames={"month", "day"})}
  28. )  


  29. 通過(guò)@Version設(shè)置樂(lè)觀鎖


  30. Java代碼  
  31. 1.@Entity  
  32. 2.public class Flight implements Serializable {   
  33. 3.    ...   
  34. 4.    @Version  
  35. 5.    @Column(name="OPTLOCK")   
  36. 6.    public Integer getVersion() { ... }   
  37. 7.}  
  38. @Entity
  39. public class Flight implements Serializable {
  40.     ...
  41.     @Version
  42.     @Column(name="OPTLOCK")
  43.     public Integer getVersion() { ... }
  44. }  
復(fù)制代碼
version這個(gè)屬性會(huì)被映射成為樂(lè)觀鎖字段,實(shí)體管理器會(huì)通過(guò)它檢測(cè)到有沖突的更新。為了防止丟失更新,可以設(shè)置最晚提交生效策略(last-commit-wins strategy)。

version字段可以是數(shù)字或者時(shí)間戳,Hibernate支持自定義的或者適當(dāng)?shù)膶?shí)現(xiàn)UserVersionType的類型。

Mapping simple properties
Declaring basic property mappings(聲明基本屬性映射)
實(shí)體中任何一個(gè)非靜態(tài)的、非暫時(shí)性屬性都認(rèn)為是持久化字段,除非使用@Transient注解。

屬性不加注解相當(dāng)于加@Basic,@Basic允許聲明加載策略(FetchType)。



Java代碼
  1. 1.public transient int counter; //transient property   
  2. 2.  
  3. 3.private String firstname; //persistent property   
  4. 4.  
  5. 5.@Transient  
  6. 6.String getLengthInMeter() { ... } //transient property   
  7. 7.  
  8. 8.String getName() {... } // persistent property   
  9. 9.  
  10. 10.@Basic  
  11. 11.int getLength() { ... } // persistent property   
  12. 12.  
  13. 13.@Basic(fetch = FetchType.LAZY)   
  14. 14.String getDetailedComment() { ... } // persistent property   
  15. 15.  
  16. 16.@Temporal(TemporalType.TIME)   
  17. 17.java.util.Date getDepartureTime() { ... } // persistent property   
  18. 18.  
  19. 19.@Enumerated(EnumType.STRING)   
  20. 20.Starred getNote() { ... } //enum persisted as String in database  
  21. public transient int counter; //transient property

  22. private String firstname; //persistent property

  23. @Transient
  24. String getLengthInMeter() { ... } //transient property

  25. String getName() {... } // persistent property

  26. @Basic
  27. int getLength() { ... } // persistent property

  28. @Basic(fetch = FetchType.LAZY)
  29. String getDetailedComment() { ... } // persistent property

  30. @Temporal(TemporalType.TIME)
  31. java.util.Date getDepartureTime() { ... } // persistent property

  32. @Enumerated(EnumType.STRING)
  33. Starred getNote() { ... } //enum persisted as String in database  
復(fù)制代碼
在普通的Java API中,時(shí)間精度是沒(méi)有定義的。當(dāng)處理時(shí)間數(shù)據(jù)時(shí),你可能需要在數(shù)據(jù)庫(kù)中描述期望的時(shí)間精度。

時(shí)間數(shù)據(jù)可以有DATE、TIME、TIMESTAMP的精度,通過(guò)@Temporal注解可以微調(diào)。

@Lob標(biāo)識(shí)屬性應(yīng)該持久化為Blob或者Clob類型,這決定于屬性的類型。
java.sql.Clob、Character[]、char[] 和 String會(huì)持久化成Clob。
java.sql.Blob、Byte[]、byte[]和Serializable會(huì)被持久化成Blob。



Java代碼
  1. 1.@Lob  
  2. 2.public String getFullText() {   
  3. 3.    return fullText;   
  4. 4.}   
  5. 5.  
  6. 6.@Lob  
  7. 7.public byte[] getFullCode() {   
  8. 8.    return fullCode;   
  9. 9.}  
復(fù)制代碼

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2012-03-19 17:25 |只看該作者
謝謝分享
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP