séan35
séan35

Reputation: 1052

how to delete records from DB with update cascade in hibernate

I want to delete records from DB (MySQL) with update cascade.

There is a day plan and some duration plans associated with this day plan on DB. I want to update the duration list for this day plan.

Example;

Initializations;

...
    DayPlan dayPlan = new DayPlan();
    Set<DurationPlan> list = new HashSet<DurationPlan>();
...

Preperations to update;

...
    durationPlan = new DurationPlan(dayPlan, x, y, z);
    list.add(durationPlan);
    dayPlan.setDURATION_PLANS(list);
...

Update process with Hibernate-session;

...
session.saveOrUpdate(dayPlan);
...

DurationPlan hibernate-mapping;

<class name="planManagement.DurationPlan" table="DURATIONPLAN">
    <id name="DURATION_PLAN_ID" type="int">
        <column name="DURATION_PLAN_ID" />
        <generator class="native" />
    </id>
    <many-to-one name="DAY_PLAN" class="planManagement.DayPlan" lazy="false" fetch="join">
        <column name="DAY_PLAN_ID" not-null="true"/>
    </many-to-one>
    ...
</class>

DayPlan hibernate-mapping;

<class name="planManagement.DayPlan" table="DAYPLAN">
    <id name="DAY_PLAN_ID" type="int">
        <column name="DAY_PLAN_ID" />
        <generator class="native" />
    </id>
    <set name="DURATION_PLANS" table="DURATIONPLAN" inverse="true" cascade="all" lazy="true" fetch="select">
        <key>
            <column name="DAY_PLAN_ID" not-null="true" />
        </key>
        <one-to-many class="planManagement.DurationPlan" />
    </set>
    ...
</class>

All feedback appreciated!

Upvotes: 0

Views: 48

Answers (1)

Amogh
Amogh

Reputation: 4573

The problem here is, How hibernate will find which records need to be deleted? As when you call session.saveOrUpdate(dayPlan); where dayPlan will contains list of duration containing only two objects i.e a,b (a(updated), b(no updated).) As per you 'c' need to be deleted but how hibernate will get to know 'c' need to be deleted.

Upvotes: 1

Related Questions