2017-02-12 156 views
0

我插入一些數據在MySQL數據庫沒有任何問題。現在我想再添加一列「日期」,我想插入日期作爲默認插入。我怎樣才能做到這一點?默認日期值休眠

「FbData.hbm.xml」:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="com.hibernate.FbData" table="dataresponse" catalog="karma"> 
     <id name="idData" type="java.lang.Integer"> 
      <column name="idData" /> 
      <generator class="identity" /> 
     </id> 
     <property name="likes" type="java.lang.Long"> 
      <column name="likes" not-null="false" unique="false" /> 
     </property> 
     <property name="shares" type="java.lang.Long"> 
      <column name="shares" not-null="false" unique="false" /> 
     </property> 
     <property name="time" type="string"> 
      <column name="time" not-null="false" unique="false" /> 
     </property> 
     <property name="idPage" type="string"> 
      <column name="idPage" not-null="false" unique="false" /> 
     </property> 
    </class> 
</hibernate-mapping> 

我的類具有getter和setter方法:

public class FbData implements Serializable{ 

    private static final long serialVersionUID = 1L; 

    private Integer idData; 
    private Long likes; 
    private Long shares; 
    private String time; 
    private String idPage; 

} 

先謝謝了!

回答

1

您可以使用@PrePersist註釋行插入到數據庫

@PrePersist 
protected void onCreate() { 
    if (date == null) { 
     date = new Date(); 
    } 
} 

這樣前分配當前日期的屬性,如果不設置date的價值,並嘗試堅持的對象數據庫,你會得到當前的日期作爲價值。

+0

我把這個方法的類「FbData」與註釋。我做插入,但列「日期」的所有字段是「空」。 – MathiasMS

+0

你使用的是什麼版本的Hibernate?你是否啓動Session或EntityManager?我添加了一個涵蓋以Hibernate爲中心和以JPA爲中心的選項的答案。 – Naros

1

Hibernate的具體做法是使用@CreationTimestamp註釋。

@Entity 
public class SomeEntity { 
    @CreationTimestamp 
    private Date date; 
} 

根據JavaDoc發現here

屬性標記爲包含它的實體的創建時間戳。當第一次保存擁有實體時,該屬性值將被設置爲當前的JVM日期。

支持的屬性類型:

  • java.util.Date
  • 日曆
  • java.sql.Date
  • 時間
  • 時間戳

如果你想使用JPA規範來完成此操作℃溶液來,你可以這樣做兩種方式:

  • 應用實體監聽處理程序。
  • 應用在實體@PrePersist回調方法

申請實體監聽:

// A simple interface 
public interface CreationTimestampAware { 
    void setCreationTime(Date date); 
    Date getCreationTime(); 
} 

// Define the entity listener 
public class CreationTimestampListener { 
    @PrePersist 
    public void onPrePersist(Object entity) { 
    // entity implements a CreationTimestampAware interface that exposes 
    // the single method #setCreationTime which we call here to set 
    // the value on the entity. 
    // 
    // Just annotate the entities you want with this functionality 
    // and implement the CreationTimestampAware interface 
    if (CreationTimestampAware.class.isInstance(entity)) { 
     ((CreationTimestampAware) entity).setCreationTime(new Date()); 
    } 
    } 
} 

// Link to your entity 
@EntityListeners({CreationTimestampListener.class}) 
public class SomeEntity implements CreationTimeAware { 
    // specify your date property and implement the interface here 
} 

如果您需要跨多個實體的功能,您可以輕鬆地掛接到這個以最小的努力和管理功能在一個地方而不是在多個實體中。

但是,這對於簡單的單一實體功能來說有很多工作要做。如果是這樣的話,你可以用其他的建議答案只有在實體本身添加註釋的JPA回調方法

@PrePersist 
private void onPersistCallback() { 
    // just set the value here 
    // this will only ever be called once, on a Persist event which 
    // is when the insert occurs. 
    this.date = new Date(); 
}