| Considering the following entities:
@Entity(name = "Phone")
public static class Phone {
@Id
private Long id;
private String number;
@OneToMany(mappedBy = "phone")
@MapKey(name = "timestamp")
@MapKeyTemporal(TemporalType.TIMESTAMP )
private Map<Date, Call> callHistory = new HashMap<>();
public Phone() {}
public Phone(String number) {
this.number = number;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNumber() {
return number;
}
public Map<Date, Call> getCallHistory() {
return callHistory;
}
public void addCall(Call call) {
callHistory.put( call.getTimestamp(), call );
call.setPhone( this );
}
}
@Entity(name = "Call")
public static class Call {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private Phone phone;
private Date timestamp;
private int duration;
public Call() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
}
The Phone has a Map property where the key is the Call timestamp and the value is the Call itself. When running this path expression HQL query:
List<Map.Entry<Date, Call>> callHistory = session.createQuery(
"select entry(ch) " +
"from Phone ph " +
"join ph.callHistory ch " +
"where ph.id = :id " )
.setParameter( "id", 1L )
.list();
We get the following error:
|