1. 程式人生 > >entityBean中使用非持久化變數

entityBean中使用非持久化變數

entityBean中的屬性一般都要持久化到資料庫,但程式有時候從entityBean獲取的資料不需要持久化到資料庫,這時用

註解@Transient。

所處理的資料不需要在entityBean中宣告屬性,只需要get()方法,再將@Transient加在get()方法上。

示例:

 
   @Transient
   public BigDecimal getTotal()
   {
      return hotel.getPrice().multiply( new BigDecimal( getNights() ) );
   }

此示例方法將返回total這個資料,而不會把 total持久化到資料庫。