Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>org.omnifaces</groupId>
<artifactId>omnipersistence</artifactId>
<version>1.3-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>OmniPersistence</name>
Expand Down Expand Up @@ -80,7 +80,7 @@
<test.glassfish.eclipselink.version>5.0.2</test.glassfish.eclipselink.version> <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink --> <!-- NOTE: This must be EclipseLink version bundled in test.glassfish.version -->
<test.tomee.version>10.2.0</test.tomee.version> <!-- https://mvnrepository.com/artifact/org.apache.tomee/apache-tomee -->
<test.tomee.openjpa.version>4.1.1</test.tomee.openjpa.version> <!-- https://mvnrepository.com/artifact/org.apache.openjpa/openjpa-all --> <!-- NOTE: This must be OpenJPA version bundled in test.tomee.version -->
<test.h2.version>2.4.240</test.h2.version> <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<test.h2.version>2.5.252</test.h2.version> <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/omnifaces/persistence/JPA.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public static Expression<String> concat(CriteriaBuilder builder, Object... expre
* @param builder The involved criteria builder.
* @param expression Expression to be lowercased.
* @return A new expression wherein given expression is lowercased.
* @since 1.3
* @since 2.0
*/
public static Expression<String> lower(CriteriaBuilder builder, Expression<String> expression) {
return builder.function("LOWER", String.class, expression);
Expand Down
35 changes: 1 addition & 34 deletions src/main/java/org/omnifaces/persistence/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static jakarta.persistence.metamodel.Attribute.PersistentAttributeType.MANY_TO_ONE;
import static jakarta.persistence.metamodel.Attribute.PersistentAttributeType.ONE_TO_MANY;
import static jakarta.persistence.metamodel.Attribute.PersistentAttributeType.ONE_TO_ONE;
import static java.lang.String.format;
import static org.omnifaces.persistence.JPA.QUERY_HINT_CACHE_RETRIEVE_MODE;
import static org.omnifaces.persistence.JPA.QUERY_HINT_CACHE_STORE_MODE;
import static org.omnifaces.utils.Collections.unmodifiableSet;
Expand All @@ -31,7 +30,6 @@
import java.util.Set;
import java.util.function.Function;

import jakarta.annotation.Nullable;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.ElementCollection;
Expand All @@ -46,7 +44,6 @@
import jakarta.persistence.metamodel.Bindable;
import jakarta.persistence.metamodel.SingularAttribute;

import org.omnifaces.persistence.model.Identifiable;
import org.omnifaces.persistence.service.BaseEntityService;

/**
Expand Down Expand Up @@ -106,12 +103,6 @@ public <E> Class<E> getEntityType(E entity) {
return invokeOnProxy(entity, "getPersistentClass", super::getEntityType);
}

@Override
@Nullable
public <I> I getIdentifier(@Nullable Object entity) {
return invokeOnProxy(entity, "getIdentifier", super::getIdentifier);
}

@Override
public void configureSecondLevelCache(Query query, boolean cacheable) {
query.setHint(QUERY_HINT_HIBERNATE_CACHEABLE, cacheable);
Expand Down Expand Up @@ -216,7 +207,6 @@ public void configureSecondLevelCache(Query query, boolean cacheable) {
);
private static final Optional<Class<Object>> OPENJPA_QUERY_IMPL = findClass("org.apache.openjpa.persistence.OpenJPAQuery");
private static final Set<String> AGGREGATE_FUNCTIONS = unmodifiableSet("MIN", "MAX", "SUM", "AVG", "COUNT");
private static final String ERROR_NOT_IDENTIFIABLE = "%s is not a proxy and does not implement Identifiable";

private static Object unwrapEntityManagerFactoryIfNecessary(EntityManagerFactory entityManagerFactory) {
var packageName = entityManagerFactory.getClass().getPackage().getName();
Expand Down Expand Up @@ -364,29 +354,6 @@ public <E> Class<E> getEntityType(E entity) {
return (Class<E>) findEntityClass(entity.getClass()).orElse(entity.getClass());
}

/**
* Returns the identifier of the given entity. If it is a proxy, then the identifier will be extracted from the proxy. The default implementation returns
* {@link Identifiable#getId()}.
*
* @param <I> The generic ID type.
* @param entity The entity to get the identifier for, may be <code>null</code>.
* @return The identifier of the given entity, or <code>null</code> when <code>null</code> was supplied or when the entity has no ID.
* @throws IllegalArgumentException When the given entity is not a proxy and does not implement {@link Identifiable}.
*/
@Nullable
@SuppressWarnings({ "unchecked", "deprecation", "removal" })
public <I> I getIdentifier(@Nullable Object entity) {
if (entity == null) {
return null;
}

if (!(entity instanceof Identifiable<?> identifiable)) {
throw new IllegalArgumentException(format(ERROR_NOT_IDENTIFIABLE, entity.getClass()));
}

return (I) identifiable.getId();
}

/**
* Returns the table name of the given entity. The default implementation returns the name of the {@link Table} annotation of {@link #getEntityType(Object)}
* or else defaults to its {@link #getEntityName(Class) entity name} in upper cased form when there is no such annotation or it has no name.
Expand All @@ -410,7 +377,7 @@ public String getTableName(Object entity) {
* @return The Jakarta Persistence entity name of the given entity type.
* @throws NullPointerException When given entity type is <code>null</code>.
* @see BaseEntityService#getEntityName()
* @since 1.3
* @since 2.0
*/
public static String getEntityName(Class<?> entityType) {
return findEntityClass(entityType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@
*/
@MappedSuperclass
@EntityListeners(BaseEntityListener.class)
@SuppressWarnings({"deprecation", "removal"})
public abstract class BaseEntity<I extends Comparable<I> & Serializable> implements Comparable<BaseEntity<I>>, Identifiable<I>, Serializable {
public abstract class BaseEntity<I extends Comparable<I> & Serializable> implements Comparable<BaseEntity<I>>, Serializable {

private static final long serialVersionUID = 1L;

Expand All @@ -119,7 +118,6 @@ protected BaseEntity() {
*
* @return The ID, or <code>null</code> if not set yet, e.g. when the entity has a generated ID and is not persisted yet.
*/
@Override
@Nullable
public abstract I getId();

Expand All @@ -128,7 +126,6 @@ protected BaseEntity() {
*
* @param id The ID, may be <code>null</code>.
*/
@Override
public abstract void setId(@Nullable I id);

/**
Expand Down
83 changes: 0 additions & 83 deletions src/main/java/org/omnifaces/persistence/model/Identifiable.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/org/omnifaces/persistence/model/dto/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public final class Page implements Serializable { // This class MAY NOT be mutab
* attribute is named <code>code</code> by its code. The actual name of the ID attribute works as well. A page without ordering is ordered by the ID
* descending.
*
* @since 1.3
* @since 2.0
*/
public static final String ID = "id";

Expand Down Expand Up @@ -253,7 +253,7 @@ public Map<String, Object> getLastOrderingValues() {
* applicable.
*
* @return The amount of records to skip after the record represented by the last ordering values.
* @since 1.3
* @since 2.0
*/
public int getResidualOffset() {
return residualOffset;
Expand Down Expand Up @@ -484,7 +484,7 @@ public Builder range(int offset, int limit, Map<String, Object> lastOrderingValu
* @return This builder.
* @see Page#getLastOrderingValues()
* @see Page#getResidualOffset()
* @since 1.3
* @since 2.0
*/
public Builder range(int offset, int limit, Map<String, Object> lastOrderingValues, int residualOffset, boolean reversed) {
checkRangeNotSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ protected EntityType<E> getMetamodel() {
* Returns the entity name of current entity, to be used in JPQL. This is the name of {@link Entity}, or else the simple name of the entity class.
*
* @return The entity name of current entity.
* @since 1.3
* @since 2.0
*/
protected String getEntityName() {
return getMetamodel().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.PersistenceUnitUtil;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.EntityType;
import jakarta.persistence.metamodel.Metamodel;
Expand Down Expand Up @@ -115,7 +116,7 @@ Mappings getMappings(EntityManagerFactory entityManagerFactory, Provider provide
var metamodel = entityManagerFactory.getMetamodel();
return new Mappings(
mappingsPerFactory.computeIfAbsent(entityManagerFactory, k -> new FactoryMappings()),
UNIT_MAPPINGS.computeIfAbsent(entityManagerFactory, k -> new UnitMappings(metamodel)), metamodel, provider
UNIT_MAPPINGS.computeIfAbsent(entityManagerFactory, k -> new UnitMappings(metamodel)), entityManagerFactory, metamodel, provider
);
}

Expand All @@ -127,12 +128,17 @@ final class Mappings {

private final FactoryMappings factoryMappings;
private final UnitMappings unitMappings;
private final EntityManagerFactory entityManagerFactory;
private final Metamodel metamodel;
private final Provider provider;

private Mappings(FactoryMappings factoryMappings, UnitMappings unitMappings, Metamodel metamodel, Provider provider) {
private Mappings(
FactoryMappings factoryMappings, UnitMappings unitMappings, EntityManagerFactory entityManagerFactory, Metamodel metamodel, Provider provider
)
{
this.factoryMappings = factoryMappings;
this.unitMappings = unitMappings;
this.entityManagerFactory = entityManagerFactory;
this.metamodel = metamodel;
this.provider = provider;
}
Expand All @@ -159,8 +165,8 @@ IdAttribute getIdAttribute(Class<?> type) {
}

/**
* Returns the ID of the given entity of this persistence unit: via the provider when it is a proxy, else via {@link BaseEntity#getId()} when it extends
* it, else via its ID attribute.
* Returns the ID of the given entity of this persistence unit: via {@link PersistenceUnitUtil#getIdentifier(Object)} when it is a proxy, which does not
* initialize the proxy, else via {@link BaseEntity#getId()} when it extends it, else via its ID attribute.
*
* @param <I> The generic ID type.
* @param entity The entity, may be a proxy.
Expand All @@ -170,7 +176,7 @@ IdAttribute getIdAttribute(Class<?> type) {
@SuppressWarnings("unchecked")
<I> I getId(Object entity) {
if (provider.isProxy(entity)) {
return provider.getIdentifier(entity);
return (I) entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity);
}

return entity instanceof BaseEntity<?> baseEntity ? (I) baseEntity.getId() : getIdAttribute(provider.getEntityType(entity)).get(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
* @param <I> The generic ID type.
* @param <E> The generic entity type.
* @see BaseEntityService
* @since 1.3
* @since 2.0
*/
public final class EntityRepository<I extends Comparable<I> & Serializable, E> implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* does nothing.
*
* @author Bauke Scholtz
* @since 1.3
* @since 2.0
*/
public final class EntityRepositoryExtension implements Extension {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* join.
*
* @author Bauke Scholtz
* @since 1.3
* @since 2.0
* @see PostponedFetchRoot
*/
class SingularFetch<Z, X> extends FetchWrapper<Z, X> {
Expand Down
21 changes: 2 additions & 19 deletions src/test/java/org/omnifaces/persistence/ProviderEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,20 @@
package org.omnifaces.persistence;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.omnifaces.persistence.Provider.ECLIPSELINK;
import static org.omnifaces.persistence.Provider.HIBERNATE;
import static org.omnifaces.persistence.Provider.OPENJPA;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import org.junit.jupiter.api.Test;
import org.omnifaces.persistence.model.Identifiable;
import org.omnifaces.persistence.model.BaseEntity;

/**
* Unit tests for the entity methods of {@link Provider} on entities which do not implement {@link Identifiable}. This class lives outside the integration test
* Unit tests for the entity methods of {@link Provider} on entities which do not extend {@link BaseEntity}. This class lives outside the integration test
* package so that its stub entity does not end up in the persistence unit of the integration tests.
*/
class ProviderEntityTest {

@Test
void identifierOfNullIsNull() {
assertNull(HIBERNATE.getIdentifier(null));
}

@Test
void identifierOfNonProxyEntityNotImplementingIdentifiableIsRejected() {
var entity = new PlainEntity();
assertThrows(IllegalArgumentException.class, () -> HIBERNATE.getIdentifier(entity));
assertThrows(IllegalArgumentException.class, () -> ECLIPSELINK.getIdentifier(entity));
assertThrows(IllegalArgumentException.class, () -> OPENJPA.getIdentifier(entity));
}

@Test
void entityTypeOfSubclassIsEntityAnnotatedSuperclass() {
assertEquals(PlainEntity.class, HIBERNATE.getEntityType(new PlainEntitySubclass()));
Expand Down
Loading
Loading