简体   繁体   中英

Hibernate HQL Left Join Query

I have two tables which I need to be joined but there in no relation specified in the entity . Can I write something like

select uc.id, uc.name, mpn.name from UCR uc, MpnMapping mpn

I'm getting an error called Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected token

Yes, you can do it by specifing the join in the where clause. It must be something like this:

select a, b from A a, B b where a.joinColumn = b.joinColumn

You can see more info in the below links:

Joining two unrelated tables in hibernate

http://www.codewrecks.com/blog/index.php/2009/09/04/theta-join-in-hql-join-with-unrelated-entities/

Yes you can use new operator and a DTO class to set the values..

Query

 select new com.example.UCRMNP(uc.id, uc.name, mpn.name) from UCR uc, MpnMapping mpn;

DTO class

First can create a DTO class and specify all the column variables, like this

class UCRMNP{
 int id;
 String name;
 String name1;
 public UCRMNP( int id, String name, String name1){
 this.id=id;
 this.name=name;
 this.name1=name1;
}
}

Now you can set the retrieved data to that specific DTO class, You dont want to annotate it with anything, just specify the constructor and execute the Query using new operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM