-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate2 + SQL + inner join
PostPosted: Fri Nov 25, 2005 8:46 am 
Newbie

Joined: Mon Nov 14, 2005 6:28 am
Posts: 9
I have a J2EE application with Hibernate2.

I need execute a SQL like this:

Code:
select ac.*
from actividades ac, actividades_linea li
where ac.ACTI_CODIGO_ACTIVIDAD = li.ACLI_CODIGO_ACTIVIDAD
and li.ACLI_CODIGO_LINEA = 'CC'


OR

Code:
SELECT ac.* FROM ACTIVIDADES ac inner join ACTIVIDADES_LINEA li ON li.ACLI_CODIGO_ACTIVIDAD = ac.ACTI_CODIGO_ACTIVIDAD WHERE li.ACLI_CODIGO_LINEA = 'CC'


I have proven with createSQLQuery function:

Code:
sql = "";
sql += "SELECT {ac}.ACTI_CODIGO_ACTIVIDAD AS {ac.codigoAC} ";
sql += "FROM ACTIVIDADES {ac} ";
sql += "inner join ACTIVIDADES_LINEA {li} ON {li}.ACLI_CODIGO_ACTIVIDAD = {ac}.ACTI_CODIGO_ACTIVIDAD ";
sql += "WHERE {li}.ACLI_CODIGO_LINEA = 'CC'";
         
Query storeQuery = sess.createSQLQuery(sql,  new String[] {"ac","li"}, new Class[] { Actividad.class, ActividadLinea.class} );         

List storeList = storeQuery.list();


But jump a Exception.

There is some simple method to execute a SQL with joins?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 4:44 pm 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
You could use the HQL isntead of SQL:
Code:
select ac
from Actividad ac
left join ac.lines


The assumption is that Actividad and ActividadLinea have a "one-to-many" / "many to many" / "one-to-one" / "many to one" relationship


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 3:10 am 
Newbie

Joined: Mon Nov 14, 2005 6:28 am
Posts: 9
Actividad and ActividadLinea have a many to many relationship, but I dont have this relationship in hibernate mappings, and now I dont change this.

I have to execute a native SQL or PL/SQL from my application with hibernate.

Any suggest?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 4:48 am 
Newbie

Joined: Mon Nov 14, 2005 6:28 am
Posts: 9
Finally i have a solution:

Code:
String sql = "SELECT {acti.*} FROM acti_realizadas acti INNER JOIN acti_real_lineas li ON li.ACLI_CODIGO_ACTIVIDAD = acti.ACTI_CODIGO_ACTIVIDAD WHERE li.ACLI_CODIGO_LINEA = 'CC'";
      
sess = sf.openSession();                  
list = sess.createSQLQuery(sql, "acti", Actividad.class).list();
sess.close();


Thanks anandbn :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.