how does toplink session.refreshObject() handle one to many mappings? - java

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());

Related

Spring Boot Data Problem on save entity on element to list changes

As I was updating a list of something under JSONB (postgresql) I noticed that a change to a JSONB list (which is materialized by a list of DTO in Java #Entity, and its #Type annotation over...) was not persisted though an element of the list was updated.
Nevertheless, changing the reference to that list with new ArrayList<>(myPreviousList) lead to persisting the changes in database.
I think the problem is not that linked to the database implementation... but more to the orm bridge, that thinks that a list of element with same references is not to be changed. Or I missed something perhaps?
Using Spring-Boot v2.0.6
EDIT: the changes were actually into a nested list of that list above, but I think the problem still remains :D
EDIT: Actually it's when I update another column of that table that the save is effective. And it also a JSONB type
Please advise
Best regards
Problem solved as it was a nested object that override equals() method and so the entity property was always considered not changed.
Thank you for your help all

Fetch an object without its relationships/children using Hibernate/JPA

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.

Strange initialization of lazy object in JPA

Well, i have class named "Consultation" that have a list of "ItemBudget", and i have a class named "Budget" that have a list of "ItemOrcamento" too. The relation Consultation<-->ItemBudget is ManyToMany, and relation Budget<-->ItemBudget is OneToMany.
In JSF i do the following:
<p:dataTable rowKey="#{item.id}" var="item" value="#{consultationMB.consultation.budget.itensAsList}" selection="#{consultationMB.itemBudget}" >
I use method "getItensAsList" that return a ArrayList() instead of a HashSet() that primefaces dataTable tag can't read correctly
As you can see, my selection is "itemBudget", so in my ManagedBean called ConsultationMBImpl i try to execute the following:
if (!itemBudget.getSituation().getCode().equals("WAITING_CONCLUSION")){
//some code here
}
When i try make the code above all fields that have reference to another class like: Situation, Dentist and others have this: "Dentist_javassist_32", "Situation_javassist_49"...
And all fields are null or zero.
That's caused by Hibernate, which loads a proxy (suffixed with _javassist) instead of the original object. If you debug your code with an IDE and try to call a getter on the fly, you'll get the real value, even it seems to be null for the property.
Why is it that done? Because it's much faster to load proxies rather than real objects for the ORM tool. Hibernate keeps a cache with already loaded objects instead of hitting the DB once and again.
If you want to avoid lazy loading, you could use a get instead of a load method over Hibernate's Session. Also for its relations, you can mark them as lazy="false", so Hibernate will load them as real objects. If you want to directly unproxy an already loaded instance, there are also some methods to achieve that.
However, don't do that unless you strictly require it. As I said before, that will make Hibernate load more information from DB and consequently, loose eficiency.

Hibernate SaveOrUpdate with Delete

I am attempting to delete an item from a collection in a Hibernate Java object using the saveOrUpdate function on the parent object. Update and Inserts work properly, but objects are not Deleted properly. Does saveOrUpdate() have the ability to recognize and delete objects that have been removed from a parent's set?
As a side note, I have mappers that map from DB -> hibernate java object -> domain object, and the domain object is kept in session. Do I need to keep the hibernate java object in session for this to work properly?
UPDATE (ANSWERED): I just ended up using merge() instead of saveOrUpdate(). Merge called DELETE when necessary without having to store the java hibernate object in session.
You need to add delete-orphan to the mapping. This will tell hibernate to delete 'orphaned' objects from a one to many relationship. Here's a link to the specific item in the documentation.
You're looking for "delete-orphan". Check out the reference guide on parent-child relationships and the annotations guide for the annotation syntax for it.

Difference between HQL delete query and session.delete()

I'm quite new to Hibernate and have a question. What is the difference between deleting an object by using an HQL query and deleting an object by using the delete(...) Method of the Session Class?
Session.delete(...) is only useful if you already have a reference to the entity you want to delete.
delete-by-query is useful for deleting several objects according to certain criteria, objects that you may not have previously loaded into the session.
I believe that delete-by-query actually loads each entity into the session and deletes them individually - someone correct me if I'm wrong on this.

Categories