我正在为我的API使用dropwizard框架。我有一个命名查询如下:
@NamedQuery(name = "com.myapp.entity.MyEntity.findByMatchId", query = "SELECT test.name, count(test) as count FROM MyEntity as test,Entity1 as d where test.drug=d.id and test.drug.id= :drugId group by test.name") })
我将count(test)
函数的结果作为名称count
返回。以下是我的实体类:
@NamedQuery(name = "com.myapp.entity.MyEntity.findByMatchId", query = "SELECT test.name, count(test) as count FROM MyEntity as test,Entity1 as d where test.drug=d.id and test.drug.id= :drugId group by test.name") })
@Entity
@Table(name = "my_entity")
@NamedQueries({
@NamedQuery(name = "com.myapp.entity.MyEntity.findByMatchId", query = "SELECT test.name, count(test) as count FROM MyEntity as test,Entity1 as d where test.drug=d.id and test.drug.id= :drugId group by test.name") }))
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@JsonBackReference("drug_id")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "drug_id", nullable = false)
private Drug drug;
@Column(name = "timestamp", nullable = false)
private Date timestamp;
// This value is not getting mapped
private Long count;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Drug getDrug() {
return drug;
}
public void setDrug(Drug drug) {
this.drug = drug;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public SideEffectSeverity getSideEffectSeverity() {
return sideEffectSeverity;
}
public void setSideEffectSeverity(SideEffectSeverity sideEffectSeverity) {
this.sideEffectSeverity = sideEffectSeverity;
}
public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
}
我可以在这里做什么来获得正确的映射,其中count函数的值为count。
为什么我的映射没有正确发生,我是否必须给它一些注释?
答案 0 :(得分:2)
您是否从上述查询中获得了任何结果?
如果是,那么请注意,count()函数的返回类型将很长。所以希望你试图用DTO映射响应(它应该包含两个字段作为名称和计数)。
答案 1 :(得分:0)
您使用的是OracleSQL还是POSTgreSQL?
Count是一个保留字,重命名你的列会像这样修复
std::function