简体   繁体   English

在JPA中联接2个表的CreateQuery

[英]CreateQuery joining 2 tables in JPA

I have this error 我有这个错误

The collection-valued path 'c.medecin' cannot be resolved to a valid association field
The state field path 'm.id' cannot be resolved to a valid type.

when I execute this request 当我执行此请求时

createQuery("select c from Creneaux c join c.medecin m where m.id=:idMedecin").setParameter("idMedecin", medecin.getId());

I use these 2 tables : MEDECINS(ID) and CRENEAUX(ID, ID_MEDECIN) 我使用以下两个表:MEDECINS(ID)和CRENEAUX(ID,ID_MEDECIN)

@Entity
@Table(name = "medecins")
@XmlRootElement
public class Medecins implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Long id;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "medecin"/*"idMedecin"*/)
    private transient List<Creneaux> creneauxList;
}

and

@Entity
@Table(name = "creneaux")
public class Creneaux implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Long id;
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="ID_MEDECIN")
    private transient Medecins medecin;

    @Column(name = "ID_MEDECIN")
    private BigInteger idMedecin;

I begin in JPA, so I a not sure about all the code. 我从JPA开始,所以我不确定所有代码。 I think the Query is correct, but I don't know how to annotate the entities to make the Query valid. 我认为查询是正确的,但是我不知道如何注释实体以使查询有效。 Thanks 谢谢

I wonder why adding the last attribute in the second Entity that is: idMedecin while you've already joined the two entities to each others. 我想知道为什么要在第二个实体中添加最后一个属性: idMedecin而您已经将两个实体彼此连接了。 Maybe you should omit it. 也许你应该忽略它。 If the purpose of the query is to select all the creneaux related to the given medecin, then you ought to change the query to: 如果查询的目的是选择与给定的医学相关的所有克里尼奥酒,那么您应该将查询更改为:

createQuery("SELECT c FROM Creneaux c WHERE c.medecin.id = :idMedecin").setParameter("idMedecin", medecin.getId());

Where medecin.getId() should be provided before the query's execution. 执行查询之前应在何处提供medecin.getId()

That's because the Creneaux.medecin field is transient . 这是因为Creneaux.medecin字段是transient Transient fields are ignored by JPA. JPA将忽略瞬态字段。

Another thing is that you don't have two join these two entities. 另一件事是这两个实体没有两个联接。 If you want to filter by Medecin ID it's enough to execute a query as @Omar described. 如果要按Medecin ID进行过滤,则足以执行@Omar描述的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM