0

I have a entity model like this:

public class Facture implements Serializable 
{
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_FACTURE")
private long idFacture;
...

private Panier panier;
    ...
 }

 public class Panier
 {
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_PANIER")
private long idPanier;  

@ManyToOne
private Client client;
@OneToMany
private List<LignePanier> articles = new ArrayList<LignePanier>();
...
 }

 public class Client
 {
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_CLIENT")
private long idClient;
...
  }

So I would like to query all the facture from a client X. I try something like this:

 public List<Facture> listeFacture(Long clientID) {
    List<ParameterMap> parameters = new ArrayList<ParameterMap>();
    parameters.add(new ParameterMap(StandardBasicTypes.LONG, clientID));
    return dao.query("select facture from Facture facture where facture.panier.client.idClient = ?", parameters);
}

I get this exception:

  org.hibernate.QueryException: could not resolve property: client of: be.infoserv.web.model.Facture [select facture from be.infoserv.web.model.Facture facture where facture.panier.client.idClient = ?]

I think it's not possible to query throuth object like this but i don't know how to write this query...

Sorry for my english, i am a french user.

2
  • Do you have proper getter and setters in place?? Commented Aug 1, 2012 at 18:58
  • Yes i have all getter and setters in place Commented Aug 1, 2012 at 19:15

1 Answer 1

1

You may have to use inner joins to do this:

select facture
from Facture facture
     inner join facture.panier as panier
     inner join panier.client as client
where client.clientId = ?

Or use criteria which can be a bit safer since you can't muck up the hql:

Criteria factureCrit = session.createCriteria(Facture.class);
Criteria panierCrit = factureCrit.createCriteria("panier");
Criteria clientCrit = panierCrit.createCriteria("client");
clientCrit.add(Restrictions.idEq(clientId));

return factureCrit.list();
Sign up to request clarification or add additional context in comments.

3 Comments

I think your idea is good but i have this exception when i use a join in the query... stupid hibernate xD! java.lang.NullPointerException at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:396) at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3671)
you could also try "inner join fetch" as well, which might help. But the idea is the same. i use this construct all the time. However, you might be better off with a criteria query. See the edit
Thanks for your help, when i have a @manyToOne mapping on panier in class facture all your solution work! But i don't want this mapping because i would like to freeze the state of my bill. So i finaly do it programmaticaly not so good but it works. I mark your your response as answer ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.