最近项目的实体类中使用了 Java8 中新的日期和时间 API,在数据持久化时遇见一些曲折,记录下来作为前车之鉴。
Task 实体类:
import com.*.*.domain.enumration.TaskStatus;
import lombok.ToString;
import javax.persistence.*;
import java.io.Serializable;
import java.time.ZonedDateTime;
/**
* 任务实体类
*
* @author smileorsilence
* @date 2018/04/28
*/
@ToString
@Entity
@Table(name = "task")
public class Task implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(name = "uuid", unique = true)
private String uuid;
@Column(name = "priority")
private Integer priority = 10;
@Column(name = "created_time")
// @Temporal(TemporalType.TIMESTAMP)
// org.hibernate.AnnotationException:
// @Temporal should only be set on a java.util.Date or java.util.Calendar property
private ZonedDateTime createdTime;
// 省略getters & setters & equals & hashCode ...
}
修改前的 maven 依赖:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>