开发者

Hibernate: Unable to find entity which clearly exists, why not?

I have entity class which is used to model a weather station, and it includes a unique string property "code". I also have an entity class which is used to contain daily averages for various observations, and this daily average entity class has an many-to-one association to the station entity class, i.e. each daily average object is associated with a station:

@Entity(name = EsrlDailyAvg.TABLE_NAME)
@Table(name = EsrlDailyAvg.TABLE_NAME, 
       uniqueConstraints = { @UniqueConstraint(columnNames = { EsrlDailyAvg.STATION_COLUMN_NAME,
                                                               EsrlDailyAvg.DATE_COLUMN_NAME }) })
public final class EsrlDailyAvg
    extends AbstractPersistentEntity<Long>
{
    public static final String TABLE_NAME = "ESRL_DAILY_AVG";
    public static final String STATION_COLUMN_NAME = "ESRL_STATION_ID";
    public static final String DATE_COLUMN_NAME = "ESRL_DATE";
    private EsrlStation station;
    private Date date;
    // additional properties used to contain average values

    @Column(name = DATE_COLUMN_NAME, nullable = false)
    public Date getDate()
    {
        return date;
    }

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinColumn(name = STATION_COLUMN_NAME)
    public EsrlStation getStation()
    {
        return station;
    }

    ....
}


@Entity(name = EsrlStation.TABLE_NAME)
public class EsrlStation
    extends AbstractPersistentEntity<Long>
{
    public static final String TABLE_NAME = "ESRL_STATION";
    private EsrlStationCodeEnum code;

    @Column(name = "CODE", nullable = false, unique = true)
    @Enumerated(EnumType.STRING)
    public EsrlStationCodeEnum getCode()
    {
        return code;
    }

    ...
}

The DAO class for daily average entities contains a method to find a unique daily average entity by station code and date:

public EsrlDailyAvg findByStationCodeDate(final String stationCode,
                                          final Date date)
{
    String hql = "from " + getCanonicalPersistentClassName() + " dailyAvg where dailyAvg.station.code = '" +
                 stationCode + "' and dailyAvg.date = :date";
    return (EsrlDa开发者_开发问答ilyAvg) getCurrentSession().createQuery(hql).setParameter("date", date).uniqueResult();
}

I have written tests which save and then successfully find daily average entities by station code and date, so I am assuming that the above DAO code works as expected. However when I run a stand-alone program which uses this DAO method I cannot successfully find a daily average entity object which I know to be present (I can query the database using SQL and see the record).

I'm not sure if this is relevant but I run my tests using an in-memory HSQL database, and I am running the stand-alone program using an Oracle database.

I have debugged the code and the station code and date parameters are being passed as expected to the DAO method.

Can anyone comment as to why this is happening, or suggest where I should look for the error? Thanks in advance for your help.


It turns out that the error had to do with how I was creating the date parameter that was being passed to the DAO method in question. I was first creating a GregorianCalendar object in order to set the year, month, and day, but I neglected to set the milliseconds field value to zero. Once I did that then the Date value that I used as the parameter to the DAO method (myCalendar.getTime()) matches to the date value in the database and the DAO method finds the record as expected.

Lesson learned: when deriving a Date from a Calendar be sure to set all of the Calendar fields (don't assume zero values for unset Calendar fields).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜