日期时间JPA映射
程序员文章站
2022-03-02 15:29:00
...
http://www.mamicode.com/info-detail-1519347.html
数据库日期时间类型
以MySQL为例,Date类型存储日期,Time类型存储时间,DateTime存储日期时间。对应的,这三个MySQL数据库字段类型在实体类的类型分别是java.sql.Date,java.sql.Time,java.sql.Timestamp。
Date类型
数据库表
在MySQL创建表student,字段CreateTime表示记录创建时间,仅仅存储时间。
CREATE TABLE `Student` (
`StudentId` bigint unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`CreateTime` date NOT NULL,
PRIMARY KEY (`StudentId`),
KEY `StudentName` (`Name`)
) ENGINE=InnoDB;
实体类
数据库表student的字段CreateTime是Date类型,对应的,实体类的属性createTime是java.sql.Date类型。
package com.gxz.entities;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Student {
private long id;
private String name;
private Date createTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
持久化
Student student = new Student();
student.setName("张三");
student.setCreateTime(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2016-01-01").getTime()));
manager.persist(student);
查看数据库表student的数据,如下所示。若把持久化的时间代码修改成2016-01-01 01:01:01,持久化的效果依旧不会保存时间,因为字段CreateTime的Date类型只会保存日期,属性java.sql.Date类型只会保存日期。
Student student = new Student();
student.setName("张三");
student.setCreateTime(new Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-01-01 01:01:01").getTime()));
manager.persist(student);
持久化后,查看数据库表student的数据,如下所示。若把数据库表student字段CreateTime的类型修改为DateTime,使之可以保存日期时间,但是,因为属性java.sql.Date类型只会保存日期,所以,该列的时间部分依旧全部为0。首先,我们修改数据库。
CREATE TABLE `Student` (
`StudentId` bigint unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`CreateTime` datetime NOT NULL,
KEY `StudentName` (`Name`)
) ENGINE=InnoDB;
持久化的时间代码如下所示。student.setCreateTime(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2016-01-01").getTime()));
持久化后,查看数据库表student的数据,如下所示。
持久化的时间代码如下所示。
student.setCreateTime(new Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-01-01 01:01:01").getTime()));
持久化后,查看数据库表student的数据,如下所示。
DateTime类型
数据库表
把数据库表student字段CreateTime的类型修改为DateTime,使之可以保存日期时间。
实体类
数据库表student的字段CreateTime是DateTime类型,对应的,实体类的属性createTime是java.sql.Timestamp类型。
package com.gxz.entities;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Student {
private long id;
private String name;
private Timestamp createTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
}
持久化
Student student = new Student();
student.setName("张三");
student.setCreateTime(new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-01-02 02:01:01").getTime()));
manager.persist(student);
持久化后,查看数据库表student的数据,如下所示。Time类型
数据库表
把数据库表student字段CreateTime的类型修改为time,仅仅可以保存时间。
CREATE TABLE `Student` (
`StudentId` bigint unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`CreateTime` time NOT NULL,
KEY `StudentName` (`Name`)
) ENGINE=InnoDB;
实体类
数据库表student的字段CreateTime是time类型,对应的,实体类的属性createTime是java.sql.Time类型。
package com.gxz.entities;
import java.sql.Time;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Student {
private long id;
private String name;
private Time createTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Time getCreateTime() {
return createTime;
}
public void setCreateTime(Time createTime) {
this.createTime = createTime;
}
}
持久化
Student student = new Student();
student.setName("张三");
student.setCreateTime(new Time(new SimpleDateFormat("HH:mm:ss").parse("02:01:01").getTime()));
manager.persist(student);
持久化后,查看数据库表student的数据,如下所示。实体类属性是java.util.Calendar或java.util.Date
如果实体类属性是java.util.Calendar或java.util.Date,需要把这种类型转化为java.sql.Time、java.sql.Timestamp、java.sql.Date。实现转化的注解是javax.persistence.Temporal,该注解有一个属性javax.persistence.TemporalType,取值分别是TemporalType.DATE、TemporalType.TIME、TemporalType.TIMESTAMP。具体转化关系如下所述。
1.TemporalType.DATE:把java.util.Calendar或java.util.Date转化为java.sql.Date(持久化实体时),或把java.sql.Date转化为java.util.Calendar或java.util.Date(获取实体时)。
2.TemporalType.TIME:把java.util.Calendar或java.util.Date转化为java.sql.Time(持久化实体时),或把java.sql.TIME转化为java.util.Calendar或java.util.Time(获取实体时)。
3.TemporalType.TIMESTAMP:把java.util.Calendar或java.util.Date转化为java.sql.Timestamp(持久化实体时),或把java.sql.Timestamp转化为java.util.Calendar或java.util.Time(获取实体时)。
数据库表(DateTime类型)
数据库表student的字段CreateTime是DateTime类型。
实体类(Calendar类型)
实体类的属性是createTime的类型是java.util.Calendar,需要把java.util.Calendar转化为java.sql.Timestamp。
package com.gxz.entities;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table
public class Student {
private long id;
private String name;
private Calendar createTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Temporal(TemporalType.TIMESTAMP)
public Calendar getCreateTime() {
return createTime;
}
public void setCreateTime(Calendar createTime) {
this.createTime = createTime;
}
}
持久化(Calendar类型)
Student student = new Student();
student.setName("张三");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-05-12 12:25:23"));
student.setCreateTime(calendar);
持久化后,查看数据库表student的数据,如下所示。数据库表(Date类型)
数据库表student的字段CreateTime是Date类型。
实体类(java.util.Date类型)
实体类的属性是createTime的类型是java.util.Date,需要把java.util.Date转化为java.sql.Date。
package com.gxz.entities;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table
public class Student {
private long id;
private String name;
private Date createTime;
@Temporal(TemporalType.DATE)
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
持久化(java.util.Date类型)
Student student = new Student();
student.setName("张三");
student.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").parse("2016-05-13"));
manager.persist(student);
持久化后,查看数据库表student的数据,如下所示。
标签:
原文地址:http://blog.csdn.net/seedshome/article/details/52316913
上一篇: JPA字段映射问题