Hibernate Annotation文檔整理(一)
Setting up an annotations project
•HibernateUtil類(Annotation方式)
Java代碼- 1.public class HibernateUtil {
- 2.private static final SessionFactory sessionFactory;
- 3. static {
- 4. try {
- 5. sessionFactory = new AnnotationConfiguration()
- 6. .configure().buildSessionFactory();
- 7. } catch (Throwable ex) {
- 8. // Log exception!
- 9. throw new ExceptionInInitializerError(ex);
- 10. }
- 11. }
- 12. public static Session getSession()
- 13. throws HibernateException {
- 14. return sessionFactory.openSession();
- 15. }
- 16.}
- public class HibernateUtil {
- private static final SessionFactory sessionFactory;
- static {
- try {
- sessionFactory = new AnnotationConfiguration()
- .configure().buildSessionFactory();
- } catch (Throwable ex) {
- // Log exception!
- throw new ExceptionInInitializerError(ex);
- }
- }
- public static Session getSession()
- throws HibernateException {
- return sessionFactory.openSession();
- }
- }
復(fù)制代碼 •需要添加hibernate.cfg.xml配置文件,內(nèi)容如:
Xml代碼- 1.<!DOCTYPE hibernate-configuration PUBLIC
- 2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- 3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- 4.<hibernate-configuration>
- 5. <session-factory>
- 6. <mapping package="test.animals"/>
- 7. <mapping class="test.Flight"/>
- 8. <mapping class="test.Sky"/>
- 9. <mapping class="test.Person"/>
- 10. <mapping class="test.animals.Dog"/>
- 11.
- 12. <mapping resource="test/animals/orm.xml"/>
- 13. </session-factory>
- 14.</hibernate-configuration>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <mapping package="test.animals"/>
- <mapping class="test.Flight"/>
- <mapping class="test.Sky"/>
- <mapping class="test.Person"/>
- <mapping class="test.animals.Dog"/>
- <mapping resource="test/animals/orm.xml"/>
- </session-factory>
- </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.@Entity
- 2.public class Flight implements Serializable {
- 3. Long id;
- 4.
- 5. @Id
- 6. public Long getId() { return id; }
- 7.
- 8. public void setId(Long id) { this.id = id; }
- 9.}
- 10.public class Flight implements Serializable {
- @Entity
- public class Flight implements Serializable {
- Long id;
- @Id
- public Long getId() { return id; }
- public void setId(Long id) { this.id = id; }
- }
- public class Flight implements Serializable {
-
- Defining the table
-
- @Table元素包含一個(gè)schema和catalog屬性,如果他們需要被定義。
- 你還可以使用@ UniqueConstraint 給表定義唯一約束(建議使用@Column.unique方法。)
-
- Java代碼
- 1.@Table(name="tbl_sky",
- 2. uniqueConstraints = {@UniqueConstraint(columnNames={"month", "day"})}
- 3.)
- @Table(name="tbl_sky",
- uniqueConstraints = {@UniqueConstraint(columnNames={"month", "day"})}
- )
-
- 通過(guò)@Version設(shè)置樂(lè)觀鎖
-
- Java代碼
- 1.@Entity
- 2.public class Flight implements Serializable {
- 3. ...
- 4. @Version
- 5. @Column(name="OPTLOCK")
- 6. public Integer getVersion() { ... }
- 7.}
- @Entity
- public class Flight implements Serializable {
- ...
- @Version
- @Column(name="OPTLOCK")
- public Integer getVersion() { ... }
- }
-
復(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.public transient int counter; //transient property
- 2.
- 3.private String firstname; //persistent property
- 4.
- 5.@Transient
- 6.String getLengthInMeter() { ... } //transient property
- 7.
- 8.String getName() {... } // persistent property
- 9.
- 10.@Basic
- 11.int getLength() { ... } // persistent property
- 12.
- 13.@Basic(fetch = FetchType.LAZY)
- 14.String getDetailedComment() { ... } // persistent property
- 15.
- 16.@Temporal(TemporalType.TIME)
- 17.java.util.Date getDepartureTime() { ... } // persistent property
- 18.
- 19.@Enumerated(EnumType.STRING)
- 20.Starred getNote() { ... } //enum persisted as String in database
- public transient int counter; //transient property
- private String firstname; //persistent property
- @Transient
- String getLengthInMeter() { ... } //transient property
- String getName() {... } // persistent property
- @Basic
- int getLength() { ... } // persistent property
- @Basic(fetch = FetchType.LAZY)
- String getDetailedComment() { ... } // persistent property
- @Temporal(TemporalType.TIME)
- java.util.Date getDepartureTime() { ... } // persistent property
- @Enumerated(EnumType.STRING)
- 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.@Lob
- 2.public String getFullText() {
- 3. return fullText;
- 4.}
- 5.
- 6.@Lob
- 7.public byte[] getFullCode() {
- 8. return fullCode;
- 9.}
復(fù)制代碼 |