/* * Copyright 2012 Nick Stuart * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.bittheory.domain; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.validation.constraints.NotNull; /** * * @author nick */ @MappedSuperclass public class Base implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @NotNull @Column(insertable = true, updatable = false) @Temporal(TemporalType.TIMESTAMP) private Date createdAt; @NotNull @Temporal(TemporalType.TIMESTAMP) private Date updatedAt; public long getId() { return id; } public void setId(long id) { this.id = id; } public Date getCreatedAt() { return createdAt; } public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } public Date getUpdatedAt() { return updatedAt; } public void setUpdatedAt(Date updateAt) { this.updatedAt = updateAt; } @PrePersist @PreUpdate public void updateTimestamps() { this.updatedAt = new Date(); if (createdAt == null) { this.createdAt = new Date(); } } }