Total Pageviews

2015/05/19

[JPA] timestamp does not save into database

Problem
Here is my entity
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@Entity
public class Dbm100f1 implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "DBM100F1_REVIEWSEQNO_GENERATOR", sequenceName = "SE_DBM_DBM100F1")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DBM100F1_REVIEWSEQNO_GENERATOR")
    @Column(name = "REVIEW_SEQ_NO")
    private long reviewSeqNo;
 
    @Temporal(TemporalType.DATE)
    @Column(name = "CREATE_DATE")
    private Date createDate;
 
    @Temporal(TemporalType.DATE)
    @Column(name = "UPDATE_DATE")
    private Date updateDate;
 
    //ignore other fields and getter/setter methods

|

But as I save this entity into database, it save date only exclude time.
Solution
Change the TemporalType
  • TemporalType.DATE : represents date only (e.g. 2015/05/19).
  • TemporalType.TIME : represents time only (e.g. 10:28:34).
  • TemporalType.TIMESTAMP : represents date and time (e.g. 2015/05/19 10:28:34).

Theorefore, amend the entity as bellowing:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@Entity
public class Dbm100f1 implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "DBM100F1_REVIEWSEQNO_GENERATOR", sequenceName = "SE_DBM_DBM100F1")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DBM100F1_REVIEWSEQNO_GENERATOR")
    @Column(name = "REVIEW_SEQ_NO")
    private long reviewSeqNo;
 
    @Temporal(TemporalType.TIMESTAMP )
    @Column(name = "CREATE_DATE")
    private Date createDate;
 
    @Temporal(TemporalType.TIMESTAMP )
    @Column(name = "UPDATE_DATE")
    private Date updateDate;
 
    //ignore other fields and getter/setter methods

|


Then the create_date and update_date can save date and time now.


Reference
[1] http://www.objectdb.com/java/jpa/entity/types#Date_and_Time_Temporal_Types_

No comments: