【发布时间】:2021-11-04 07:46:14
【问题描述】:
我有这个查询可以在原生 SQL 和 Java 中的 Hibernate 中使用。
错误提示
无法解析属性:
entityName...
(或数量)当我使用 pageable 对这两列之一的结果进行排序时。
Hibernate 也无法将项目映射到 Page<McConsumerBalanceEntity> 并返回 pageimpl 的对象,所以我使用 List<Object[]> 手动映射项目,这是可行的。
休眠版本是 5.3.7。数据库是 PostgreSQL 10.5。 Spring Boot 是 2.1.0
@Query(
value = "select csr.id, csr.firstName, csr.lastName, " +
"sum (case " +
"when acs.type = 'PAYMENT' then acs.amount " +
"else -acs.amount " +
"end) as amount, " +
"me.id as entityId, " +
"me.name as entityName " +
"from McConsumerEntity csr, EbAccountConsumerEntity acs, McEntity me " +
"where csr.idMcEntity in :childEntityIds " +
"and csr.idMcEntity = me.id " +
"and csr.id = acs.idMcConsumer " +
"and amount > 0 " +
"group by csr.id, me.id, me.name ")
public class McConsumerBalanceEntityGenerated implements Serializable {
private static final long serialVersionUID = 217L;
@Id
@GeneratedValue(generator = "sequence_null", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "sequence_null",
sequenceName = "sequence_null", allocationSize = 100)
protected Long id;
@Column(name = "amount")
protected BigDecimal amount;
@Column(name = "entity_id")
protected Long entityId;
@Column(name = "entity_name")
protected String entityName;
@Column(name = "first_name")
protected String firstName;
@Column(name = "last_name")
protected String lastName;
//---------------------------------------------------------------------
public McConsumerBalanceEntityGenerated() {
super();
}
/*
//---------------------------------------------------------------------
private McConsumerBalanceEntity(Builder builder) {
this.id = builder.id;
}
*/
//---------------------------------------------------------------------
public Long getId() {
return id;
}
/**
* Access method for the amount property.
*
* @return the current value of the amount property
*/
public BigDecimal getAmount() {
return this.amount;
}
/**
* Sets the value of the amount property.
*
* @param aAmount the new value of the version property
*/
public void setAmount(BigDecimal aAmount) {
this.amount = aAmount;
}
/**
* Access method for the entityId property.
*
* @return the current value of the entityId property
*/
public Long getEntityId() {
return this.entityId;
}
/**
* Sets the value of the entityId property.
*
* @param aEntityId the new value of the version property
*/
public void setEntityId(Long aEntityId) {
this.entityId = aEntityId;
}
/**
* Access method for the entityName property.
*
* @return the current value of the entityName property
*/
public String getEntityName() {
return this.entityName;
}
/**
* Sets the value of the entityName property.
*
* @param aEntityName the new value of the version property
*/
public void setEntityName(String aEntityName) {
this.entityName = aEntityName;
}
/**
* Access method for the firstName property.
*
* @return the current value of the firstName property
*/
public String getFirstName() {
return this.firstName;
}
/**
* Sets the value of the firstName property.
*
* @param aFirstName the new value of the version property
*/
public void setFirstName(String aFirstName) {
this.firstName = aFirstName;
}
/**
* Access method for the lastName property.
*
* @return the current value of the lastName property
*/
public String getLastName() {
return this.lastName;
}
/**
* Sets the value of the lastName property.
*
* @param aLastName the new value of the version property
*/
public void setLastName(String aLastName) {
this.lastName = aLastName;
}
/* //---------------------------------------------------------------------
public static class Builder {
private Long id;
public Builder setId(Long id) {
this.id = id;
return this;
}
public McConsumerBalanceEntity build() {
return new McConsumerBalanceEntity(this);
}
}*/
}
【问题讨论】:
-
您的意思是在 Hibernate 中可能不起作用?你在本机 SQL 作品和 Hibernate 作品中都写了这个
-
我们需要查看
McConsumerBalanceEntity的定义。请注意,List<Object>可能是此处的标准方式,因为您的查询在逻辑上不返回单个实体。 -
@Rollerball 如果我不按这两列排序(当我按 id 排序时)它会起作用
-
@TimBiegeleisen McConsumerBalanceEntity 扩展自 McConsumerBalanceEntityGenerated 并实现了可序列化。我现在将发布代码
-
存储库方法是什么样的?另外,请发布完整的堆栈跟踪。
标签: java hibernate hibernate-mapping