New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent Hibernate lazy loading #97
Comments
I haven't thought about this problem specifically, but there are probably a few ways you can approach it. One is to set a global Condition that basically only maps properties that are not PersistentSets (or whatever. More on conditions. |
Try with Condition, but I need the property 'parentSource'. It is with this property that I check if the object type is Javassist or persistentBag.
|
You can access the parent context and it's source via |
The method 'applies' the Condition class is call after the 'resolveSourceValue' method, and this method is the exception thrown on line 189
Entity root:
Entity Endereco
DTO
|
No consideration for this issue? |
You are right about the
Unfortunately right now you'd need to set this on each TypeMap you use. One solution is I could add support for a global |
Would be very good. The Jackson project has a module responsible for disable the lazy loading. You could use the same idea. |
hi, did this problem fixed? |
The ability to configure a global property condition was added a while back. I just committed a test that demonstrates skipping lazily fetched JPA properties using a condition. Basic usage (for Hibernate):
|
Thanks! it works. |
Hi there! I'm working on a project that aims to solve common JPA problems when mapping entities to DTOs using ModelMapper. This issue has already been solved on the project. Project link: JPA Model Mapper |
Using lambda:
|
Is there a way to do it selectively per modelmapper.map(..) execution? Not globally as discussed? I have a case that I leave the decision to API caller to request nested JPA objects. Let's say if an optional parameter is set to false nested object is null, if true API returns nested object as well. |
Well, I have the same problem. The proposed condition above didn't work for me. Without it, I get this error:
With the proposed solution, I still get the error:
|
Hi @micobarac , Can you provide reproducible code or more information? Thanks. |
/**
* An Account.
*/
@Entity
@Table(name = "account")
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(max = 100)
@Pattern(regexp = Constants.ACCOUNT_REGEX, message = Constants.ACCOUNT_MESSAGE)
@Column(name = "name", nullable = false)
private String name;
@ManyToOne
private Group group;
...
}
/**
* A Group.
*/
@Entity
@Table(name = "jhi_group")
public class Group implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(min = 1, max = 255)
@Pattern(regexp = Constants.NAME_REGEX, message = Constants.NAME_MESSAGE)
@Column(name = "name")
private String name;
@JsonIgnore
@ManyToMany(mappedBy = "groups")
private Set<Account> accounts = new HashSet<>();
@OneToMany(mappedBy = "group", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<GroupService> services = new HashSet<>();
...
} When I try to map Account i get from the database to a new Account (even though apply the proposed mapper condition), I get:
|
Have you tried the solution mentioned above? |
Do you have full stacktrace and example/reproducible code? |
No, unfortunatelly not. It's a huge project with big and interconnected entities. The point is that the solution above solves the problem of preventing mapper to map the persistent collections, but nevertheless, it triggers those lazy loaded getters somehow and throws the error of trying to initialize lazy loaded collection. |
I guessed the solution provided above skip the property, so the lazy loaded getters won't be called if it was configured correctly. I can't see how your code and how you configured modelmapper, I'm not sure how I can help to fix this problem. Maybe you can write a minimum reproducible code for this? |
I confirm the solution does not work for me either. It looks like the condition is called after the attempt to initialize the lazy loaded property. |
I debugged a bit and it looks to me the conditions are applied after Object source = resolveSourceValue(context, mapping);
MappingContextImpl<Object, Object> propertyContext = propertyContextFor(context, source,
mappingImpl);
Condition<Object, Object> condition = (Condition<Object, Object>) Objects.firstNonNull(
mapping.getCondition(),
context.getTypeMap().getPropertyCondition(),
configuration.getPropertyCondition());
if (condition != null) {
boolean conditionIsTrue = condition.applies(propertyContext);
if (conditionIsTrue && mapping.isSkipped()) // when(condition).skip()
return;
else if (!conditionIsTrue && !mapping.isSkipped()) { // when(condition)
context.shadePath(propertyPath);
return;
}
}
which is triggering the error:
Am I missing anything? |
Hi,
Is there any way to avoid Lazy Loading launched by hibernate? With the dozer can create a CustomFieldMapper that manipulates these values, but found the ModelMapper much simpler
The text was updated successfully, but these errors were encountered: