我有一个表,例如 Instrument,其中包含 ID、State 和 User_ID 作为列。
所以我有这个 JPA 查询来返回所有具有匹配的仪器记录
用户身份。
query = manager.createQuery("SELECT instrument from Instrument instrument
where instrument.User_ID=:User_ID",Instrument.class);
query.setParameter("User_ID", User_ID);
List<Instrument> instruments= query.getResultList();
for(Instrument instrument:instruments){
System.out.println("Instrument ID "+instrument.getID());
// using sysout as it is not prod code yet
}
它仅返回重复次数与匹配记录相同的第一条记录。
11:13:01,703 INFO [stdout] (http-/127.0.0.1:8080-1) Instrument ID 1
11:13:01,704 INFO [stdout] (http-/127.0.0.1:8080-1) Instrument ID 1
11:13:01,704 INFO [stdout] (http-/127.0.0.1:8080-1) Instrument ID 1
我在 Db 中有 3 条记录,仪器 ID 为 1,2 和 3
我在 hibernate 上启用了 show sql query 并且查询直接在数据库上运行良好
并返回不同的记录。
休眠查询:
select instrumentjdo0_.User_ID as member_U1_0_, instrumentjdo0_.ID as ID2_0_,
instrumentjdo0_.state as state4_0_ from instrument instrumentjdo0_ where instrumentjdo0_.User_ID=?
仪器实体
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "instrument")
public class Instrument{
@Id
@Column(name="User_ID", length=9, unique=true, nullable=false)
String user_ID;
@Column(name="ID",nullable=false)
String ID;
@Column(name="state",nullable=false)
String state;
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getUserID() {
return user_ID;
}
public void setUserID(String userID) {
this.user_ID = userID;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
不知道我错过了什么。