I using Hibernate/JPA and I need to fetch an object without its relationship/children How can I do that? The questions is not about to use Lazy or Eager strategy because in both case the children are attached to the object, initializate or not.
Ex: I have a obj A with a onetomany relationship to B. I want to fetch a list of A without the B attached to it.
Thanks
You can use DTO pattern which is desirable in RESTfull services.
Be sure to have only fields that you need and you can use spring ModelMapper to convert entity to dto.
http://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application
..or implement builder (lombok) or populator/converter pattern
Lazy loading will not fetch the child records until otherwise you call for the child record's getter method. So you can use this in your case.
Note: In lazy loading child record will not be attached unless otherwise it's getter is invoked.
Related
Have an entity with a OneToMany relationship in Spring Data that I would need to filter by a specific column. I've been unable to find a satisfying solution to get it with a single query (both parent entity and the filtered OneToMany in one go) so I'm now thinking in having it lazy fetched and after getting the parent entity, have a separate query to retrieve the linked entities and just using a setter to link them together (ie: parent.setChildren(childrenRepository.findAllBy...)).
Question is: would this effectively disable any future trigger to the lazy fetch so my manually set children would remain? Use case here is to add these children manually in certain cases, and using the default lazy fetch when the conditions don't apply.
I tested this and seems to be working as intended, but could not find information on when the lazy fetch should be triggered as to make sure my logic would work in any scenario (is it disabled as soon as the setter is invoked? is this documented anywhere?)
Thanks in advance.
We are using JPA + Hibernate.
I have some Many-to-one mappings which are lazy loaded.
In Service, I Initiallize the Many-to-one objects by calling their getter method. but proxy gets assigned to parent VO and not actual VO Object.
My Question is, Is there any way in JPA to force to use no proxy Strategy.
My limitation here is i cant use Hibernate Objects or annotaions like #LazytoOne etc.
thanks in advance.
You cannot prevent Hibernate from using proxy objects there due to the fact that somehow it has to guarantee it's a lazy relation.
You have multiple choices:
Trigger the initialization Hibernate.initialize(parent.getChild()). Note that this is not the best way to do it and this also requires an active transaction.
Fetch the relation when fetching the entity itself. This can be done with the Fetch Joins. JPQL/HQL/Criteria API are capable of doing this.
Use read-only projections which contains only the data you need. For this particular case you can use Spring Data JPA as it comes with such a feature.
I suggest you to go with either option 2 or 3 as they are the most effective ways to do this.
Furher reading about lazy-loading here.
I am using Apache CXF with Spring and Dozer Mapper to convert DTOs (Database Objects) in to models. For Hibernate side I have enabled lazy="extra" and lazy="true" for mapping and which works fine hibernate loads child whenever respective getter methods are called. But when I convert DTO using dozer mapper it calls getter methods of all the child object in some case it was not necessary but in some it is not necessary. Is there any way by using which I can reduce overheads.
If you convert entities to DTOs, you have two options:
do not use lazy collections
ignore the collections when serializing, if you don't want them in the output
Fetch(load) child's always in every call results to heavy process and
unnecessary data to persist. So instead of doing this always fetch
data as lazy and initialize child's model whenever there is
need. This is the best way to fetch data.
I have one to many association in my dto,
Parent DTO : Question
Child DTO : History
Question 1:
Means One question record has many history records, I am using JPA2.x with hibernate,
I bound the entities with lazy fetching method/mode. some places I want the lazy fetching...
and some places I don't want the lazy fetching not even egar. Means I don't want the data collection itself. I need only the parent class list, even if I call getHistories() method, it should not do lazy fetch in few places.... How to use entityManager to avoid lazy fetching.....even though the collection was set to lazy fetch mode.....?
Question 2:
I need to encrypt the question using mysql encode/decode functions. when inserting encrypt the value and save. when selecting decode the content.....
Solution can be in mapping or criteria query...?
Thanks In Advance....!
Question 1:
Your requirement makes no sense. If you don't want to access the history, don't call getHistories(). If you access the collection of History, either your Question is attached, and the collection will be loaded, or it's detached, and you'll get a lazy loading exception.
Question 2:
See http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/annotations/ColumnTransformer.html. The example in the javadoc is precisely what you want to do.
For question 1, you need to do two things,
If you want your parent-child entities to be selected 'lazily', then annotate the bir-directional fields with fetch = lazy. This should work well in the hibernate session, as long as you dont load or 'touch' one of the lazy loaded fields.
If you want your parent-child entities to be selected 'eagerly', then anotate the fields in the bi-directional association as fetch = eager.
If you want 'some places I don't want the lazy fetching not even egar', then you have to decide at code time, whether to have a relationship between the parent-child entity, if you don't, then just dont map the bi-directional association. You CAN create more than one entity class to map different assoications, even entities with no association.
am using the Session.refreshObject(Object o) method to fresh an object that has a getList() method. This list is provided by toplink using a one to many mapping. When I refresh the object, I want this list to be refreshed too. Is this possible or I have to refresh something else?
Thanks in Advance!
refreshObject is equivalent to calling ReadObjectQuery.
If you want to refresh objects in cascade you can use ReadObjectQuery.cascadePrivateParts(). According to the doc this method
Cascade the query and its properties on the queries object(s) and all
privately owned objects related to the queries object(s)
.
I depends on the mapping of the entity in the list. But if you want to actually refresh those objects you must do a
refreshObject(o.getList());