mysql - 查询jpql获取ManyToMany关联生成的表的值

标签 mysql jboss7.x jpql ejb-3.1

我在 mysql 上有由 ManyToMany 关联生成的表。该表是 user_carrier 但当我运行此查询时:

select c from client c
inner join c.utilisateur_transportcat utc 
inner join utc.transportcat tc
inner join tc.transport t
where t.intitule like 'byroad'

我收到此错误:

:table user_carrier is not mapped

我认为这是因为它不是一个类,它只是一个由ManyToMany关联生成的表关联,但我能做什么?

类别承运人是:

@Entity
@Table(name="TransportCat")
public class TransportCat {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer IdTransportCat;
    private String Intitule;
    @Lob @Basic(fetch=FetchType.LAZY, optional=false)
    private String Description;
    private String iconImage;
    private boolean Statut;

    @ManyToOne
    @JoinColumn(name="IdTransport")
    private Transport transport;


    @ManyToMany(mappedBy="listTransportTrCat")
    private List<Client> listClient=new ArrayList<Client>();

表用户是:

@Entity
@DiscriminatorValue(value="client")
public class Client extends Utilisateur{

    //champs client privé
    private String Nom;
    private String Prenom;

    //question 
        @OneToMany(mappedBy="client",fetch=FetchType.LAZY,cascade=CascadeType.ALL,orphanRemoval=true)
        private  List<Question> listeQuestion=new ArrayList<Question>();

    //liste des abonnements d'un utilisateur
        @OneToMany(mappedBy="pk.client",fetch=FetchType.LAZY,orphanRemoval=true)
        private List<Commission_Client> listeCommissionClient=new ArrayList<Commission_Client>();

    /*@OneToMany(mappedBy="pk.client",fetch=FetchType.LAZY)
    private List<Abon_Comm_Client> listeAbonnement=new ArrayList<Abon_Comm_Client>();*/


    //liste des  factures
    @OneToMany(mappedBy="client",fetch=FetchType.LAZY)
    private List<Facture> listeFacture=new ArrayList<Facture>();

    //geolocalisation
    @OneToMany(mappedBy="client")
    private List<geolocalisation> listeGeolocalisations=new ArrayList<geolocalisation>();


    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="IdVille_usr")
    private Ville Ville_Utilisateur;

    //déclaration
    @OneToMany(mappedBy="Transporteur",fetch=FetchType.LAZY)
    private List<Declaration> listeDeclaration=new ArrayList<Declaration>();


    //clients
    @OneToMany(mappedBy="client",fetch=FetchType.LAZY)
    private List<Paiement> listePaiement=new ArrayList<Paiement>();

    private String CodePost_usr;
    private String Rue_usr;
    private String Telephone_usr;
    private String Mobile_usr;
    private Long CID;
    private Date dateCr;
    //commentaire
    @Lob @Basic(fetch=FetchType.LAZY, optional=false)
    private String Comment;

    //champs pour un client proffessionnel

    public List<Paiement> getListePaiement() {
        return listePaiement;
    }


    public void setListePaiement(List<Paiement> listePaiement) {
        this.listePaiement = listePaiement;
    }




    private String position;
    //proffessionnelle ou priv�
    private String Type_client;

    //Transporteur ou expediteur
    private String TranExp;

    //� propos de la soci�t�
    private String Nom_Societe;
    private String Details_Societe;

    @ManyToOne
    @JoinColumn(name="id")
    private Langue langue;

    @ManyToOne
    @JoinColumn(name="idIndustrie")
    private Industrie industrie;

    //type de service local national international pour un transporteur
    private String ServiceType;

    //ville societe
    @ManyToOne
    @JoinColumn(name="IdVille_Societe")
    private  Ville ville_Societe;

    private String CodePost_Societe;
    private String Rue_Societe;

    private String Telephone_societe;
    private String Fax;
    private String Organization_Number;


    @OneToMany(mappedBy="Client",cascade=CascadeType.ALL,orphanRemoval=true,fetch=FetchType.LAZY)
    List<GLN> list_GLN=new ArrayList<GLN>();

    //assurance
    private String Assurance_Societe;
    private String Assurance_Adresse;
    private String Assurance_Policy_Number;
    private String Assurance_montant;
    private String Assurance_Contact_phone;
    private Date Asssurance_Expiration;



    @ManyToOne
    @JoinColumn(name="idVille_Postal")
    private Ville ville_postal;

    private String CodePost_Postal;
    private String Rue_Postal;


    //Expediteur
    private String Web_Site;

    //Transporteur


    @ManyToMany(cascade=CascadeType.ALL)
    private  List<TransportCat> listTransportTrCat=new ArrayList<TransportCat>();

在类中,用户是具有多对多的类中的最后一个关联,并且在类中,载体是最后一个关联 并感谢您的帮助

最佳答案

正如我在评论中所说:JPQL 从不使用表名和列名。始终是实体名称及其字段/关联。

select c from client c

这已经是错误的了。该实体名为 Client,而不是 client

inner join c.utilisateur_transportcat

这又是错误的:Client 类中没有名为 utilisateur_transportcat 的字段。

您只需要客户端实体与其关联的 TransportCat 之间的连接。这两个实体之间的关联定义为

@ManyToMany(cascade=CascadeType.ALL)
private List<TransportCat> listTransportTrCat = new ArrayList<TransportCat>();

所以查询很简单

select c from Client c
join c.listTransportTrCat tc
inner join tc.transport t
where t.intitule = 'byroad'

我强烈建议学习 Java 命名约定并遵守它们。您的所有字段都使用不同的字段:iconImage(正确)、Statut(不正确:应以小写字母开头)、Telephone_societe(不正确:不应该有下划线,应为 TelephoneSociete),CID`(不正确:所有大写字母都保留给常量)。

还要选择一种语言,最好是英语,并坚持下去,而不是随意混合法语和英语单词。对于列表类型的字段,请勿将字段命名为 listXXX。只需使用复数形式:

private List<Paiement> paiements = new ArrayList<Paiement>();

不要使用缩写。 TransportCat 没有任何意义。如果 Cat 表示类别,则将该类命名为 TransportCategory。

正确命名事物非常重要。如果您以不同的方式命名所有字段,您将不断犯错误,因为您永远不会记得如何命名它们。而且代码读起来很痛苦。

关于mysql - 查询jpql获取ManyToMany关联生成的表的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340203/

相关文章:

mysql - 连接字符串修改(替换、连接、修剪)

mysql - 如何避免同一个表上的多个 JOINS?

security - 在企业 bean 中使用 getUserPrincipal() 和 isCallerInRole() 方法时如何编写 JUnit 测试?

java - 为什么不保留新实体?

JPQL NEW 运算符与 native 查询?

mysql - MYSQL数据库中选定行的大小计算

python - 如何将数据追加到现有数据库,而不影响已存在的数据?

jakarta-ee - jboss/wildfly 中的缓存连接管理器

java - 如何在JBoss AS7中使用hibernate-infinispan的write Behind方法

java - QueryDSL:格式化字符串的 DateTimePath