6

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

调制点:

@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;

模组范围类型:

@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 记录的实体?

本机 SQL 查询:

SELECT ID, NAME FROM MOD_PM WHERE ID IN (SELECT PM_ID FROM MOD_PM_SCOPE_TYPES WHERE SCOPE_TYPE_ID=1)

如何将此查询转换为 JPQL?

4

1 回答 1

18

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

试试这个:

select p from ModPm p join p.modScopeTypes type where type.scopeTypeId = 1
于 2013-03-01T10:09:38.753 回答