Reputation: 35
I am inserting some data on a MySQL db without any problem. Now I want to add one more column "date" and I want to insert as default the Date of the insert. How can I do that?
"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!!!
Upvotes: 2
Views: 9176
Reputation: 21113
The Hibernate specific way to do this is to use the @CreationTimestamp
annotation.
@Entity
public class SomeEntity {
@CreationTimestamp
private Date date;
}
From the javadoc found 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
If you want to do this using a JPA-specific solution, you can do this two ways:
@PrePersist
callback method on the entityTo 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
@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();
}
Upvotes: 8
Reputation: 2823
You can use @PrePersist
annotation to assign current date to the attribute before inserting the row to database
@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.
Upvotes: 2