[英]Hibernate : org.hibernate.WrongClassException when I try to remove an object
我正在使用繼承來使用Hibernate在JPA上描述我的模型,我有以下類:
@Entity
@Table(name = "evento")
@Inheritance(strategy = InheritanceType.JOINED)
public class Evento implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
@Id
private int id;
@Column(name = "numero")
private String numeroEvento;
和這個孩子班:
@Entity
@Table(name = "evento_sesion")
@PrimaryKeyJoinColumn(name = "idevento", referencedColumnName = "id")
public class EventoSesion extends Evento implements Serializable {
@Column(name = "objeto")
private String objeto;
...
問題是,當我嘗試執行“ eventoFacade.remove(e)”(e是數據庫中現有對象的實例)時,出現此錯誤:
Caused by: javax.persistence.PersistenceException: org.hibernate.WrongClassException: Object with id: null was not of the specified subclass: entidades.Evento (class of the given object did not match class of persistent copy)
有人可以幫助我還是給我一個線索?
謝謝 :)
我嘗試給出一個答案,但是答案將是推測性的。 問題出在對實體管理器執行remove
的代碼中,即eventoFacade.remove(e)
。
但是,假定您已在數據庫中創建並存儲了EventoSession
類型的Entity,然后嘗試使用基本類型的實例調用em.remove
,然后得到您提到的異常。
讓我們假設EventoSession
類型的實體具有主鍵1,現在我們嘗試刪除該實體。
Evento es = new Evento();
es.setNumeroEvento("...");
es.setId(1);
es = em.merge(es); // detached objects cannot be removed, therefore we merge es
em.remove(es);
然后,對em.merge
的調用將以javax.persistence.PersistenceException
結束,其原因是:
Caused by: org.hibernate.WrongClassException:
Object [id=null] was not of the specified subclass [entidades.Evento]:
class of the given object did not match class of persistent copy
我認為這可能是問題所在。
要修復此問題,只需通過實體管理器加載要刪除的實例:
em.remove(em.find(Evento.class, 1));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.