1

I have following as table structure

**BPV table** 
  id, vid, bid

**vt table** 
   vid, name, gender

**uv table**
   uvid, vid, cast,...

i want to write left join between BPV ,uv based on vid in hibernate (mysql)

bpv entity have vt as one to one as well as uv have vt with one to one but there is no bpv &uv in realation

AnilHoney
  • 259
  • 8
  • 20

3 Answers3

2

Hibernate has Criteria API, which is a good way to execute queries in a OO approach. If you have your persistent entities, then you can do this:

Criteria criteria = session.createCriteria(BPV.class, "bpv");
criteria.createCriteria("bpv.vt", "vt", CriteriaSpecification.LEFT_JOIN);
criteria.createCriteria("vt.uv", "uv", CriteriaSpecification.LEFT_JOIN);
// add restrictions
return criteria.list();
Juan Luis
  • 3,317
  • 4
  • 17
  • 23
1

Hibernate SQL:

from BPV where BPV.uv.cast = "your_condition"

It requires properly described entities and dependency;

or pure SQL:

select * from BPV left join uv on BPV.vid = uv.vid where uv.cast = "your_condition"
n00bot
  • 229
  • 1
  • 11
  • you could just do a hibernate native query, you seem to be using sql/hql anyway – NimChimpsky Jun 11 '13 at 09:14
  • here is no realtion between bpv and uv these are both have commonly with vt table so above query not possible – AnilHoney Jun 11 '13 at 09:48
  • bpv entity have vt as one to one as well as uv have vt with one to one but there is no bpv &uv in realation – AnilHoney Jun 11 '13 at 09:49
  • if you described entity with @OneToOne field and FetchType.EAGER, you may simple use HQL "from bpv". It gives you a List, and each BPV will have vt field which have uv field. If you want HQL with condition, use "from bpv where bpv.vt.uv.cast=..." just like an objects in Java – n00bot Jun 13 '13 at 06:25
0

What you're asking for is not posible, there is a Theta Join solution but it only supports inner join, so you have to give up on that approach and do one of the following solutions:

Map the proper relationship:

If this query is a unusual escenario, you dont necesary have to change your actual mapping , you can make a new dto Class with the new mapping, and use that one for this query.

Use native SQL:

This is prettly simple too, use .createSQLQuery(), to run the native query and you can use .addScalar() + .setResultTransformer(Transformers.aliasToBean(someDto.class)) to get a list of your entity.

Ziul
  • 883
  • 1
  • 13
  • 24