开发者

How to map native query to one model class?

Hi I´m using Eclipselink and I did a native query to select some fields of 2 tables. I mapped my table Logins in a model class. I would not like to map my table "B" because I need only 2 fields of this table on my sql result.. can I map this 2 fields in my Logins table to my sql result ?

My sql is this:

select l.login_id, s.lugarcerto,s.vrum, l.username, l.first_name, l.last_name, l.phone, l.fax_number, l.address, l.zip, 
          l.address2 as 'birth_date', l.city as 'cpf_cnpj'
from Logins l
join (select se.login_id, lugarcerto = min(case when se.service = 'IM' then '1' end), vrum = min(case when se.service = 'VE' then '1' end)
        from (select dist开发者_如何学Goinct ad.login_id, substring(ap.Rate_code,(CHARINDEX('-', ap.Rate_code)+1),2) as 'service'
                from Ad_Data.dbo.ad ad
                join Ad_Data.dbo.ad_pub ap on (ad.ad_id = ap.ad_id)
               where ap.ad_type =1) se
       group by se.login_id) s on (s.login_id = l.login_id)

I did map Logins table and I want to map s.lugarcerto and s.vrum to my SQL query result. There´s anyway to just add it to my Logins model ?


Not without having mappings for the attributes you want those values put into, and not without causing problems with them being cached in the entity.

Why not just return the values beside the entity, much like you would with a JPQL query such as: "Select l, subquery1, subquery2 from Logins l" ie:

 Query q = em.createNativeQuery(yourQueryString, "resultMappingName");

And in the entity, include the annotation:

@SqlResultSetMapping(name="resultMappingName",
entities={@EntityResult(entityClass=com.acme.Logins.class, )},
columns={@ColumnResult(name="LUGARCERTO"), @ColumnResult(name="VRUM")}
)

Best Regards, Chris

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜