Java classes: JPA, refactoring from Date to DateTime

With a table created using this SQL

Create Table X (
    ID varchar(4) Not Null,
    XDATE date
);

and an entity class defined in this way

@Entity
@Table(name = "X")
public class X implements Serializable {
    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false, length = 4)
    private String id;
    @Column(name = "XDATE")
    @Temporal(TemporalType.DATE)
    private Date xDate; //java.util.Date
    ...
}

With the above, I can use JPA to achieve relational object matching. However, an attribute xDatecan only store dates, for example. dd/MM/yyyy.

How do I reorganize above to save a full date object using only one field, i.e. dd/MM/yyyy HH24:mm?

+5
source share
2 answers

@Temporal TemporalType.DATETIME? java.util.Date java.sql.Date , TemporalType, JPA / ; , , .

+4

, TemporalType.DATETIME:

@Column(name = "XDATE")
@Temporal(TemporalType.DATETIME)
private Date xDate; //java.util.Date

TIMESTAMP ( xDate 'yyyy-MM-dd HH:mm:ss.S').

+7

All Articles