Hibernate查找错误表中的列



我有这个查询在本地SQL和Java中的Hibernate中工作。

出现

错误

无法解析属性:entityName

(或amount),当我使用pageable对这两列之一的结果排序时。

Hibernate也无法将项目映射到Page<McConsumerBalanceEntity>并返回对象的pageimpl,所以我使用List<Object[]>手动映射项目,这是有效的。

Hibernate版本是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);
}
}*/
}

由于错误明确指出无法解析property: entityName,这意味着该列不存在于它试图获取的实体中。我可以观察到,在你的SQL本地查询,你正试图从我这里获取它。名称和根据您提供的实体,它存在于McConsumerBalanceEntityGenerated中。因此,尝试连接这个实体并从这个表/实体中获取entityName。

您没有说明您使用的是哪个Hibernate版本或哪个数据库,因此这里的每个人都必须猜测这些问题以及更多问题。如果你想得到好的答案,考虑在你的问题中提供更多的细节,或者付钱给顾问为你做这件事。你在这里问问题的方式就好像你在问一个修理工"我的福特车开得快时发出奇怪的声音,有什么问题吗?">

说了这么多,我将开始猜测,假设你正在使用Oracle。也许你遇到了一个类似于下面的问题:https://discourse.hibernate.org/t/issues-in-migrating-from-hibernate-5-3-3-to-5-3-4/5679/2

Hibernate也无法将项目映射到Page并返回pageimpl的对象,所以我使用List<Object[]>手动映射项目

这是意料之中的。Hibernate不做"推理"。或者类似的东西。如果希望返回McConsumerBalanceEntity,则使用JPQL构造函数表达式。

最新更新