Collection not updating upon saving opposite entity using Spring Data Jpa - spring

I have two entities that are in a one-to-many relationship:
Parent entity:
#OneToMany(mappedBy = "parent")
public List<Child> getChildren()
Child entity:
#ManyToOne
#JoinColumn(name = "PARENT_ID")
public Parent getParent()
Consider the following code (inside transaction):
Child child = childRepository.findById(id).get();
Parent parent = child.getParent();
child.setParent(null);
childRepository.saveAndFlush(child);
List<Child> children = parent.getChildren();
In this case the 'children' list will still contain the child entity although it is already removed. I tried flushing the repositories, saving the parent entity or even getting a new one from the parentRepository, none of these worked.
Why is the children list not updated upon save and how can I make sure the collection is up-to-date without explicitly removing the entity (I want to make further operations on the entities in the collection)?

Related

Save either a new or detached child object using Spring JPA and Hibernate

I have a class like this:
#Entity
#Table(name = "parent")
class Parent{
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "child")
private Child child;
}
The child class and therefore the child table has no references to Parent. Now the problem is that I have code like:
//Brand new transient parent created
Parent parent = new Parent();
//Either find child in the DB using the attributes or create a brand new one
Child child = createOrFindChild(childAttributes)
//Set child in parent
parent.setChild();
parentRepository.save(parent)
The problem here is that if I don't find the child and try to save I get the error:
"object references an unsaved transient instance - save the transient instance before flushing"
If I set CascadeType.PERSIST then it works when a brand new child but when I find the child in the DB and it is detached then I get the error:
"detached entity passed to persist".
Anyway to make it work without explicitly saving child first?
You can use CascadeType.MERGE and use EntityManager#merge in your repository.

JPA - How to efficiently get all children of a parent by JPA?

Having a parent and child class as below
#Entity
class Parent {
#Id
Long id;
#OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
List<Child> children;
}
#Embeddable
class Child{
#Id
Long id;
// child does not have parent id
}
I am using ObjectDB and JPA. My db got bigger and some parents has 500K children.
Normally, to get all children of a parent, I was loading parent and accessing children as parent.getChildren() via lazy loading.
However, since the list too big, it requires a lot of memory.
How can I get all children of a specific parent as a lightweight DTO object list in a performant way?
Bonus question: how can I delete all children of a parent efficiently?
Setting Child as embeddable is a possible solution. Note that in that case no need for an id field, which is unused and just consumes space. You can delete the embeddable children by clearing the collection.
To improve performance you may need a different design in which not all the 500K are loaded each time together.

Spring/Hibernate/JPA: Prevent reassigning child to another parent

I am working in a SpringBoot application with Hibernate that exposes a REST interface.
The Problem
Users are able to create objects, let's name them parent. Users can associate the parent object with several child items. These items are also composed of other objects, but lets keep it simple for now. I want to prevent the case that a client can do the following:
create parent1 and create child1, child2
create parent2 and create child3
update parent1 with child3 in payload
This behaviour should not be possible as the child items should not be updatable when they belong to another parent.
My goal is to make it impossible to update parent and update any child that belongs to a different parent.
The model
#Entity(name = "Parent")
#Table(name = "t_parent")
data class Parent(
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int = 0,
#JoinColumn(name = "fk_parent")
#OneToMany(orphanRemoval = true, cascade = [CascadeType.ALL])
val children: MutableList<Child> = mutableListOf()
)
#Table(name = "t_child")
#Entity(name = "Child")
data class Child(
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int = 0,
)
I am using Spring's JpaRepository for persistence.
Solutions?
The 'dumbest' way of doing this would be to fetch parent1 from the database and remove all child items from the client's request that are not contained in dbParent1. This is quite tedious and not really smart I guess.
Thanks for your advice.
What about transactions? In a transactional method you can check if a child belongs to another parent. If you find any obstacle for update/insert or whatever, then rollback the transaction (for example, just throw an exception). This methodology will require additional service layer in your archtecture since the code of repos is autogenerated from interface declarations.
This approach is described here: How to override save method of CrudRepository REST wise
I would do this in the following way for the update operation:
1) Fetch the parent with childId
2a) If there are no parents, then just add the child to the parent (new child)
2b) Otherwise, check if the child belongs to the parent (you can use parentId probably set in the #PathVariable
2b1) If not, throw a new IllegalArgumentException("wrong parent")
2b2) Otherwise, update parent

Why Entitys uninitialized collection is initialized automatically only for Entities persisted before current transaction?

(Please feel free to edit the title after reading this question)
I have quite simple #ManyToOne bidirectional mapping between entities Parent and Child.
The list of children Collection<Child> children in Parent is never initialized so it should be null.
When using EntityManager.find(...) for previously persisted Parent and then getting the list from that Parent gives ArrayList even there are no children yet with this Parent and it is fine.
However if persisting or merging a new Parent in the same transaction collection of children will be null even if the persisted/merged Parent is fetched again with EntityManager.find(...).
So i wonder this different behavior and if it is happening only in my environment.
I assume it has something to do with the caching of entities: entity is found from cache and it is returned instead of fetching it from db AND the initialization of empty collections will happen only when fetched from db, maybe depending on the JPA implementation.
Is my assumption even near the truth and if not what is the reason ?
Entities and test cases below. My test environment listed in tags.
// using lombok
#Slf4j
#RunWith(Arquillian.class)
public class NoPersistTest {
#PersistenceContext
private EntityManager em;
#Deployment
public static final WebArchive deploy() {
WebArchive wa = ShrinkWrap.create(WebArchive.class, "test.war")
.addAsWebInfResource("test-persistence.xml", "persistence.xml").addClasses(Parent.class, Child.class);
return wa;
}
#Test
#Transactional
public void testWithPreviouslyPersistedParent() {
Parent parent = em.find(Parent.class, 1); // has no children in db
// before
Child child = new Child();
child.setParent(parent);
parent.getChildren().add(child);
log.info("type of Collection<Child> is {}", parent.getChildren().getClass().getName());
// above logs "type of Collection<Child> is
// org.apache.openjpa.util.java$util$ArrayList$proxy"
}
#Test(expected = NullPointerException.class)
#Transactional
public void testPersistingParentInSameTransaction() {
Parent parent = new Parent();
em.persist(parent);
Parent parent2 = em.find(Parent.class, parent.getId());
Child child = new Child();
child.setParent(parent2);
log.info("Collection<Child> is {}", parent2.getChildren());
// above logs Collection<Child> is null
parent2.getChildren().add(child);
}
#Test(expected = NullPointerException.class)
#Transactional
public void testMergingParentInSameTransaction() {
Parent parent = new Parent();
parent = em.merge(parent);
Parent parent2 = em.find(Parent.class, parent.getId());
Child child = new Child();
child.setParent(parent2);
log.info("Collection<Child> is {}", parent2.getChildren());
// logs Collection<Child> is null
parent2.getChildren().add(child);
}
}
#Entity #Getter #Setter
public class Parent {
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#OneToMany(mappedBy="parent", cascade=CascadeType.ALL, orphanRemoval=true)
private Collection<Child> children;
private Date created = new Date(); // just to have something to persist
}
#Entity #Getter #Setter
public class Child {
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private Date created = new Date(); // just to have something to persist
#ManyToOne(optional=false)
private Parent parent;
}
If you create the Parent the collection is not initialized because you don't do it. And also when persisting the Parent JPA will leave the collection as it is.
But when you read the Parent with Hibernate the collection will contain a proxy because toMany relationships are fetched LAZY and this proxy is used to fetch the children on demand.
My recommendation is to always initialize collection to avoid NullPointerExceptions. That's good programming style.
The answer below is correct, I'd just like to add some more information as I was asked to in a comment elsewhere.
JPA uses caching to avoid database hits where possible, and where a database hit is still required, caching avoids the cost of rebuilding objects and allows maintaining Identity - ensuring you get back the same A instance when traversing A->B->A circular references.
When you persist an entity, you are placing it in the EntityManager cache as a managed entity - calling find on that EntityManager will return you the same exact instance you just passed in.
A initialA = new A();
A managedA = em.persist(initialA);
managedA==initialA
The persist call itself will not change anything within your entity (except possibly the ID if a sequence that allows preallocation to be used), so any null references will still be null.
Eventually the transaction commits and depending on your provider, entities can be cached in a second level cache. I'll assume you aren't using it for the sake of brevity; unless you force the EM to refresh this instance (flush first if its a new one!) or read it in a separate EntityManager, you will always get that same instance back with any null references.
If you refresh it or otherwise cause it to be reloaded, your JPA provider is required to set everything in the object as it is in the database, according to your mappings. Since null isn't a persistable state for a collection mapping, that means it will either eagerly fetch your references, or place proxies in there for lazy relationships, causing you to find an empty collection.

Spring data JPA avoid multiple select in many to one relationship

I'm using Spring Data JPA and have a many to one relationship from Child to Parent Class using hibernate. I'm writing a search API which would search on child table using some child table columns and return list of child objects along with some data from Parent class for each child object. I'm doing by default eager fetch for many to one relationship. The problem i'm facing is lets say after searching child table 10 entries are returned then hibernate is doing 10 different select queries on parent class to get Parent object for each child object. Is there a way to optimize this ? There is a solution given to similar problem here but it is for one to many case. I could not find anything helpful regarding this on web also. Any ideas ?
as you didn't show any codes in the question, it's a little hard to solve it but I think if you specify join column (#JoinColumn annotation) and use #OneToMany annotation in parent class(with specifying fetch type) and #ManyToOne inside child you should not have any problem:
#Entity(name ="Parent")
public class Parent {
#Id
#Column
private int id;
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#JoinColumn(name= "paren_id")
private Set<Child> children;
//getters and setters
}
#Entity(name ="Child")
public class Child{
#Id
#Column
private int id;
#ManyToOne
private Parent parent;
//getters and setters
}

Resources