首页 > 解决方案 > 休眠在错误的表中寻找列

问题描述

我有这个查询可以在原生 SQL 和 Java 中的 Hibernate 中使用。

它打破了错误,说

无法解析属性:entityName……</p>

(或数量)当我使用 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);
        }
    }*/

}

标签: javahibernatehibernate-mapping

解决方案


你并不是说你正在使用哪个 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 构造函数表达式。


推荐阅读