2017-02-19 4 views
-2

In Hibernate 4 i tun könnte wieDaten mit CriteriaQuery in Hibernate 5

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()))); 

wo validTill ist vom Typ LOCALDATE in Java 8. Aber mit Hibernate 5, wenn ich tun wie

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()) 
      )); 

Vergleich dann erhalte ich einen Fehler bei der Methode builder.ge(), die besagt, dass der Typ CriteriaBuilder nicht für Argumente (Path, LocalDate) anwendbar ist.

mein Flight implimentation ist

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; 
} 

}

Vorschläge, wie LOCALDATE vergleichen mit Hibernate 5 Kriterien verwenden.

+1

Was ist der Fehler? Fügen Sie auch die FlightData-Entity impl –

Antwort

0

Wenn Sie mit Hibernate 5 gestartet wird, dann würde ich vorschlagen, diese Abhängigkeit zu Ihrem Classpath hinzu:

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

Dies ermöglicht Hibernate diese Typen als Grundtypen zu behandeln.

Datum und Zeit API wird in der nächsten Version von JPA standardisiert werden. Bis dahin ist eine Umgehung wie die Verwendung dieser Bibliothek oder die Implementierung von benutzerdefinierten @Converter (siehe diesen tollen Artikel zu diesem Thema: converters) eine Notwendigkeit.

+0

danke @Maciej Kowalski hinzu –

Verwandte Themen