Java tutorial
/* * Copyright 2015 Kaiserpfalz EDV-Service, Roland T. Lichti * * 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 de.kaiserpfalzEdv.infopir.backend.db; import de.kaiserpfalzEdv.infopir.backend.HasId; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; import org.joda.time.DateTime; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import javax.persistence.Column; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.MappedSuperclass; import javax.persistence.Transient; import java.sql.Timestamp; import static javax.persistence.GenerationType.IDENTITY; /** * The central base entity superclass. Contains the id. * * @author klenkes * @version 2015Q1 * @since 06.09.15 07:49 */ @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public abstract class BaseEntity implements HasId { private static final long serialVersionUID = 6878676245717735227L; private Long id; private Timestamp created; private Timestamp lastModified; @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id_", nullable = false, updatable = false) @Override public Long getId() { return id; } public void setId(final Long id) { this.id = id; } @Column(name = "created_at_", nullable = false, updatable = false) public Timestamp getCreated() { return created; } public void setCreated(final Timestamp timestamp) { created = timestamp; } @Column(name = "last_modified_at_") public Timestamp getLastModified() { return lastModified; } public void setLastModified(final Timestamp timestamp) { lastModified = timestamp; } @Transient public boolean isNew() { return getId() == null; } @Transient @CreatedDate public DateTime getCreatedDate() { return new DateTime(created); } public void setCreatedDate(final DateTime timestamp) { created = new Timestamp(timestamp.getMillis()); } @Transient @LastModifiedDate public DateTime getLastModifiedDate() { return new DateTime(lastModified); } public void setLastModifiedDate(final DateTime timestamp) { lastModified = new Timestamp(timestamp.getMillis()); } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (obj == this) { return true; } if (obj.getClass() != getClass()) { return false; } BaseEntity rhs = (BaseEntity) obj; return new EqualsBuilder().append(this.id, rhs.getId()).isEquals(); } @Override public int hashCode() { return new HashCodeBuilder().append(id).toHashCode(); } @Override public String toString() { return new ToStringBuilder(this).append("id", id).append("created", created) .append("lastModified", lastModified).toString(); } }