I am trying persist this entity:
@Entity
public class Produto extends Model {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
...
@OneToMany
@Fetch(FetchMode.SELECT)
@Cascade(CascadeType.ALL)
private List<Imagem> thumbnails;
...
}
through this form:
<table>
<tbody><tr>
<td>
<button type="button" onclick="add_single_imagem();" style="display: block;">
<img class="thumbnail" src="/images/icon_add_imagem.png" alt="adicionar icone">
<input type="file" accept="image/jpeg" class="image-uploader" id="thumbnails" style="display: none;" onchange="image_upload(this);" data-target="thumbnails" data-url="/imagem/upload" data-path="/imagem/download">
</button>
</td>
<td>
<div class="gallery" id="gallery">
<input type="hidden" name="thumbnails" value="3"><img class="thumbnail" id="image_3" src="/imagem/download/3"></div>
</td>
</tr>
</tbody></table>
which follow this "route":
controller
@RequestMapping(value = "/insert", method=RequestMethod.POST)
@ResponseBody
@PreAuthorize("hasPermission(#user, 'cadastra_'+#this.this.name)")
public void insert(@Valid E object, BindingResult result) {
serv.insert(object);
}
service
public void insert(E object) {
dao.insert(object);
}
dao
public void insert(E object) {
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(object);
entityManager.getTransaction().commit();
entityManager.close();
}
PropertyEditor
public class ImagemEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) {
if (!text.equals("")) {
Integer id = Integer.parseInt(text);
ImagemService serv = new ImagemService();
org.loja.AppContextHolder.getContext().getAutowireCapableBeanFactory().autowireBean(serv);
Imagem imagem = serv.findBy("id", id);
setValue(imagem);
} else {
setValue(null);
}
}
}
But I am getting this error:
org.hibernate.PersistentObjectException: detached entity passed to persist: org.loja.model.imagem.Imagem
and when I try quit the application (with ctrl-c, I am running it with spring-boot), it crashes, stuck with this message:
2019-11-18 19:55:46.244 INFO 134572 --- [ Thread-3] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
until I kill the process.
Anyone can give a hint of what's wrong here?
I managed to solve this issue changing the attribute configuration to that:
@OneToMany(fetch = FetchType.EAGER)
private Set<Imagem> thumbnails;
and with this html/thymeleaf code:
<table>
<tr>
<td>
<button type="button" onclick="add_single_imagem();" th:style="${command.icone}? 'display: none;' : 'display: block;'">
<img class="thumbnail" th:src="@{/images/icon_add_imagem.png}" alt="adicionar icone"/>
<input type="file" accept="image/jpeg" class="image-uploader" id="thumbnails" style="display: none;" th:attr="data-target=${'thumbnails'}, data-url=@{/imagem/upload}, data-path=@{/imagem/download}" onchange="image_upload(this);"/>
</button>
</td>
<td>
<div class="gallery" id="gallery">
<th:block th:each="img,stat : ${command.thumbnails}">
<input type="hidden" th:field="*{thumbnails}" th:value="${img.id}"/>
<img class="thumbnail" th:id="${'image_'+img.id}" th:src="@{/imagem/download/__${img.id}__}" th:alt="${command.nome}">
</th:block>
</div>
</td>
</tr>
</table>
Now this attribute is persisted alongside the entity without problems.