说,我有两个实体:
@Entity
@Table(name = "PAR")
public class Par implements java.io.Serializable, Simple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", updatable = false, nullable = false)
private int id; // id
@Column(name = "CD", updatable = false, nullable = false)
private String cd; // cd
@Column(name = "NAME", updatable = false, nullable = false)
private String name;
@Column(name = "VAL_TP", updatable = false, nullable = true)
private String tp;
@Column(name = "DATA_TP", updatable = false, nullable = true)
private String dataTp;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCd() {
return this.cd;
}
public void setCd(String cd) {
this.cd = cd;
}
public String getTp() {
return tp;
}
public void setTp(String tp) {
this.tp = tp;
}
public String getDataTp() {
return dataTp;
}
public void setDataTp(String dataTp) {
this.dataTp = dataTp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
@Table(name = "DW")
public class Dw implements java.io.Serializable, Simple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", updatable = false, nullable = false)
private int id;
@Column(name = "S1", updatable = true)
private String s1;
@Column(name = "NS1", updatable = true)
private Double n1;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="FK_HFP", referencedColumnName="ID")
private Par par;
public Dw() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
public Double getN1() {
return n1;
}
public void setN1(Double n1) {
this.n1 = n1;
}
public Par getPar() {
return par;
}
public void setPar(Par par) {
this.par = par;
}
}
我需要找到与par.cd =“TEST1”匹配的“dw”行并获取S1字段的值。
我必须通过dw迭代来完成:
for (Dw dw: dwMng.findAll()) {
if (dw.getPar().getCd()=="TEST1") {
System.out.println("Found row with par.cd:"+dw.getPar().getCd()+" value of S1="+dw.getS1);
}
}
但我认为这样做并不方便。
我怎样才能快速获得这样的排?
例如(不使用dw.getPar()):
for (Dw dw: dwMng.findAll()) {
if (dw.getCdOfPar("Test1")=="TEST1") {
System.out.println("Found row with par.cd:"+dw.getPar().getCd()+" value of S1="+dw.getS1);
}
}
或者可能存在另一种方式来完成这项工作?
我只需要将子字段“嵌入”父级
答案 0 :(得分:0)
在这种情况下,你也可以写一个NamedQuery
。
你可以写下面的内容
String sql = "select * from Employer employer
join Employee employee on employee.employerid = employer.id
where employer.name = :name";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Object.class);
query.setParameter("name", name);
List results = query.list();