简体   繁体   English

默认日期值Hibernate

[英]Default Date value Hibernate

I am inserting some data on a MySQL db without any problem. 我在MySQL数据库上插入一些数据没有任何问题。 Now I want to add one more column "date" and I want to insert as default the Date of the insert. 现在我想再添加一列“date”,我想插入默认的插入日期。 How can I do that? 我怎样才能做到这一点?

"FbData.hbm.xml" : “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>

And my Class with getters and setters: 我的班级有吸气剂和二传手:

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;

}

Thanks in Advance!!! 提前致谢!!!

The Hibernate specific way to do this is to use the @CreationTimestamp annotation. Hibernate执行此操作的具体方法是使用@CreationTimestamp注释。

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

From the javadoc found here : 这里找到的javadoc:

Marks a property as the creation timestamp of the containing entity. 将属性标记为包含实体的创建时间戳。 The property value will be set to the current JVM date exactly once when saving the owning entity for the first time. 第一次保存拥有实体时,属性值将恰好设置为当前JVM日期一次。

Supported property types: 支持的属性类型:

  • java.util.Date java.util.Date
  • Calendar 日历
  • java.sql.Date java.sql.Date
  • Time 时间
  • Timestamp 时间戳

If you want to do this using a JPA-specific solution, you can do this two ways: 如果要使用特定于JPA的解决方案执行此操作,可以通过以下两种方式执行此操作:

  • Apply an Entity Listener handler. 应用实体侦听器处理程序。
  • Apply a @PrePersist callback method on the entity 在实体上应用@PrePersist回调方法

To apply an entity listener: 要应用实体侦听器:

// 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
}

If you need the functionality across multiple entities, you can easily hook into this with minimal effort and manage the functionality in one place rather than in multiple entities. 如果您需要跨多个实体的功能,您可以轻松地以最小的工作量并在一个地方而不是在多个实体中管理功能。

But that's a lot of work for a simple, single entity feature. 但这对于简单的单一实体功能来说是很多工作。 If that is the case, you can use the other suggested answer with just adding an annotated JPA callback method on the entity itself 如果是这种情况,您可以使用其他建议的答案,只需在实体本身上添加带注释的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();
}

You can use @PrePersist annotation to assign current date to the attribute before inserting the row to database 在将行插入数据库之前,可以使用@PrePersist批注为属性分配当前日期

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

This way, if you don't set the value of the date and try to persist the object to the DB, you'll get current date as a value. 这样,如果您没有设置date的值并尝试将对象持久保存到数据库,您将获得当前日期作为值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM