org.omnifaces.persistence.model.BaseEntity Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of omnipersistence Show documentation
Show all versions of omnipersistence Show documentation
Utilities for JPA, JDBC and DataSources
The newest version!
/*
* Copyright 2021 OmniFaces
*
* 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
*
* https://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 org.omnifaces.persistence.model;
import static java.util.Arrays.stream;
import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsLast;
import static java.util.Objects.requireNonNullElseGet;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Function;
import jakarta.persistence.EntityListeners;
import jakarta.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}.
*
* There are five more mapped superclasses which may also be of interest.
*
* - {@link TimestampedBaseEntity} - extends {@link BaseEntity} with
created
and lastModified
columns and automatically takes care of them.
* - {@link VersionedBaseEntity} - extends {@link TimestampedBaseEntity} with a
@Version
column and automatically takes care of it.
* - {@link GeneratedIdEntity} - extends {@link BaseEntity} with
id
column and automatically takes care of it.
* - {@link TimestampedEntity} - extends {@link GeneratedIdEntity} with
created
and lastModified
columns and automatically takes care of them.
* - {@link VersionedEntity} - extends {@link TimestampedEntity} with a
@Version
column and automatically takes care of it.
*
*
* @param The generic ID type.
* @author Bauke Scholtz
*/
@MappedSuperclass
@EntityListeners(BaseEntityListener.class)
public abstract class BaseEntity & Serializable> implements Comparable>, Identifiable, Serializable {
private static final long serialVersionUID = 1L;
/**
* Hashes by default the ID.
*/
@Override
public int hashCode() {
return hashCode(BaseEntity::getId);
}
@SafeVarargs
@SuppressWarnings("unchecked")
protected final > int hashCode(final Function... getters) {
final var values = stream(getters).map(getter -> getter.apply((E) this)).filter(Objects::nonNull).toArray();
return values.length > 0 ? Objects.hash(values) : super.hashCode();
}
/**
* Compares by default by entity class (proxies taken into account) and ID.
*/
@Override
public boolean equals(final Object other) {
return equals(other, BaseEntity::getId);
}
@SafeVarargs
@SuppressWarnings("unchecked")
protected final > boolean equals(final Object other, final Function... getters) {
if (other == this) {
return true;
}
if (!getClass().isInstance(other) && !other.getClass().isInstance(this)) {
return false;
}
return stream(getters).map(getter -> Objects.equals(getters[0].apply((E) this), getters[0].apply((E) other))).allMatch(b -> b);
}
/**
* Orders by default with "nulls last".
*/
@Override
public int compareTo(final BaseEntity other) {
return compareTo(other, BaseEntity::getId);
}
@SafeVarargs
@SuppressWarnings({ "unchecked", "rawtypes" })
protected final > int compareTo(final Object other, final Function... getters) {
if (other == null) {
return -1;
}
Comparator
© 2015 - 2024 Weber Informatics LLC | Privacy Policy