将日期与hibernate 5

时间:2017-02-19 09:32:29

标签: java hibernate

在hibernate 4中,我可以这样做

Criteria criteria = session.createCriteria(FlightData.class);

    // Restriction to match Departure Location, Arrival Location and ValidTill Date
    criteria.add(Restrictions.and(Restrictions.eq("departureLocation", query.getDepartLoc()),
            Restrictions.eq("arrivalLocation", query.getArrivalLoc()),
            Restrictions.ge("validTill", query.getTravelDate())));

其中validTill在java 8中的类型为LocalDate。 但是如果我愿意的话,请使用hibernate 5

CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery<FlightData> cq = builder.createQuery(FlightData.class);


    Root<FlightData> root = cq.from(FlightData.class);

    cq.select(root).where(builder.and(
            builder.equal(root.get("departureLocation"), query.getDepartLoc()),
            builder.equal(root.get("arrivalLocation"), query.getArrivalLoc()),
            builder.ge(root.get("validTill"), query.getTravelDate())
            ));

然后我在builder.ge()方法上说错了 类型CriteriaBuilder不适用于参数(Path,LocalDate)。

我的FlightData暗示是

public class FlightData implements Serializable {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = 6511441862178750017L;

/** The id serves as a primary key for the table */
private long id;

/** The flight number. */
private String flightNumber;

/** The departure location. */
private String departureLocation;

/** The arrival location. */
private String arrivalLocation;

/** The fare. */
private double fare;

/** The seat availability. */
private String seatAvailability;

/** The flight time. */
private String flightTime;

/** The flight duration. */
private String flightDuration;

/** The flight class. */
private String flightClass;

/** The Valid date. */
private LocalDate validTill;

/**
 * Gets the id.
 *
 * @return the id
 */
public long getId() {
    return id;
}

/**
 * Sets the id.
 *
 * @param id the new id
 */
public void setId(long id) {
    this.id = id;
}


/**
 * Sets the flight number.
 *
 * @param flightNum the new flight number
 */
public void setFlightNumber(String flightNum) {
    flightNumber = flightNum;
}

/**
 * Gets the flight number.
 *
 * @return the flight number
 */
public String getFlightNumber() {
    return flightNumber;
}

/**
 * Sets the departure location.
 *
 * @param depLoc the new departure location
 */
public void setDepartureLocation(String depLoc) {
    departureLocation = depLoc;
}

/**
 * Gets the departure location.
 *
 * @return the departure location
 */
public String getDepartureLocation() {
    return departureLocation;
}

/**
 * Sets the arrival location.
 *
 * @param arrLoc the new arrival location
 */
public void setArrivalLocation(String arrLoc) {
    arrivalLocation = arrLoc;
}

/**
 * Gets the arrival location.
 *
 * @return the arrival location
 */
public String getArrivalLocation() {
    return arrivalLocation;
}

/**
 * Sets the seat availability.
 *
 * @param seatAvail the new seat availability
 */
public void setSeatAvailability(String seatAvail) {
    seatAvailability = seatAvail;
}

/**
 * Gets the seat availability.
 *
 * @return the seat availability
 */
public String getSeatAvailability() {
    return seatAvailability;
}

/**
 * Sets the fare.
 *
 * @param fare the new fare
 */
public void setFare(double fare) {
    this.fare = fare;
}

/**
 * Gets the fare.
 *
 * @return the fare
 */
public double getFare() {
    return fare;
}

/**
 * Sets the local date.
 *
 * @param date the new local date
 */
public void setValidTillDate(String date) {
    StringTokenizer st = new StringTokenizer(date, "-");
    int day = Integer.parseInt(st.nextToken());
    int month = Integer.parseInt(st.nextToken());
    int year = Integer.parseInt(st.nextToken());

    validTill = LocalDate.of(year, month, day);
}

/**
 * Gets the local date.
 *
 * @return the local date
 */
public LocalDate getValidTillDate() {
    return validTill;
}

/**
 * Sets the flight duration.
 *
 * @param flightDur the new flight duration
 */
public void setFlightDuration(String flightDur) {
    flightDuration = flightDur;
}

/**
 * Gets the flight duration.
 *
 * @return the flight duration
 */
public String getFlightDuration() {
    return flightDuration;
}

/**
 * Sets the flight class.
 *
 * @param flightClass the new flight class
 */
public void setFlightClass(String flightClass) {
    this.flightClass = flightClass;

}

/**
 * Gets the flight class.
 *
 * @return the flight class
 */
public String getFlightClass() {
    return flightClass;
}

/**
 * Sets the flight time.
 *
 * @param flighttime the new flight time
 */
public void setFlightTime(String flighttime) {
    flightTime = flighttime;
}

/**
 * Gets the flight time.
 *
 * @return the flight time
 */
public String getFlightTime() {
    return flightTime;
}

}

有关如何使用Criteria将LocalDate与hibernate 5进行比较的任何建议。

1 个答案:

答案 0 :(得分:0)

如果您开始使用Hibernate 5,那么我建议将此依赖项添加到您的类路径中:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.2.7.Final</version>
</dependency>

这将允许Hibernate将这些类型视为基本类型。

日期和时间API将在下一版本的JPA中标准化。 在此之前,使用该库或实现自定义@Converter的解决方法(请参阅有关此主题的精彩文章:converters)是必要的。