Consulta personalizada de Hibernate para la pantalla de búsqueda

I have search screen which has 10 fields and involves around 5 to 6 tables to get the result. I am writing a custom SQL query and appending where condition only when a value exists.

Can someone please suggest is this the right approach or any other better way.

I cannot use any plugins or additional APIs for my situation.

StringBuffer sqlQuery = new StringBuffer();
sqlQuery.append (" select member.* from member, customer, case where customer.status='A');
if(firstName != null)
sqlQuery.append("customer.firstName= :firstName");

if(caseid != null)
sqlQuery.append("case.caseid =:caseid");

SQLQuery queryObj = createSQLquery(sqlQuery.toString());
queryObj.list();

mientras que member, customer, case are actual tables.

ACTUALIZACIÓN

when I am adding where condition to string buffer

if(caseid != null)
sqlQuery.append(" and case.caseid =:caseid");

and when queryObj is formed I am doing it as
if(caseid != null)
queryObj.setString("caseid", caseid);

is there better way to add after queryObj (to combine both)?

Gracias de antemano.

preguntado el 12 de junio de 12 a las 17:06

1 Respuestas

No, it's not necessarily the right approach. SQL is not the query language of choice when using Hibernate. HQL should be preferred when possible. And Hibernate also has la API de criterios which has been designed precisely to build dynamic queries.

Respondido el 12 de junio de 12 a las 17:06

using Criteria as I told I have more than 5 tables I don't know how to refer in each other. And if I refer, some of them are one to many, many to one, many to many. I am kind of using HQL, edited the question again, please check and let me know. Thanks - changeme

docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/…. I can't do more than linking to the documentation. Try to read it, understand it, and experiment. You won't be able to be a criteria wizard in 3 minutes time. If you're stuck, post a question explaining precisely what you're trying to do, and join the relevant code and mapping of the entities involved. - JB Nizet

can you please see the update once and help me with that little bit more? - changeme

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.