-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from croz-ltd/feature_returniInitializedEntit…
…yOnRegistryCreateAndUpdate Avoid lazy init exception in DefaultRegistryDataService
- Loading branch information
Showing
7 changed files
with
322 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
nrich-registry/src/main/java/net/croz/nrich/registry/data/util/HibernateUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the original author or authors. | ||
* | ||
* 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 net.croz.nrich.registry.data.util; | ||
|
||
import lombok.SneakyThrows; | ||
import org.hibernate.Hibernate; | ||
import org.hibernate.proxy.HibernateProxy; | ||
import org.springframework.beans.BeanUtils; | ||
|
||
import jakarta.persistence.Entity; | ||
import java.beans.PropertyDescriptor; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public final class HibernateUtil { | ||
|
||
private static final String PROPERTY_PATH_FORMAT = "%s.%s"; | ||
|
||
private static final String COLLECTION_ELEMENT_NAME_FORMAT = "%s-%s"; | ||
|
||
private HibernateUtil() { | ||
} | ||
|
||
public static void initialize(Object entity) { | ||
initializeInternal(entity, null, new ArrayList<>()); | ||
} | ||
|
||
private static void initializeInternal(Object entity, String propertyPath, List<String> alreadyInitializedProperties) { | ||
if (entity == null || alreadyInitializedProperties.contains(propertyPath)) { | ||
return; | ||
} | ||
|
||
Class<?> entityType = resolveEntityType(entity); | ||
|
||
if (!isManagedType(entityType)) { | ||
return; | ||
} | ||
|
||
if (!Hibernate.isInitialized(entity)) { | ||
Hibernate.initialize(entity); | ||
} | ||
|
||
alreadyInitializedProperties.add(propertyPath); | ||
|
||
Arrays.stream(BeanUtils.getPropertyDescriptors(entityType)).forEach(propertyDescriptor -> { | ||
String propertyName = propertyDescriptor.getName(); | ||
Object propertyValue = getPropertyValue(entity, propertyDescriptor); | ||
|
||
if (propertyValue instanceof Collection<?> collection) { | ||
int index = 0; | ||
for (Object collectionElementValue : collection) { | ||
String collectionElementPropertyName = String.format(COLLECTION_ELEMENT_NAME_FORMAT, propertyName, index++); | ||
String calculatedPropertyPath = calculatePropertyPath(propertyPath, collectionElementPropertyName); | ||
|
||
initializeInternal(collectionElementValue, calculatedPropertyPath, alreadyInitializedProperties); | ||
} | ||
} | ||
else { | ||
String calculatedPropertyPath = calculatePropertyPath(propertyPath, propertyName); | ||
|
||
initializeInternal(propertyValue, calculatedPropertyPath, alreadyInitializedProperties); | ||
} | ||
}); | ||
} | ||
|
||
@SneakyThrows | ||
private static Object getPropertyValue(Object entity, PropertyDescriptor propertyDescriptor) { | ||
Method method = propertyDescriptor.getReadMethod(); | ||
|
||
if (method == null) { | ||
return null; | ||
} | ||
|
||
return method.invoke(entity); | ||
} | ||
|
||
private static Class<?> resolveEntityType(Object entity) { | ||
Class<?> type = entity.getClass(); | ||
|
||
if (entity instanceof HibernateProxy hibernateProxy) { | ||
type = hibernateProxy.getHibernateLazyInitializer().getPersistentClass(); | ||
} | ||
|
||
return type; | ||
} | ||
|
||
private static boolean isManagedType(Class<?> entityType) { | ||
return entityType.getAnnotation(Entity.class) != null; | ||
} | ||
|
||
private static String calculatePropertyPath(String existingPropertyPath, String propertyName) { | ||
return existingPropertyPath == null ? propertyName : String.format(PROPERTY_PATH_FORMAT, existingPropertyPath, propertyName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
nrich-registry/src/test/java/net/croz/nrich/registry/data/util/HibernateUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the original author or authors. | ||
* | ||
* 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 net.croz.nrich.registry.data.util; | ||
|
||
import net.croz.nrich.registry.RegistryTestConfiguration; | ||
import net.croz.nrich.registry.data.util.stub.HibernateUtilTestEntity; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
|
||
import static net.croz.nrich.registry.data.util.testutil.HibernateUtilGeneratingUtil.createAndSaveHibernateUtilTestEntity; | ||
import static net.croz.nrich.registry.testutil.PersistenceTestUtil.executeInTransaction; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.catchException; | ||
|
||
@SpringJUnitWebConfig(RegistryTestConfiguration.class) | ||
class HibernateUtilTest { | ||
|
||
@Autowired | ||
private PlatformTransactionManager transactionManager; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Test | ||
void shouldInitializeEntity() { | ||
// given | ||
HibernateUtilTestEntity entity = executeInTransaction(transactionManager, () -> createAndSaveHibernateUtilTestEntity(entityManager)); | ||
|
||
// when | ||
HibernateUtilTestEntity loadedEntity = loadAndInitializeEntity(entity.getId()); | ||
|
||
// and when | ||
Exception exception = catchException(() -> { | ||
loadedEntity.getParent().getId(); | ||
loadedEntity.getChildren().get(0).getId(); | ||
}); | ||
|
||
// then | ||
assertThat(exception).isNull(); | ||
} | ||
|
||
private HibernateUtilTestEntity loadAndInitializeEntity(Long id) { | ||
return executeInTransaction(transactionManager, () -> { | ||
HibernateUtilTestEntity loadedEntity = entityManager.find(HibernateUtilTestEntity.class, id); | ||
|
||
HibernateUtil.initialize(loadedEntity); | ||
|
||
return loadedEntity; | ||
}); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...egistry/src/test/java/net/croz/nrich/registry/data/util/stub/HibernateUtilTestEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the original author or authors. | ||
* | ||
* 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 net.croz.nrich.registry.data.util.stub; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
import java.util.List; | ||
|
||
@Setter | ||
@Getter | ||
@Entity | ||
public class HibernateUtilTestEntity { | ||
|
||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
@Id | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private HibernateUtilTestEntity parent; | ||
|
||
@OneToMany(fetch = FetchType.LAZY) | ||
private List<HibernateUtilTestEntity> children; | ||
|
||
@Getter(AccessLevel.NONE) | ||
private String hiddenName; | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...src/test/java/net/croz/nrich/registry/data/util/testutil/HibernateUtilGeneratingUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the original author or authors. | ||
* | ||
* 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 net.croz.nrich.registry.data.util.testutil; | ||
|
||
import net.croz.nrich.registry.data.util.stub.HibernateUtilTestEntity; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import java.util.List; | ||
|
||
public final class HibernateUtilGeneratingUtil { | ||
|
||
private HibernateUtilGeneratingUtil() { | ||
} | ||
|
||
public static HibernateUtilTestEntity createAndSaveHibernateUtilTestEntity(EntityManager entityManager) { | ||
HibernateUtilTestEntity firstChild = createHibernateUtilTestEntityInternal(entityManager, "first", null, null); | ||
HibernateUtilTestEntity secondChild = createHibernateUtilTestEntityInternal(entityManager, "second", null, null); | ||
HibernateUtilTestEntity parent = createHibernateUtilTestEntityInternal(entityManager, "parent", null, null); | ||
|
||
return createHibernateUtilTestEntityInternal(entityManager, "main", parent, List.of(firstChild, secondChild)); | ||
} | ||
|
||
private static HibernateUtilTestEntity createHibernateUtilTestEntityInternal(EntityManager entityManager, String name, HibernateUtilTestEntity parent, List<HibernateUtilTestEntity> children) { | ||
HibernateUtilTestEntity entity = new HibernateUtilTestEntity(); | ||
|
||
entity.setName(name); | ||
entity.setParent(parent); | ||
entity.setChildren(children); | ||
|
||
entityManager.persist(entity); | ||
|
||
return entity; | ||
} | ||
} |