Search code examples
jpaderby

jpa-derby Boolean merge


am working with JPA(EclipseLink) and Derby. In my object there is a boolean field. Before a merge operation, the field is set to true. but after the merge, the field still holds the false value.

@Entity
@Access(AccessType.PROPERTY)
public class SleepMeasure extends AbstractEntity {
    private static final long serialVersionUID = 1361849156336265486L;

    ...
    private boolean WeatherDone;

    public boolean isWeatherDone() { // I have already tried with the "getWeatherDone()"
        return WeatherDone;
    }

    public void setWeatherDone(boolean weatherDone) {
        WeatherDone = weatherDone;
    }   
    ...
}

It doesn't seem to matter whether, I use "getWeatherDone()" or "isWeatherDone()".

using code:

public class WeatherDataCollectorImpl{
    ...
    private void saveMeasures(WeatherResponse mResponse, SleepMeasure sleep) throws Exception {

        AppUser owner = sleep.getOwner();
        ...
        sleep.setWeatherDone(Boolean.TRUE);
        reposService.updateEntity(sleep,SleepMeasure.class);
    }
    ...
}

And here is my repository class

    public class RepositoryImpl{
    ...
    public <T extends AbstractEntity> T updateEntity(T entity, Class<T> type) throws RepositoryException {
        openEM();
        EntityTransaction tr = em.getTransaction();

        try {

            tr.begin();
            {
                // entity.weatherdone has value true
                    entity = em.merge(entity);
                // entity.weatherdone has value false
            }
            tr.commit();

        } catch (Exception e) {
            tr.rollback();
        }

        return entity;
    }
    ...
}

JPA console Info: There is no error, nor warning and not even any info that the boolean column shall be updated.

--Merge clone with references com.sleepmonitor.persistence.entities.sleep.SleepMeasure@b9025d
...
--Register the existing object  // other objects
...
--Register the existing object com.sleepmonitor.persistence.entities.sleep.SleepMeasure@1ba90cc

So how do I solve this small problem.

Note: Derby defined this field as "SMALLINT".

thanks.


Solution

  • Oh God! I found my problem. Actually I realised, it was not only the boolean field, but the whole object could not be updated. While trying to complete a bideirection referencing, I stupidly did this in a setter property instead of an addMethod() .

    public void setSleepProperties(SleepProperties sleepProperties) {
        this.sleepProperties = sleepProperties;
        if (!(sleepProperties == null)) {
            this.sleepProperties.setSleepMeasure(this);
        }
    }
    

    Instead of:

    public void addSleepProperties(SleepProperties sleepProperties) {
        this.sleepProperties = sleepProperties;
        if (!(sleepProperties == null)) {
            this.sleepProperties.setSleepMeasure(this);
        }
    }
    

    So I ended up with the referenced entity (sleepProperties.sleepMeasure) over-writing the updates on the owning entity just before a merge. That was very defficult to find, and I think have learned a big lesson from it. Thanks to all who tried to help me out.

    The "addMethod()" solved my problem.