有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法从实体解析属性

我有以下配置:

主表:

@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 = ?]

你知道为什么商家id没有地图吗


共 (1) 个答案

  1. # 1 楼答案

    需要使用商户id

    试试这个:

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