The SQL query is as follows,
select s1.*
from Sample1 s1,Sample2 s2 where
s1.field1=s2.field4 and
s2.field2='XXYYZZ'
Table Structure
- The table Sample1 has only three fields ( field1, field2, field3 )
- The table Sample2 h开发者_如何转开发as three fields ( field4, field5, field6 )
And the Bean Names are
Sample1Bean
, Sample2Bean
I want the data only from the Sample1 only, (field1, field2, field3). How can I do this using Hibernate without HQL and Using Criteria class?
SELECT s1 FROM Sample1 s1, Sample2 s2 WHERE s1.field1 = s2.field2 AND s2.field2 = 'XXYYZZ'
It will return object of type Sample1 as result.
List s1 = session.createCriteria(Sample1.class)
.createCriteria("field1")
.add(Restrictions.eq("field2", "XXYYZZ")).
.list();
Hibernate make queries on objects not on table. So the association between Sample1 and Sample2 should be mapped.
Obs:
- s1 will have a list of Sample1.
- "field1" should be a attribute in Sample1 of type Sample2.
精彩评论