sql - How to union multiple tables and perform paging in DB with Nhibernate -
is there way implement union , paging in db?
eg:
(select a.col1 columna, a.col2 columnb, a.col3 columnc table1 a) union (select b.col1 columna, b.col2 columnb, b.col3 columnc table2 b)
i've found several solutions implement function, paging performed in memory, not in db.
1. use sql query
this has several drawbacks, glaring 1 limit , offset not standardized across databases (oracle requires subquery) - if change db, query need changed.
// petdso not nhibernate mapped, no virtual.. public class petdto { public string name { get; set; } public string owner { get; set; } public long age { get; set; } } ilist<petdto> pets = session.createsqlquery(@" select name name, owner owner, age age cat union select dog_name name, owner_name owner, age_in_years age dog order name, owner, age limit :returnedrows offset :skiprows") .setparameter("skiprows", 1) .setparameter("returnedrows", 2) .setresulttransformer(new aliastobeanresulttransformer(typeof(petdto))) .list<petdto>();
2. use view , map it
create view in db , map (probably <class mutable="false">). careful though, need id each row won't change between queries, because nh caching entities using id.
hql doesn't support unions (nh-2710) , should complete way query db.
Comments
Post a Comment