我有以下hibernate
查询字符串:
String queryString = "select \r\n" +
"cms.my_status_id as 'myStatusId',\r\n" +
"cms.status_label as 'statusLabel',\r\n" +
"csl.status_id as 'companyStatusLabel'\r\n" +
"from "+client+".corresponding_status cms \r\n" +
"join "+client+".company_status_label csl on csl.status_id = cms.my_status_id";
我的对应实体是:
@Entity(name = "corresponding_status")
@Table(name = "corresponding_status")
public class CorrespondingStatus implements Serializable {
@Id
@JsonProperty
@Column(name = "my_status_id")
private Integer myStatusId;
// varchar(255)
@JsonProperty
@Column(name = "status_label")
private String statusLabel;
@JsonProperty
@Transient
private String companyStatusLabel;
但是,当我运行查询时,我得到:
Column 'my_status_id' not found
即使它确实在数据库中。
这是什么问题?
答案 0 :(得分:2)
在HQL中,必须使用属性而不是数据库列名称。将您的HQL更改为
String queryString = "select \r\n" +
"cms.myStatusId as 'myStatusId',\r\n" +
"cms.statusLabel as 'statusLabel',\r\n" +
"csl.status_id as 'companyStatusLabel'\r\n" +
"from "+client+".corresponding_status cms \r\n" +
"join "+client+".company_status_label csl with csl.status_id = cms.myStatusId";
编辑: 您可能需要相应地更改company_status_label实体
EDIT2:更改为WITH
答案 1 :(得分:0)
我建议不要使用criteria API来手动构建JPA查询。您上面的查询将更改为:
String queryString = "select \r\n" +
"cms.my_status_id as 'myStatusId',\r\n" +
"cms.status_label as 'statusLabel',\r\n" +
"csl.status_id as 'companyStatusLabel'\r\n" +
"from "+client+".corresponding_status cms \r\n" +
"join "+client+".company_status_label csl on csl.status_id = cms.my_status_id";
类似于:
Session session = HibernateUtil.getHibernateSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Entity> cq = cb.createQuery(Entity.class);
Root<Entity> root = cq.from(Entity.class);
cq.select(root);
Query<Entity> query = session.createQuery(cq);
List<Entity> results = query.getResultList();