All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.omnifaces.persistence.model.BaseEntity Maven / Gradle / Ivy

There is a newer version: 0.22.J1
Show newest version
package org.omnifaces.persistence.model;

import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.util.Objects;

import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import org.omnifaces.persistence.listener.BaseEntityListener;
import org.omnifaces.persistence.service.BaseEntityService;

/**
 * 

* Let all your entities extend from this. Then you can make use of {@link BaseEntityService}. This mapped superclass * already automatically takes care of the id column. *

* There are two more mapped superclasses which may also be of interest. *

    *
  • {@link TimestampedEntity} - extends {@link BaseEntity} with created and lastModified * columns and automatically takes care of them. *
  • {@link VersionedEntity} - extends {@link TimestampedEntity} with a @Version column. *
* * @param The generic ID type, usually {@link Long}. * @author Bauke Scholtz */ @MappedSuperclass @EntityListeners(BaseEntityListener.class) public abstract class BaseEntity & Serializable> implements Comparable>, Identifiable, Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = IDENTITY) private I id; @Override public I getId() { return id; } @Override public void setId(I id) { this.id = id; } /** * Hashes by default the classname and ID. */ @Override public int hashCode() { return (getId() != null) ? Objects.hash(getClass().getSimpleName(), getId()) : super.hashCode(); } /** * Compares by default by entity class (proxies taken into account) and ID. */ @Override public boolean equals(Object other) { return (other != null && getId() != null && other.getClass().isAssignableFrom(getClass()) && getClass().isAssignableFrom(other.getClass())) ? getId().equals(((BaseEntity) other).getId()) : (other == this); } /** * Orders by default with "nulls last". */ @Override public int compareTo(BaseEntity other) { return (other == null) ? -1 : (getId() == null) ? (other.getId() == null ? 0 : 1) : getId().compareTo(other.getId()); } /** * The default format is ClassName[id={id}]. */ @Override public String toString() { return String.format("%s[id=%d]", getClass().getSimpleName(), getId()); } }