Skip to content

Commit 78795c6

Browse files
committed
Less extensive test on proxy identifier access.
The upgrade to Hibernate 6.2 causes its proxy generation strategy to differ depending on execution context. We previously used a lazy many-to-one relationship to verify our proxy identifier logic as that relationship reliably resulted in a proxy instance being created on versions < 6.2. It looks like some optimizations in Hibernate [0] now cause it "optimize" the proxy away. I wasn't able to figure out the exact circumstances of that optimization but our tests running on CI reliably didn't see a proxy returned while local, standalone executions did. We now simply obtain a proxy through EntityManager.getReference(…) and verify that the identifier is still accessible, dropping the addition check for a non-initialization. [0] https://hibernate.atlassian.net/browse/HHH-15790 Related ticket: #2899.
1 parent b44816c commit 78795c6

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

spring-data-jpa/src/test/java/org/springframework/data/jpa/mapping/JpaMetamodelMappingContextIntegrationTests.java

+7-26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import jakarta.persistence.EntityManager;
21+
2022
import java.util.Collections;
2123

2224
import jakarta.persistence.EntityManager;
@@ -26,13 +28,11 @@
2628
import org.junit.jupiter.api.BeforeEach;
2729
import org.junit.jupiter.api.Test;
2830
import org.junit.jupiter.api.extension.ExtendWith;
29-
3031
import org.springframework.beans.factory.annotation.Autowired;
3132
import org.springframework.context.annotation.ComponentScan.Filter;
3233
import org.springframework.context.annotation.Configuration;
3334
import org.springframework.context.annotation.FilterType;
3435
import org.springframework.context.annotation.ImportResource;
35-
import org.springframework.data.jpa.domain.sample.Category;
3636
import org.springframework.data.jpa.domain.sample.OrmXmlEntity;
3737
import org.springframework.data.jpa.domain.sample.Product;
3838
import org.springframework.data.jpa.domain.sample.User;
@@ -43,8 +43,6 @@
4343
import org.springframework.data.mapping.PersistentPropertyPaths;
4444
import org.springframework.test.context.ContextConfiguration;
4545
import org.springframework.test.context.junit.jupiter.SpringExtension;
46-
import org.springframework.transaction.PlatformTransactionManager;
47-
import org.springframework.transaction.support.TransactionTemplate;
4846

4947
/**
5048
* Integration tests for {@link JpaMetamodelMappingContext}.
@@ -63,7 +61,6 @@ class JpaMetamodelMappingContextIntegrationTests {
6361
@Autowired ProductRepository products;
6462
@Autowired CategoryRepository categories;
6563
@Autowired EntityManager em;
66-
@Autowired PlatformTransactionManager transactionManager;
6764

6865
@BeforeEach
6966
void setUp() {
@@ -129,29 +126,13 @@ void detectsEntityPropertyForCollections() {
129126
@Test // DATAJPA-630
130127
void lookingUpIdentifierOfProxyDoesNotInitializeProxy() {
131128

132-
TransactionTemplate template = new TransactionTemplate(transactionManager);
133-
final Category category = template.execute(status -> {
134-
135-
Product product = products.save(new Product());
136-
return categories.save(new Category(product));
137-
});
138-
139-
template.execute(status -> {
129+
Product product = em.getReference(Product.class, 1L);
140130

141-
Category loaded = categories.findById(category.getId()).get();
142-
Product loadedProduct = loaded.getProduct();
131+
JpaPersistentEntity<?> entity = context.getRequiredPersistentEntity(Product.class);
132+
IdentifierAccessor accessor = entity.getIdentifierAccessor(product);
143133

144-
JpaPersistentEntity<?> entity = context.getRequiredPersistentEntity(Product.class);
145-
IdentifierAccessor accessor = entity.getIdentifierAccessor(loadedProduct);
146-
147-
assertThat(accessor.getIdentifier()).isEqualTo(category.getProduct().getId());
148-
assertThat(loadedProduct).isInstanceOf(HibernateProxy.class);
149-
assertThat(((HibernateProxy) loadedProduct).getHibernateLazyInitializer().isUninitialized()).isTrue();
150-
151-
status.setRollbackOnly();
152-
153-
return null;
154-
});
134+
assertThat(accessor.getIdentifier()).isEqualTo(1L);
135+
assertThat(product).isInstanceOf(HibernateProxy.class);
155136
}
156137

157138
/**

0 commit comments

Comments
 (0)