"org.hibernate.PersistentObjectException: detached entity" 随后应用程序崩溃
"org.hibernate.PersistentObjectException: detached entity" followed by application crash
我正在尝试保留这个实体:
@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;
...
}
通过此表格:
<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>
跟随这个 "route":
控制器
@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);
}
服务
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();
}
属性编辑器
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);
}
}
}
但是我收到这个错误:
org.hibernate.PersistentObjectException: detached entity passed to persist: org.loja.model.imagem.Imagem
当我尝试退出应用程序时(使用 ctrl-c,我 运行 使用 spring-boot),它崩溃了,卡在这条消息中:
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'
直到我终止进程。
任何人都可以提示这里有什么问题吗?
我设法解决了这个问题,将属性配置更改为:
@OneToMany(fetch = FetchType.EAGER)
private Set<Imagem> thumbnails;
并使用此 html/thymeleaf 代码:
<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>
现在此属性与实体一起持久保存,没有问题。
我正在尝试保留这个实体:
@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;
...
}
通过此表格:
<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>
跟随这个 "route":
控制器
@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);
}
服务
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();
}
属性编辑器
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);
}
}
}
但是我收到这个错误:
org.hibernate.PersistentObjectException: detached entity passed to persist: org.loja.model.imagem.Imagem
当我尝试退出应用程序时(使用 ctrl-c,我 运行 使用 spring-boot),它崩溃了,卡在这条消息中:
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'
直到我终止进程。
任何人都可以提示这里有什么问题吗?
我设法解决了这个问题,将属性配置更改为:
@OneToMany(fetch = FetchType.EAGER)
private Set<Imagem> thumbnails;
并使用此 html/thymeleaf 代码:
<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>
现在此属性与实体一起持久保存,没有问题。