简体   繁体   English

当我尝试删除对象时,Hibernate:org.hibernate.WrongClassException

[英]Hibernate : org.hibernate.WrongClassException when I try to remove an object

I'm using inheritance to describe my model on JPA with hibernate, I have this classes: 我正在使用继承来使用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;

And this child class: 和这个孩子班:

@Entity
@Table(name = "evento_sesion")
@PrimaryKeyJoinColumn(name = "idevento", referencedColumnName = "id")
public class EventoSesion extends Evento implements Serializable {

    @Column(name = "objeto")
    private String objeto;
...

The problem is when I try to do a "eventoFacade.remove(e)" (e is an instance of an existent object in DB) I get this error: 问题是,当我尝试执行“ 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)

Somebody can help me or give me a clue? 有人可以帮助我还是给我一个线索?

Thanks :) 谢谢 :)

I try to give an answer, but the answer will be speculative; 我尝试给出一个答案,但是答案将是推测性的。 the problem is in the code which performs the remove on the entity manager, ie in eventoFacade.remove(e) . 问题出在对实体管理器执行remove的代码中,即eventoFacade.remove(e)

However, assume that you have created and stored an Entity of type EventoSession in the database, and then you try to invoke em.remove with an instance of the base type, then you get the exception which you mentioned. 但是,假定您已在数据库中创建并存储了EventoSession类型的Entity,然后尝试使用基本类型的实例调用em.remove ,然后得到您提到的异常。

Let us assume, that the entity of type EventoSession has the primary key 1, and now we try to remove this entity. 让我们假设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);

The call to em.merge will then end in a javax.persistence.PersistenceException , caused by: 然后,对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

and I assume that this could be the problem. 我认为这可能是问题所在。

To fix it, simply load the instance to be removed over the entity manager: 要修复此问题,只需通过实体管理器加载要删除的实例:

em.remove(em.find(Evento.class, 1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 org.hibernate.WrongClassException? - How to fix org.hibernate.WrongClassException? org.hibernate.WrongClassException关于通过Hibernate保存实体 - org.hibernate.WrongClassException on saving an entity via Hibernate Hibernate:org.hibernate.WrongClassException,SINGLE_TABLE继承和DiscriminatorFormula - Hibernate: org.hibernate.WrongClassException, SINGLE_TABLE inheritance and DiscriminatorFormula 从 hibernate 4 升级到 5.2.17 时单表继承中的 org.hibernate.WrongClassException - org.hibernate.WrongClassException in single table inheritance when upgrade from hibernate 4 to 5.2.17 自定义鉴别器的 Hibernate WrongClassException - Hibernate WrongClassException for Custom Discriminators 休眠WrongClassException /集/鉴别符? - Hibernate WrongClassException / Sets / Discriminators? Discriminator,WrongClassException JPA和Hibernate后端 - Discriminator , WrongClassException JPA with Hibernate backend 当我尝试在belogic中使用休眠模式保存对象时,出现此异常 - When I try to save object using hibernate in belogic this exception appear 当我尝试在Spring中更新对象时,Hibernate会执行删除查询 - Hibernate doing a delete query when I try to update an object in Spring 收到org.hibernate.InvalidMappingException:尝试运行程序时无法读取xml - getting org.hibernate.InvalidMappingException: unable to read xml when I try to run my program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM