【问题标题】:Hibernate looking for column in wrong table休眠在错误的表中寻找列
【发布时间】: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


【解决方案1】:

由于错误明确指出无法解析属性:entityName,这意味着该列在它尝试获取的实体中不存在。我可以观察到,在您的 SQL 本机查询中,您正试图从 me.Name 获取它,并且根据您提供的实体,它存在于 McConsumerBalanceEntityGenerated 中。所以尝试加入这个实体并从这个表/实体中获取实体名称。

【讨论】:

  • 这个生成的实体是@MappedSuperclass,扩展实体是@Entity,但它们都不是@Table,所以我认为我不能加入它,而且它不是数据库中的表。我在这里尝试的是通过将表 3 组合在一个不在 DB 中的表作为要在 FE 上解析的数据来临时返回表。我希望这能解释一下。
  • 另外,我试图获取的 me.name 存在于 McEntity 中,因此我可以从那里获取它而无需加入您建议的实体
【解决方案2】:

您并没有说您正在使用哪个 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 手动映射项目。

这是意料之中的。 Hibernate 不做“推理”或类似的事情。如果要返回 McConsumerBalanceEntity,请使用 JPQL 构造函数表达式。

【讨论】:

  • 抱歉信息不足。休眠版本是 5.3.7。数据库是 PostgreSQL 10.5。 Spring Boot 是 2.1.0
猜你喜欢
  • 2011-05-28
  • 2017-03-05
  • 1970-01-01
  • 2011-10-25
  • 2012-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多