我问的是WHERE子句。我搜索了互联网,但我没有找到我的问题的答案。
我有几个实体类,我在下面代表:
tranformed_rows
我的问题是为什么这个查询不能正常工作。返回的列表是空的,但是数据库中存在记录,当我想要返回所有Trasa记录时,一切正常,只有在我添加带有raport属性的WHERE子句时才起作用。
@NamedQuery(name = "selectTrasy",
query = "SELECT t FROM Trasa t WHERE t.raport = :raport ORDER BY t.id")
@Entity
public class Trasa implements Serializable {
@Id
@Column(name = "id", unique = true )
@GeneratedValue
private Long id;
@Column(name = "data_z")
private Date dataz;
@Column(name = "data_do")
private Date datado;
@Column(name = "czas_z")
private String czasZ;
@Column(name = "czas_do")
private String czasDo;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "miejscowosc_z")
private MiejscowoscDB miejscowoscZ;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "miejscowosc_do")
private MiejscowoscDB miejscowoscDo;
@OneToOne(fetch = FetchType.LAZY)
private Uzytkownik user;
@OneToOne(fetch = FetchType.LAZY)
private Raport raport;
@Override
public boolean equals(Object other) {
return (other != null && getClass() == other.getClass() && id != null)
? id.equals(((Trasa) other).id)
: (other == this);
}
@Override
public int hashCode() {
return (id != null)
? (getClass().hashCode() + id.hashCode())
: super.hashCode();
}
//next I have getters and setters
}
@Entity
public class Raport implements Serializable {
@Id
@Column(name = "id", unique = true )
@GeneratedValue
private Long id;
@Column(name = "data")
private Date data;
@Column(name = "nazwa")
private String nazwa;
@Enumerated(EnumType.STRING)
private Powod powod;
@OneToOne(fetch = FetchType.LAZY)
private Uzytkownik uzytkownik;
@Override
public boolean equals(Object other) {
return (other != null && getClass() == other.getClass() && id != null)
? id.equals(((Raport) other).id)
: (other == this);
}
@Override
public int hashCode() {
return (id != null)
? (getClass().hashCode() + id.hashCode())
: super.hashCode();
}
//getters and setters
}
答案 0 :(得分:0)
将您的查询更改为:
@NamedQuery(name = "selectTrasy",
query = "SELECT t FROM Trasa t WHERE t.raport.id = :raportId ORDER BY t.id")
然后像这样运行:
trasy = (List<Trasa>)(manager.createNamedQuery("selectTrasy")
.setParameter("raportId", propertyWydatek.getRaport().getId())
.getResultList());
其他版本的查询:
@NamedQuery(name = "selectTrasy",
query = "SELECT t FROM Trasa t join t.raport r WHERE r.id = :raportId ORDER BY t.id")