天天看點

Hibernate Annotation 學習

1、一對多關聯,級聯操作

@OneToMany(mappedBy = "paymentad", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = PaymentAdHistory.class)

public List<PaymentAdHistory> getPaymentHis() {

    return paymentHis;

}

PaymentAdHistory類中

   @ManyToOne(cascade = { CascadeType.MERGE })

   @JoinColumn(name = "PAYMENTAD")

   @NotFound(action = NotFoundAction.IGNORE)

   public PaymentAd getPaymentad() {

       return paymentad;

   }

2、重新整理

Adagent aa =adagentDao.get(id);

adagentDao.getSession().refresh(aa);

3、Hibernate Annotation (Hibernate 注解)

<a href="http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html" target="_blank">http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html</a>

==============================================================================

CascadeType.REFRESH:級聯重新整理,當多個使用者同時作操作一個實體,為了使用者取到的資料是實時的,在用實體中的資料之前就可以調用一下refresh()方法!

CascadeType.REMOVE:級聯删除,當調用remove()方法删除Order實體時會先級聯删除OrderItem的相關資料!

CascadeType.MERGE:級聯更新,當調用了Merge()方法,如果Order中的資料改變了會相應的更新OrderItem中的資料,

CascadeType.ALL:包含以上所有級聯屬性。

(注:以上幾種級聯操作,隻能實在滿足資料庫的限制時才能生效,比如上邊的Order和OrderItem存在主外鍵關聯是以執行REMOVE()方法時是不能實作級聯删除的)

CascadeType.PERSIST:級聯儲存,當調用了Persist() 方法,會級聯儲存相應的資料

2、級聯删除

<a href="http://www.blogjava.net/jiafang83/archive/2009/07/26/288438.html" target="_blank">Hibernate一對多雙向annotation配置</a>

school和userMember是一對多關系:

SchoolInfo.java

1

Hibernate Annotation 學習

import javax.persistence.CascadeType;   

2

Hibernate Annotation 學習

import javax.persistence.Column;   

3

Hibernate Annotation 學習

import javax.persistence.Entity;   

4

Hibernate Annotation 學習

import javax.persistence.FetchType;   

5

Hibernate Annotation 學習

import javax.persistence.GeneratedValue;   

6

Hibernate Annotation 學習

import javax.persistence.Id;   

7

Hibernate Annotation 學習

import javax.persistence.OneToMany;   

8

Hibernate Annotation 學習

import javax.persistence.Table;   

9

Hibernate Annotation 學習

import javax.persistence.Temporal;   

10

Hibernate Annotation 學習

import javax.persistence.TemporalType;   

11

Hibernate Annotation 學習

12

Hibernate Annotation 學習

import org.hibernate.annotations.Formula;   

13

Hibernate Annotation 學習

import org.hibernate.annotations.GenericGenerator;   

14

Hibernate Annotation 學習

15

Hibernate Annotation 學習

@Entity   

16

Hibernate Annotation 學習

@Table(name = "school_info")   

17

Hibernate Annotation 學習

public class SchoolInfo implements java.io.Serializable {   

18

Hibernate Annotation 學習

19

Hibernate Annotation 學習

    @Id   

20

Hibernate Annotation 學習

    @GeneratedValue(generator = "system-uuid")   

21

Hibernate Annotation 學習

    @GenericGenerator(name = "system-uuid", strategy = "uuid")   

22

Hibernate Annotation 學習

    private String id;//hibernate的uuid機制,生成32為字元串   

23

Hibernate Annotation 學習

24

Hibernate Annotation 學習

    @Column(name = "actcodeId", updatable = false, nullable = true, length = 36)   

25

Hibernate Annotation 學習

    private String actcodeId;   

26

Hibernate Annotation 學習

27

Hibernate Annotation 學習

    @Formula("select COUNT(*) from school_info")   

28

Hibernate Annotation 學習

    private int count;   

29

Hibernate Annotation 學習

30

Hibernate Annotation 學習

    @Temporal(TemporalType.TIMESTAMP)//不用set,hibernate會自動把目前時間寫入   

31

Hibernate Annotation 學習

    @Column(updatable = false, length = 20)   

32

Hibernate Annotation 學習

    private Date createTime;   

33

Hibernate Annotation 學習

34

Hibernate Annotation 學習

    @Temporal(TemporalType.TIMESTAMP)   

35

Hibernate Annotation 學習

    private Date updateTime;// 剛開始我預設insertable=false,但會讀取出錯提示如下:   

36

Hibernate Annotation 學習

    // Value '0000-00-00' can not be represented as java.sql.Timestamp   

37

Hibernate Annotation 學習

38

Hibernate Annotation 學習

    // mappedBy="school"就相當于inverse=true,(mappedBy指定的是不需要維護關系的一端)   

39

Hibernate Annotation 學習

    // 應該注意的是mappedBy值對應@ManyToOne标注的屬性,我剛開始寫成"schoolId",讓我郁悶了好一會 

40

Hibernate Annotation 學習

   @OneToMany(mappedBy = "school", cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = UserMember.class)   

41

Hibernate Annotation 學習

    // 用範性的話,就不用targetEntity了   

42

Hibernate Annotation 學習

    private List&lt;usermember&gt; users = &lt;/usermember&gt;new ArrayList&lt;usermember&gt;();  &lt;/usermember&gt; 

43

Hibernate Annotation 學習

44

Hibernate Annotation 學習

}   

45

Hibernate Annotation 學習

@GeneratedValue(strategy=GenerationType.AUTO)我們常用的自增長機制,我這裡采用的是hibernate的uuid生成機制.

需要注意的是import javax.xx.Entity ,而不是org.hibernate.xx.Entity。

郁悶的是我上面用到@Formula,生成的sql竟然是'select COUNT(*) from school_info as formula0_ from school_info schoolinfo0_,當然不能執行了,尋求正解中~!!!!!!!!!

UserMember.java(前面引入的包已經貼過了,下面就不貼了) 

1 @Entity   

2 @Table(name = "teacher_info")//實體類和資料庫表名不一緻時,才用這個   

3 public class UserMember implements java.io.Serializable {   

4   

5    @Id   

6    @GeneratedValue(generator = "system-uuid")   

7    @GenericGenerator(name = "system-uuid", strategy = "uuid")   

8    private String id;   

9   

10    @Column(updatable = false, nullable = false, length = 20)   

11    private String logonName;   

12       

13    @Temporal(TemporalType.TIMESTAMP)   

14    @Column(updatable = false, length = 20)   

15    private Date createTime;   

16   

17    @Temporal(TemporalType.TIMESTAMP)   

18    private Date updateTime;   

19   

20   @ManyToOne(cascade = { CascadeType.MERGE })   

21    @JoinColumn(name = "schoolId")   

22    private SchoolInfo school;   

23    //注意該類就不用聲明schoolId屬性了,如果不用@JoinColumn指明關聯的字段,hibernate預設會是school_id.   

24   

25 }  

如果不是在delete()中可以試一下

public void delete_cascade(final Movietype persistentInstance) {  

   log.debug("deleting Movietype instance");  

   try {  

       getHibernateTemplate().executeFind(new HibernateCallback() {  

           public Object doInHibernate(Session s)  

                   throws HibernateException, SQLException {  

               s.setFlushMode(FlushMode.AUTO);  

               s.beginTransaction().begin();  

               s.delete(persistentInstance);  

               s.beginTransaction().commit();  

               s.close();  

               return null;  

           }  

       });  

   } catch (RuntimeException re) {  

       log.error("find all failed", re);  

       throw re;  

   }  

本文轉自追光的貓部落格51CTO部落格,原文連結http://blog.51cto.com/ql0722/1655987如需轉載請自行聯系原作者

00_yatou