简体   繁体   中英

Small troubles with JPA Query

I have encountered a problem since one hour..take me as a beginner please..

The query is very simple :

public List<String> finArticleByfamille(String famille){
EntityManager em = getEntityManager();
Query qr= em.createQuery("SELECT a.libel FROM Article a WHERE a.famille_idfamille =   '"+famille+"'");

return qr.getResultList();
}

also I have tried this :

public List<String> finArticleByfamille(String famille){
EntityManager em = getEntityManager();
Query qr= em.createQuery("SELECT a.libel FROM Article a WHERE a.familleIdfamille =    '"+famille+"'");

return qr.getResultList();
}

"famille_idfamille" is a foreign key from the table "Famille", see the screenshot:

在此处输入图片说明

the error comes from this call:

String famille=famille_produit_fact_direct.getSelectedItem().toString();
l_article=(Vector) ajc.finArticleByfamille(famille);

I get this Error :

Grave: null
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: An     exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [SELECT a.libel FROM Article a WHERE    a.famille_idfamille = 'famille2'], line 1, column 38: unknown state or association field   [famille_idfamille] of class [glob.entitys.Article].
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
at java.util.concurrent.FutureTask.get(FutureTask.java:111)
at javax.swing.SwingWorker.get(SwingWorker.java:602)
at glob.main$32.done(main.java:2068)
at javax.swing.SwingWorker$5.run(SwingWorker.java:737)

I respected the same name as the database, so why it does not work ? (sorry for the french language)

I rather prefer to use:

em.createNamedQuery(namedQuery);

in the case your are using a namedQuery inside the Entity Class.

Or, if you wish a more specific query, you can create a native query using your´s database engine native sintax

em.createNativeQuery(Query);

It sounds like your glob.entitys.Article is missing the famille_idfamille field that you are trying to reference. The query is conceptually against the Java objects, not the database directly.

Based on your database, the missing field should probably look something like:

@Basic
String famille_idfamille;

Although the name suggests it might be a foreign key? In which case mapping it as a String might not be appropriate. That might be something to look at later though.

@Tim Gage thank you, I just noticed something.. in my entity glob.entitys.Article I have familleIdfamille as field :

 @JoinColumn(name = "FAMILLE_IDFAMILLE", referencedColumnName = "IDFAMILLE", nullable = false)
 @ManyToOne(optional = false)
 private Famille familleIdfamille;

Famille it is another entity.. then it is normal that he does not know famille_idfamille ?

EDIT:

this comment its from the documentation :

  • The query string refers to the Employee entity and not to the table Employee of the Database...

Then I put this:

Query qr= em.createQuery("SELECT a.libel FROM Article a WHERE  a.familleIdfamille.idfamille = '"+famille+"'");

and its worked; it is logical that it does not work, because the familleIdfamille is an instance of Famille entity...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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