gpt4 book ai didi

mysql - 默认日期值 hibernate

转载 作者:行者123 更新时间:2023-11-29 04:35:52 24 4
gpt4 key购买 nike

我在 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;

}

提前致谢!!!

最佳答案

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

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

从javadoc中找到here :

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.

Supported property types:

  • java.util.Date
  • Calendar
  • java.sql.Date
  • Time
  • Timestamp

如果您想使用特定于 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();
}

关于mysql - 默认日期值 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42194538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com