我有2个具有“多对多”关联的实体类。

ModPm:

@Entity
@Table(name = "MOD_PM")
public class ModPm extends WebPageObject implements Serializable, IDBNamedEntity {

    private static final long serialVersionUID = 1L;

    public final static String Q_GET_WITHOUT_STATUS_FOR_SCOPE = "ModPm.getWithoutStatusForScope";

    @Id
    private long id;
    ....
    @ManyToMany
    @JoinTable(name = "MOD_PM_SCOPE_TYPES",
            joinColumns = @JoinColumn(name = "PM_ID"),
            inverseJoinColumns = @JoinColumn(name = "SCOPE_TYPE_ID")
    )
    private List<ModScopeType> modScopeTypes;

ModScopeType:
@Entity
@Table(name = "MOD_SCOPES")
@Cacheable
public class ModScopeType extends WebPageObject implements Serializable {

    private static final long serialVersionUID = 1L;

    public final static String Q_GET_ALL = "ModScopeType.getAll";

    @Id
    @Column(name = "ID")
    private long id;
    ....
    //bi-directional many-to-many association to ModPm
    @ManyToMany
    @JoinTable(name = "MOD_PM_SCOPE_TYPES",
            joinColumns = @JoinColumn(name = "SCOPE_TYPE_ID"),
            inverseJoinColumns = @JoinColumn(name = "PM_ID")
    )
    private List<ModPm> modPms;

是否可以从MOD_PM表中选择已在MOD_PM_SCOPE_TYPES表中记录了SCOPE_TYPE_ID = 1的实体?

native SQL查询:
SELECT ID, NAME FROM MOD_PM WHERE ID IN (SELECT PM_ID FROM MOD_PM_SCOPE_TYPES WHERE SCOPE_TYPE_ID=1)

如何将该查询转换为JPQL?

最佳答案

当您在查询中引用SCOPE_TYPE_ID时,您指的是什么?可能是ScopeType的ID或我没有看到的其他字段。我假设范围类型ID字段丢失。

尝试这个:

select p from ModPm p join p.modScopeTypes type where type.scopeTypeId = 1

08-28 16:19