私たちが知っているように、Java の日付/時刻型は変更可能です。したがって、副作用を避けたい場合は、それらをカプセル化する必要があります。
JPA 2.0 でこれらのデータ型を使用し、同時にカプセル化の目標を達成できる方法を探しています。
私の実践では、次のコードのようなパターンをよく使用します。
// imports are omitted
@Entity
@Access(AccessType.PROPERTY)
public class MyEntity {
private Integer id;
@Access(AccessType.FIELD)
private Calendar createdAt;
public MyEntity() {
createdAt = Calendar.getInstance();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Transient
public Date getCreatedAt() {
return createdAt.getTime();
}
public void setCreatedAt(Date createdAt) {
this.createdAt.setTime(createdAt);
}
}
それを行うためのより良い方法はありますか?
前もって感謝します。良い1日を。