java - 无法解析实体的属性

标签 java hibernate spring-data-jpa single-page-application

我有这些实体配置:

主表:

@Entity
@Table
public class Merchants {

    @Id
    private int id;

    @OneToMany(mappedBy = "merchants")
    private List<Contracts> contracts;
}

@Entity
@Table
public class Contracts {

    @Id
    private int id;

    @Column(length = 255)
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "merchant_id")
    private Merchants merchants;
}

我使用这个 JPA 查询:

String hql = "select e from " + Contracts.class.getName() + " e where e.merchant_id = ?";

        Query query = entityManager.createQuery(hql).setParameter(0, merchantId);

当我部署包时,表列merchant_id 存在。 但我在 SQL 查询期间遇到异常:

Caused by: org.hibernate.QueryException: could not resolve property: merchant_id of: org.rest.api.entity.Contracts [select e from org.rest.api.entity.Contracts e where e.merchant_id = ?]

您知道为什么merchant_id没有映射吗?

最佳答案

需要使用商户id。

试试这个:

String hql = "select e from " + Contracts.class.getName() + " e where e.merchants.id = ?";

关于java - 无法解析实体的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51696119/

相关文章:

java - 我如何说服编译器一个对象是可克隆的(java)?

java - struts、hibernate、mysql - 字符编码问题

java - Google App Engine : Prevent Duplicate values, 多个字段键

mysql - 在我的 SQL 数据库上执行查询时连接被拒绝

mysql - 使用 Criteria API : slow performance in MySql 的 Hibernate 子选择

java - Spring JPA 存储库中的 CrudRepository

Spring Boot JPA CrudRepository

java - 为 JVM 生成 .class 文件

java - 未知 SMTP 主机 : smtp. live.com

java - 从 Spring Data 中的多个表中进行选择