默认日期值休眠

Mat*_*sMS 2 mysql hibernate date insert sql-insert

我在 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>
Run Code Online (Sandbox Code Playgroud)

还有我的带有 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;

}
Run Code Online (Sandbox Code Playgroud)

提前致谢!!!

Nar*_*ros 7

执行此操作的 Hibernate 特定方法是使用@CreationTimestamp注释。

@Entity
public class SomeEntity {
  @CreationTimestamp
  private Date date;
}
Run Code Online (Sandbox Code Playgroud)

这里找到的javadoc :

将属性标记为包含实体的创建时间戳。当第一次保存拥有实体时,属性值将被设置为当前的 JVM 日期一次。

支持的属性类型:

  • 日期
  • 日历
  • 日期
  • 时间
  • 时间戳

如果要使用特定于 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
}
Run Code Online (Sandbox Code Playgroud)

如果您需要跨多个实体的功能,您可以以最少的努力轻松地连接到这一点,并在一个地方而不是在多个实体中管理功能。

但是对于一个简单的单一实体功能来说,这需要做很多工作。如果是这种情况,您可以使用其他建议的答案,只需在实体本身上添加带注释的 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();
}
Run Code Online (Sandbox Code Playgroud)