PersistenceBase.java
package com.tradecloud.common.base;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import java.io.Serializable;
import java.util.*;
/**
* Base class for all Tradecloud persisted entities.
*/
@MappedSuperclass
@XmlTransient
@XmlAccessorType(XmlAccessType.FIELD)
@SuperBuilder
@NoArgsConstructor
public abstract class PersistenceBase implements Serializable, Cloneable {
private static final long serialVersionUID = 8080352710661841912L;
/**
* Entity Id.
*/
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@XmlAttribute
@Column(name = "id", columnDefinition = "SERIAL", length = 20)
protected Long id;
/**
* Entity creation timestamp.
* XML transient because they are not really needed there.
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = true)
@XmlTransient
@JsonIgnore
protected Date created = new Date();
/**
* Entity updated/changed timestamp. Populated by hibernate
* XML transient because they are not really needed there.
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = true)
@Version
@XmlTransient
protected Date updated;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreated() {
return created;
}
public Date getUpdated() {
return updated;
}
public void setCreated(Date date) {
this.created = date;
}
public void setUpdated(Date date) {
this.updated = date;
}
/**
* Whether the object is active (not deleted). This is currently not mapped to a db field as it will take a lot of table changing.
* Initially using the state field in subclasses, eg state != DELETED.
*
* @return
*/
@JsonIgnore
public Boolean getActive() {
return Boolean.TRUE;
}
@Override
public PersistenceBase clone() {
try {
PersistenceBase clone = (PersistenceBase) super.clone();
// The default behavior seems to be to copy stuff. You probably never want to id copied.
clone.setId(null);
return clone;
} catch (CloneNotSupportedException ex) {
return null;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!HibernateUtils.proxyClassEquals(this, obj))
return false;
PersistenceBase other = (PersistenceBase) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
/**
* Utility method to get only the active objects from a collection.
*
* @param <X>
* @param col
* @return The set of active objects.
*/
public static <X extends PersistenceBase> Set<X> getActiveSet(Collection<X> col) {
Set<X> set = new HashSet<X>();
for (X c : col) {
if (c.getActive()) {
set.add(c);
}
}
return set;
}
/**
* Utility method to get only the active objects from a collection.
*
* @param <X>
* @param col
* @return The list of active objects.
*/
public static <X extends PersistenceBase> List<X> getActiveList(Collection<X> col) {
List<X> list = new ArrayList<X>();
for (X c : col) {
if (c.getActive()) {
list.add(c);
}
}
return list;
}
public PersistenceBase initialize() {
return HibernateUtils.initializeAndUnproxy(this);
}
}