猿问

JPA:外键作为 NamedQuery 中的参数

我正在尝试Raadpleging从我的数据库中返回对象。这些对象包含 aPersoon和 a Les。我的目标是返回所有 Raadplegingen,其中 Les 的 ID 等于参数。


我不确定如何在我的查询中执行此操作。我也不太确定我的第一个 namedQuery 是否也会返回 Persoon 和 Les 属性,或者我是否需要一个连接。


正如您在下面看到的,我尝试使用这样的参数WHERE r.les  = :lesId,我认为这是不正确的。


@Entity

@Access(AccessType.PROPERTY)

@Table(name = "Raadplegingen")

@NamedQueries({

    @NamedQuery(name = "ClubRaadpleging.getAlleRaadplegingen",

            query = "SELECT r FROM ClubRaadpleging r"),

    @NamedQuery(name = "ClubRaadpleging.findByLes",

            query = "SELECT r FROM ClubRaadpleging r JOIN ClubLes l ON l.id =r.les.id WHERE r.les  = :lesId")

})

public class ClubRaadpleging implements Serializable {


    private int id;

    private ClubLes les;

    private ClubPersoon persoon;

    private Date datum;


    public ClubRaadpleging() {

    }


    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(name = "RaadplegingId")

    public int getId() {

        return id;

    }


    public void setId(int id) {

        this.id = id;

    }


    @ManyToOne(cascade = CascadeType.PERSIST)

    @JoinColumn(name = "LesId")

    public ClubLes getLes() {

        return les;

    }


    public void setLes(ClubLes les) {

        this.les = les;

    }


    @ManyToOne(cascade = CascadeType.PERSIST)

    @JoinColumn(name = "PersoonId")

    public ClubPersoon getPersoon() {

        return persoon;

    }


    public void setPersoon(ClubPersoon persoon) {

        this.persoon = persoon;

    }


    @Temporal(TemporalType.DATE)

    public Date getDatum() {

        return datum;

    }


    public void setDatum(Date datum) {

        this.datum = datum;

    }


}


慕尼黑5688855
浏览 115回答 1
1回答

慕工程0101907

ClubRaadpleging和之间不需要加入ClubLes。根据实体映射,JPA 本身已经完成了这项工作。您可以尝试以这种方式简化 NamedQuery:@Entity@Access(AccessType.PROPERTY)@Table(name = "Raadplegingen")@NamedQueries({    @NamedQuery(name = "ClubRaadpleging.getAlleRaadplegingen",            query = "SELECT r FROM ClubRaadpleging r"),    @NamedQuery(name = "ClubRaadpleging.findByLes",            query = "SELECT r FROM ClubRaadpleging r WHERE r.les.id  = :lesId")})public class ClubRaadpleging implements Serializable {
随时随地看视频慕课网APP

相关分类

Java
我要回答