java - How to execute query with union in hibernate? -


hibernate doesn't support union ,so run sql separately. how combine values ?

string query ="select dp.productfamily,dp.productfamilydescr tabel1 dd, tabel2 dp dd.id = 00002 , dd.productfamily null union select dp.divnumber,dp.divdescr tabel1 dd, tabel2 dp dd.id = 00002 , dd.product not null , dd.productfamily not null";  public list<product> findmethod() {         return findallbyquery(query);    } 

please advise how execute 2 sql seperately , how combine values ?

notice each select statement within union must have same number of columns. columns must have similar data types. also, columns in each select statement must in same order.

if true add alias query:

select dp.productfamily productfamily,dp.productfamilydescr productfamilydescr tabel1 dd, tabel2 dp dd.id = 00002 , dd.productfamily null union select dp.divnumber productfamily,dp.divdescr productfamilydescr tabel1 dd, tabel2 dp dd.id = 00002 , dd.product not null , dd.productfamily not null 

you can use sqlquery , aliastobeanresulttransformer in manner:

session.createsqlquery(above sql union).addscalar("productfamily",stringtype.instance).addscalar("productfamilydescr",stringtype.instance).setresulttransformer(new aliastobeanresulttransformer(product.class)) 

product must have emtpy constructor , field accessors.

else, if union intended extract different fields different types have run 2 queries separately addall() second result first!


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -