Hibernate无法删除子记录 [英] Hibernate unable to delete child record

查看:65
本文介绍了Hibernate无法删除子记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新(而不是删除)父记录时无法删除子记录.另外,我还阅读了其他文章,但似乎其他大多数文章都使用注解而不是xml,因此很难看到它们与我的问题之间的关系.

I'm unable to delete a child record while I'm updating (not deleting) the parent record. Also, I've read other posts, but it seems most of the others are using annotations rather than xml, so it can be difficult to see how they relate to my issue.

我有两个表:EventInfo表保存有关事件的信息,然后是EventLicenseType表,该表只有两个列,而这两个列均构成主键.EventLicenseType表中的一列是EventInfo表的外键.

I have two tables: The EventInfo table that holds information about events and then the EventLicenseType table that only has two columns and both of those columns make up the primary key; one of the columns in the EventLicenseType table is a foreign key to the EventInfo table.

问题是我似乎无法删除EventLicenseType记录.我尝试了很多不同的方法,但没有任何工作对我有用.休眠状态似乎想将null用作eventinfoId列,这当然是行不通的.我试过清除Set,然后进行合并,还专门调用了session.delete(eventlicenseTypeRec),然后进行了合并.都没有为我工作.

The problem is I can't seem to delete an EventLicenseType record. I've tried a bunch of different things and nothing is working for me. It seems like hibernate wants to put null as the eventinfoId column, which of course doesn't work. I have tried clearing out the Set and then doing the merge, and also specifically calling session.delete(eventlicenseTypeRec) and then doing the merge. Neither is working for me.

EventInfo.hbm.xml文件:

EventInfo.hbm.xml file:

<hibernate-mapping default-lazy="true">

<class name="Eventinfo" table="PA_EVENTINFO">
    <id name="eventInfoId" type="int"
        column="PA_EVENTINFOID">
        <generator class="native" />
    </id>
    <property name="eventTypeId" type="java.lang.String"
        column="PA_EVENTTYPEID" length="255" />

        ...Other columns not shown here for brevity...

    <set name="eventLicenceTypeIds" lazy="false" cascade="all-delete-orphan">
        <key column="PA_EVENTINFOID"/>
        <one-to-many class="EventLicenseType" />
    </set>      
</class>

EventLicenseType.hbm.xml文件:

EventLicenseType.hbm.xml file:

<hibernate-mapping default-lazy="true">

<class name="EventLicenseType" table="PA_EVENTLICENSETYPE">
    <composite-id>
        <key-property name="licenseTypeId" type="java.lang.Integer" column="PA_LICENSETYPE"/>
        <key-property name="eventInfoId" type="java.lang.Integer" column="PA_EVENTINFOID"/>
    </composite-id>
</class>

这里是EventInfo类.同样,实际文件中还有更多字段,这只是重要的部分:

Here is the EventInfo class. Again, there are more fields in the actual file, this is just the important pieces:

public class Eventinfo implements Serializable {
     /** identifier field */
    private int eventInfoId;    

    /** nullable persistent field */
    @Field(name="eventInfo_eventTypeId")
    private String eventTypeId;

    @IndexedEmbedded
    private Set<EventLicenseType> eventLicenceTypeIds;

    /** default constructor */
    public Eventinfo() {}

    public int getEventInfoId() {
        return this.eventInfoId;
    }

    public void setEventInfoId(int eventInfoId) {
        this.eventInfoId = eventInfoId;
    }

    public String getEventTypeId() {
        return this.eventTypeId;
    }

    public void setEventTypeId(String eventTypeId) {
        this.eventTypeId = eventTypeId;
    }

    public Set<EventLicenseType> getEventLicenceTypeIds() {
        return eventLicenceTypeIds;
    }

    public void setEventLicenceTypeIds(Set<EventLicenseType> eventLicenceTypeIds) {
        this.eventLicenceTypeIds = eventLicenceTypeIds;
    }

这是EventLicenseType类

Here is the EventLicenseType class

public class EventLicenseType implements Serializable{

    @Field
    private int licenseTypeId;
    private int eventInfoId;

    public int getLicenseTypeId() {
        return licenseTypeId;
    }

    public void setLicenseTypeId(int licenseTypeId) {
        this.licenseTypeId = licenseTypeId;
    }

    public int getEventInfoId() {
        return eventInfoId;
    }

    public void setEventInfoId(int eventInfoId) {
        this.eventInfoId = eventInfoId;
    }
}

这是我在DAO中执行的方法.目前,只有一个与eventInfo记录相关联的记录,所以我只是想看看是否可以删除该记录.(还请注意,eventinfo是在围绕该方法的方法中定义的.)

Here is the method I'm executing in my DAO. For now there is only one record associated to the eventInfo record, so I'm just trying to see if I can delete that one. (Also note that eventinfo is defined in the method that surrounds this one).

public Eventinfo execute(Session session) throws Exception {

    //Get the existing eventInfo record                 
    Eventinfo existing = (Eventinfo)session.get(Eventinfo.class, eventinfo.getEventInfoId());
    Iterator iter = existing.getEventLicenceTypeIds().iterator();
    if (iter.hasNext()) {
        EventLicenseType license = (EventLicenseType) iter.next();
        iter.remove();
        session.delete(license);
    }

    session.flush();

    return (Eventinfo) session.merge(eventinfo);
}

在上面的session.flush()行上,我得到一个错误:java.sql.BatchUpdateException:无法将值NULL插入表'PA_EVENTLICENSETYPE'的列'PA_EVENTINFOID'中;列不允许为空.UPDATE失败.它表明休眠状态正在尝试执行此操作:

On the above session.flush() line, I get an error: java.sql.BatchUpdateException: Cannot insert the value NULL into column 'PA_EVENTINFOID', table 'PA_EVENTLICENSETYPE'; column does not allow nulls. UPDATE fails. It shows that hibernate is trying to do:

update PA_EVENTLICENSETYPE set PA_EVENTINFOID=null where PA_EVENTINFOID=?

为什么不能只删除记录?为什么要尝试进行更新?我还尝试将代码更改为以下代码,并得到相同的错误.

Why can't it just delete the record? Why is it trying to do an update?? I also tried changing the code to the below and get the same error.

public Eventinfo execute(Session session) throws Exception {

    //Clear out the list
    eventinfo.getEventLicenceTypeIds().clear();             

    return (Eventinfo) session.merge(eventinfo);
}

任何人都可以帮助我解决我所缺少的东西,或者为我指明正确的方向吗?

Can anyone help me with what I'm missing, or point me in the right direction?

推荐答案

您需要查看两个表之间的映射是单边还是双边.基本上,您需要将两个表的行或对象视为对象,并切断EventInfo和EventLicenceType表的对象之间的所有联系.因此,如果关系仅来自EventInfo-> EventLicenseType,则需要在EventInfo对象中将EventLicenseType的值设置为null.另外,如果EventLicenseType有映射,则需要将join列的值设置为null.然后merge()EventInfo对象.

You need to see whether the mapping between two tables are unilateral or bilateral. Basically you need to think or the rows of two tables as objects and cut all the ties between the objects of EventInfo and EventLicenceType tables. So if the relation is only from EventInfo -> EventLicenseType, you need to set the value of the EventLicenseType set to null in EventInfo object. Also if there is a mapping from EventLicenseType, you need to set the value of joining column to null. Then merge() the EventInfo object.

无显式删除或删除EventLicenseType对象.如果这里没有对EventLicenseType对象的引用,则JVM将其作为垃圾收集.

Noneed to explicitly delete or remove the EventLicenseType object. If here are no references to EventLicenseType object, the JVM will collect it as garbage.

希望能解决您的问题.

这篇关于Hibernate无法删除子记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆