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
2 changes: 1 addition & 1 deletion boms/tomee-microprofile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>4.1.1</version>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
Expand Down
2 changes: 1 addition & 1 deletion boms/tomee-plume/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>4.1.1</version>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
Expand Down
2 changes: 1 addition & 1 deletion boms/tomee-plus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>4.1.1</version>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
Expand Down
2 changes: 1 addition & 1 deletion boms/tomee-webprofile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>4.1.1</version>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,8 @@ private <T> T handleUpdate(final EntityManager em, final Object[] args) {
}

/**
* Performs an update with version checking. The strategy depends on the JPA provider:
* <ul>
* <li>Hibernate/EclipseLink: standard {@code em.merge()} handles @Version correctly</li>
* <li>OpenJPA: requires a workaround — reflection-based field copying with
* OPTIMISTIC_FORCE_INCREMENT because OpenJPA restricts setting @Version fields</li>
* </ul>
* Performs an update with version checking: verifies the entity exists and its
* {@code @Version} matches before delegating to {@code em.merge()}.
*/
private Object mergeWithVersionCheck(final EntityManager em, final Object entity) {
final Object id = getEntityId(em, entity);
Expand All @@ -351,57 +347,8 @@ private Object mergeWithVersionCheck(final EntityManager em, final Object entity
"Entity not found for update: " + entity);
}

// Check version before merge
checkVersionMatch(em, entity, existing);

// Providers other than OpenJPA handle @Version correctly via em.merge()
if (!isOpenJpa(em)) {
return em.merge(entity);
}

// OpenJPA workaround: copy fields via reflection, force version increment
try {
final jakarta.persistence.metamodel.EntityType<?> entityType =
em.getMetamodel().entity(entity.getClass());

for (final jakarta.persistence.metamodel.Attribute<?, ?> attr : entityType.getAttributes()) {
if (attr instanceof jakarta.persistence.metamodel.SingularAttribute<?, ?> sa) {
if (sa.isId() || sa.isVersion()) {
continue;
}
}
final java.lang.reflect.Field field = findField(entity.getClass(), attr.getName());
field.setAccessible(true);
final java.lang.reflect.Field existingField = findField(existing.getClass(), attr.getName());
existingField.setAccessible(true);
existingField.set(existing, field.get(entity));
}
em.lock(existing, jakarta.persistence.LockModeType.OPTIMISTIC_FORCE_INCREMENT);
em.flush();
em.refresh(existing);
// Copy updated fields (including version) back to the input entity
for (final jakarta.persistence.metamodel.Attribute<?, ?> attr : entityType.getAttributes()) {
try {
final java.lang.reflect.Field f = findField(entity.getClass(), attr.getName());
f.setAccessible(true);
final java.lang.reflect.Field ef = findField(existing.getClass(), attr.getName());
ef.setAccessible(true);
f.set(entity, ef.get(existing));
} catch (final NoSuchFieldException ignored) {
// skip
}
}
return entity;
} catch (final jakarta.data.exceptions.OptimisticLockingFailureException e) {
throw e;
} catch (final Exception e) {
return em.merge(entity);
}
}

private static boolean isOpenJpa(final EntityManager em) {
return em.getClass().getName().startsWith("org.apache.openjpa.")
|| em.getDelegate().getClass().getName().startsWith("org.apache.openjpa.");
return em.merge(entity);
}

private Object handleFindById(final EntityManager em, final Method method, final Object[] args) {
Expand Down Expand Up @@ -640,18 +587,6 @@ private void removeEntity(final EntityManager em, final Object entity) {
}
}

private static java.lang.reflect.Field findField(final Class<?> clazz, final String name) throws NoSuchFieldException {
Class<?> current = clazz;
while (current != null) {
try {
return current.getDeclaredField(name);
} catch (final NoSuchFieldException e) {
current = current.getSuperclass();
}
}
throw new NoSuchFieldException(name);
}

/**
* Checks if a deleteById method is a custom one declared on the repository (with @By annotations
* pointing to real fields) vs the inherited CrudRepository.deleteById (which may have synthetic @By).
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
<version.geronimo-mail_2.1_provider>1.1.1</version.geronimo-mail_2.1_provider>
<version.johnzon>2.2.0</version.johnzon>
<version.myfaces>4.1.4</version.myfaces>
<version.openjpa>4.1.1</version.openjpa>
<version.openjpa>4.2.0</version.openjpa>
<version.openwebbeans>4.1.1</version.openwebbeans>
<!-- org.eclipse -->
<version.eclipselink>5.0.2</version.eclipselink>
Expand Down