Timothy Vogel
Timothy Vogel

Reputation: 1597

Hibernate move records between tables

I need to handle approving records and making them avaialble based on their status. My original idea was to use a flag column to filter the records but business practices of strict segregation of approved / unapproved records prevents this approach. The next most logical approach (to me) would be to move the records to an approved table.

I am using the entity-name attribute to map the same class to two different tables - APPROVED_ and UNAPPROVED_. When I try to move the record it deletes from the unapproved but does not insert into approved. I turned on hibernate.show_sql and the retrieves / deletes are shown but no insert.

The approved table has generator class="assigned" so that it uses the id from the unapproved table as its key.

Any suggestions on what I'm not doing correctly? Or a better way to do this?

Here is the code

    try {
        // begin transaction
        ses = Activator.getSession();
        ses.beginTransaction();
        dao.setSession(ses);
        daoMotor.setSession(ses);

        // for each input record in selectedMotors
        for (Long curId : selectedMotors) {
            // retrieve the input record
            IThreePhaseMotorInput record = dao.findById(curId, false);

            // save the motor into the permanent table using entity-name
            IThreePhaseMotor curMotor = record.getMotor();
            daoMotor.makePersistent("ThreePhaseMotor", (ThreePhaseMotor) curMotor);

            // delete the input record
            dao.makeTransient((ThreePhaseMotorInput) record);
        }

        // commit transaction
        ses.getTransaction().commit();
    } catch (Throwable t) {
        ErrorInfo info = ErrorInfoFactory.getUnknownDatabaseInfo(t, null, IThreePhaseMotorList.class.getName());
        Platform.getLog(Activator.getContext().getBundle()).log(
                new Status(IStatus.ERROR, Activator.PLUGIN_ID, info.getErrorDescription(), t));
        throw new BusinessException(info);
    } finally {
        if (ses != null && ses.isOpen()) {
            ses.close();
        }
    }

And the abbreviated hbm.xml files:

<class name="ThreePhaseMotorInput" table="THREE_PHASE_MOTOR_INPUT" lazy="false">
    <id name="id" type="java.lang.Long">
        <column name="ID" />
        <generator class="native" />
    </id>
    <version generated="never" name="version" type="java.lang.Integer" />
    <many-to-one name="motor" cascade="all" entity-name="UnapprovedThreePhaseMotor"  fetch="join">
        <column name="MOTOR" />
    </many-to-one>
</class>
<class name="ThreePhaseMotor" table="UNAPPROVED_THREE_PHASE_MOTOR" entity-name="UnapprovedThreePhaseMotor">
    <id name="id" type="java.lang.Long">
        <column name="ID" />
        <generator class="native" />
    </id>
    <version generated="never" name="version" type="java.lang.Integer" />
</class>
<class name="ThreePhaseMotor" table="THREE_PHASE_MOTOR"  entity-name="ApprovedThreePhaseMotor">
    <id name="id" type="java.lang.Long">
        <column name="ID" />
        <generator class="assigned" />
    </id>
    <version generated="never" name="version" type="java.lang.Integer" />

Upvotes: 3

Views: 1294

Answers (1)

Timothy Vogel
Timothy Vogel

Reputation: 1597

After sleeping on it (my wire says I do some of my best thinking while sleeping!), I realized that the issue is as gkamai suggested. I need to do a deep copy.

Change

IThreePhaseMotor curMotor = record.getMotor();
daoMotor.makePersistent("ThreePhaseMotor", (ThreePhaseMotor) curMotor);

to

IThreePhaseMotor curMotor = new ThreePhaseMotor(record.getMotor());
daoMotor.makePersistent("ThreePhaseMotor", (ThreePhaseMotor) curMotor);

Upvotes: 1

Related Questions