I use Hibernate 5.1.0.Final. My GenericDAO
class main methods:
public T save(T entity) {
entityManager.getTransaction().begin();
entityManager.persist(entity);
entityManager.getTransaction().commit();
}
public T find(Long entityId) {
T e = (T) entityManager.find(type, entityId);
entityManager.refresh(e);
return e;
}
I have Item
entity which contains List<Image> images
.
@Entity
@Table(name="item")
public class Item extends GenericEntity {
@Column(name="title")
String title;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "item", cascade = {CascadeType.ALL}, orphanRemoval = true)
@NotFound(action = NotFoundAction.IGNORE)
List<Image> images;
@Version
@Temporal(TemporalType.TIMESTAMP)
@Column(name="version")
Date version;
}
@Entity
@Table(name="image")
public class Image extends GenericEntity {
@ManyToOne
@JoinColumn(name="item")
Item item;
@Column(name="image_name")
String imageName;
}
First, I load Item
with genericDAO.find(id)
method - it contains up-to-date list of images. Then I load Item
with the same ID in another REST method which removes old images and adds new ones, changes title. Later, if try to reload Item
with genericDAO.find(id)
in the first REST method, I get outdated images - they aren't selected again while Item
title is retrieved correctly.
How to get completely refreshed entity with its childs from a database?