Duvida Com JPA

2 respostas
jonasjgs2

boa tarde a todos…
eu tenho uma função no banco de dados que recebe alguns parâmetros e me retorna algumas colunas como se fosse uma tabela…
esta função recebe por exemplo os parâmetros:

estadodeorigem
estadodedestino
código fiscal

e retorna

percicms
reduçãodeicms
mensagem

eu tenho uma tabela de produtos onde eu tenho os campos:

produtoid int
descricao varchar
preco numeric(10,2)
natfiscal varchar

meu select jpql ficaria assi:

select p from produtos p order by p.descricao

o problema e que preciso dos impostos:

select p, função(“sp”,“sp”,“5102”,p.natfiscal) from produtos p

como eu faria isso
não posso utilizar native sql teria que arrumar algum jeito de fazer isso com o jpa)

desde já agradeço…

2 Respostas

Hebert_Coelho

Você terá que usar o JPA 2.1, pois a versão 2.0 não dá suporte a StoredProcecudre.

Tente algo como isso aqui: http://dreamand.me/java/java-jee7-jpa-stored-procedure-example/

jonasjgs2

Hebert, obrigado pela dica… mas ainda não consegui…
este exemplo ai que vc me passou e para executar uma função simples do banco…

o meu problema e que a minha função e uma função que me retorna uma linha como se fosse uma tabela…

no comando do sqlserver eu executo ela assim:

select * from pegaicms(“paramtros…”)

e esta função me retorna uma linha com dados

para melhorar e ver se algum entente melhor e o seguinte:
esta função ela me retorna a tributação de um produto…
os parâmetros passados para a mesma e da linha da tabela do produto…

então o select ficaria assim

select * from produtos as p, pegaicms(p.parametro1,p.parametro2,etc…)

este select ele funciona no banco normalmente…

segue abaixo a descrição da função… para que vcs imaginem que e realmente uma necessidade…
já esto trabalhando com o eclipselink e o jpa2.1

ALTER Function [dbo].[PegaIcms]
(     
     @FunNecessidade        numeric(2,0),
     @FunOrigem             char(2),
     @FunDestino            char(2),
     @FunTabNat             numeric (3,0),
     @FunTipoFor            char(1),
     @FunTipoEmp            char(1),
     @FunNatFiscal          char(3),
     @FunConsFinal          numeric (1,0),
     @FunAtacado            numeric (1,0),
     @FunPj                 numeric (1,0)
)

Returns @FunTabela Table
 (FunCompraIcm numeric (5,2),
  FunCompraRed numeric(5,2),
  FunVendaIcm numeric(5,2),
  FunVendaRed numeric (5,2),
  FunIcmStP numeric (5,2),
  FunIcmStRedP numeric (5,2),
  FunIcmStE numeric (5,2),
  FunIcmStRedE numeric (5,2),
  FunMensagem char(76) )
  
as
Begin
declare @FunCompraIcm numeric (5,2)
declare @FunCompraRed numeric(5,2)
declare @FunVendaIcm numeric(5,2)
declare @FunVendaRed numeric (5,2)
declare @FunIcmStP numeric (5,2)
declare @FunIcmStRedP numeric (5,2)
declare @FunIcmStE numeric (5,2)
declare @FunIcmStRedE numeric (5,2)
declare @FunMensagem char(76)

set  @FunCompraIcm =0
set  @FunCompraRed =0
set  @FunVendaIcm =0
set  @FunVendaRed =0
set  @FunIcmStP =0
set  @FunIcmStRedP =0
set  @FunIcmStE =0
set  @FunIcmStRedE =0
set  @FunMensagem = ''

---------------------------------------------------------------
-- Tratando o tabfis
---------------------------------------------------------------
declare @tbfcodfis numeric (4,0)
declare @tbforigem char(2)
declare @tbfdestino char(2)
declare @tbficm numeric (5,2)

set @tbfcodfis =
 case
    when @FunNecessidade = 1 or @Funnecessidade = 2 then
         case
            when @FunOrigem = @FunDestino then
                 5102
            else
                 6102
         end
    when @FunNecessidade = 3 then
         case
            when @FunOrigem = @FunDestino then
                 5152
            else
                 6152
         end
    when @FunNecessidade = 4 then
         case
            when @FunOrigem = @FunDestino then
                 5202
            else
                 6202
         end
    when @FunNecessidade = 5 then
         case
            when @FunOrigem = @FunDestino then
                 1152
            else
                 5152
         end
 end

set @tbfcodfis =
 case
    when @FunConsFinal <> 1 then
       @tbfcodfis
    else
       5102
 end
 
set @tbforigem = @FunOrigem
set @tbfdestino = @FunDestino

set @tbforigem =
    case
       when @FunConsFinal <> 1 then
            @tbfdestino
       else
            @tbforigem
    end

select @tbficm = tbficm from TABFIS
 where TBFORIGEM = @tbforigem and
       TBFDESTINO = @tbfdestino and
       TBFCODIGO = @tbfcodfis

set @FunVendaIcm = @tbficm

if substring(@funnatfiscal,1,1) = 'F'
   begin
     set @FunIcmStP = @tbficm
   end

if @FunConsFinal = 1
   begin
       declare @naticmcfinal decimal(5,2)
       select @naticmcfinal = naticmcfinal from CADNAT
        where NATTABNAT = @FunTabNat and
              NATESTADO = @FunOrigem and
              NATCODIGO = @FunNatFiscal
       set @FunCompraIcm = @naticmcfinal
       set @FunVendaIcm = @naticmcfinal
       set @FunCompraRed = 0
       set @FunVendaRed = 0
       if (SUBSTRING(@FunNatFiscal,1,1) = 'F')
          begin
             set @FunCompraIcm = 0
             set @FunVendaIcm = 0
             set @FunCompraRed = 0
             set @FunVendaRed = 0
             set @FunIcmStP = 0
             set @FunIcmStE = 0
             set @FunIcmStRedE = 0
             set @FunIcmStRedP = 0
          end;
   end;

if @FunDestino = 'MG' and
   @FunNatFiscal = 'TD1'
   begin
      set @FunCompraIcm = 5
      set @FunCompraRed = 0
   end;
   
---------------------------------------------------------------
-- Tratando o cadnat
---------------------------------------------------------------

declare @natestado char(2)
declare @naticm decimal(5,2)
declare @naticmreduz decimal(5,2)
declare @naticmcompra decimal (5,2)
declare @naticmredcmp decimal (5,2)
declare @naticmdev decimal (5,2)
declare @naticmreddev decimal (5,2)
declare @naticmredpj decimal (5,2)
declare @natmensagem char(76)

if @FunOrigem = @FunDestino
   begin
      select @naticm = naticm,
             @naticmreduz = naticmreduz,
             @naticmcompra = naticmcompra,
             @naticmredcmp = naticmredcmp,
             @naticmdev = naticmdev,
             @naticmreddev = naticmreddev,
             @naticmredpj = natredpj,
             @natmensagem = natmensagem
             from CADNAT
            where NATTABNAT = @FunTabNat and
                  NATESTADO = @FunOrigem and
                  NATCODIGO = @FunNatFiscal
      set @FunVendaIcm = @naticm
      set @FunVendaRed = @naticmreduz
      set @FunCompraIcm = @naticmcompra
      set @FunCompraRed = @naticmredcmp
      if SUBSTRING(@funnatfiscal,1,1) = 'F'
         begin
            set @FunIcmStP = @naticm
            set @FunIcmStRedP = @naticmreduz
            set @FunIcmStE = @naticmcompra
            set @FunIcmStRedE = @naticmredcmp
         end
      if @FunNecessidade = 2 and
         @FunAtacado = 1 and
         @FunPj = 1 and
         @naticmredpj <> 0
         begin
            set @FunVendaRed = @naticmredpj
         end;
      
      set @FunMensagem = @natmensagem
   end
else
   begin
      if @FunNecessidade = 1 or
         @FunNecessidade = 4
         begin
            set @natestado = @FunDestino
         end
      else
         begin
            set @natestado = @FunOrigem
         end
      select @naticm = naticm,
             @naticmreduz = naticmreduz,
             @naticmcompra = naticmcompra,
             @naticmredcmp = naticmredcmp,
             @naticmdev = naticmdev,
             @naticmreddev = naticmreddev,
             @naticmredpj = natredpj,
             @natmensagem = natmensagem
             from CADNAT
            where NATTABNAT = @FunTabNat and
                  NATESTADO = @natestado and
                  NATCODIGO = @FunNatFiscal
      if SUBSTRING (@funnatfiscal,1,1) = 'I' or
        (SUBSTRING (@funnatfiscal,1,1) = 'F' and
         @FunNecessidade <> 1)
         begin
            set @FunCompraIcm = 0
            set @FunCompraRed = 0
            set @FunVendaIcm = 0
            set @FunVendaRed = 0
         end
      else
         begin
            if @FunNecessidade = 1
               begin
                  set @FunVendaIcm = @naticm
                  set @FunVendaRed = @naticmreduz
               end
         end
       -- se o produto tiver uma natureza fiscal cadastrada
       -- na origem ele deve obedescer a aliquota no caso de compra
       if @FunNecessidade = 1 and
          SUBSTRING(@FunNatFiscal,1,1) <> 'F' and
          SUBSTRING(@FunNatFiscal,1,1) <> 'I'
          begin
             select @naticm = naticm,
                    @naticmreduz = naticmreduz,
                    @naticmcompra = naticmcompra,
                    @naticmredcmp = naticmredcmp,
                    @naticmdev = naticmdev,
                    @naticmreddev = naticmreddev,
                    @naticmredpj = natredpj,
                    @natmensagem = natmensagem
                from CADNAT
               where NATTABNAT = @FunTabNat and
                     NATESTADO = @FunOrigem and
                     NATCODIGO = @FunNatFiscal
             if @naticm <> null
                begin
                   set @FunCompraIcm = @naticmcompra
                   set @FunCompraRed = @naticmredcmp
                end
          end
                
       if @FunNecessidade = 3 and
          SUBSTRING(@FunNatFiscal,1,1) <> 'F' and
          SUBSTRING(@FunNatFiscal,1,1) <> 'I'
          begin
             select @naticm = naticm,
                    @naticmreduz = naticmreduz,
                    @naticmcompra = naticmcompra,
                    @naticmredcmp = naticmredcmp,
                    @naticmdev = naticmdev,
                    @naticmreddev = naticmreddev,
                    @naticmredpj = natredpj,
                    @natmensagem = natmensagem
                from CADNAT
               where NATTABNAT = @FunTabNat and
                     NATESTADO = @FunOrigem and
                     NATCODIGO = @FunNatFiscal
             if @naticm <> null
                begin
                   set @FunCompraIcm = @naticmcompra
                   set @FunCompraRed = @naticmredcmp
                end
          end
                
       if @FunNecessidade = 4 and
          SUBSTRING(@FunNatFiscal,1,1) <> 'F' and
          SUBSTRING(@FunNatFiscal,1,1) <> 'I'
          begin
             select @naticm = naticm,
                    @naticmreduz = naticmreduz,
                    @naticmcompra = naticmcompra,
                    @naticmredcmp = naticmredcmp,
                    @naticmdev = naticmdev,
                    @naticmreddev = naticmreddev,
                    @naticmredpj = natredpj,
                    @natmensagem = natmensagem
                from CADNAT
               where NATTABNAT = @FunTabNat and
                     NATESTADO = @FunDestino and
                     NATCODIGO = @FunNatFiscal
             if @naticm <> null
                begin
                   set @FunVendaIcm = @naticmdev
                   set @FunVendaRed = @naticmreddev
                end
          end
                
       if @FunNecessidade = 5 and
          SUBSTRING(@FunNatFiscal,1,1) <> 'F' and
          SUBSTRING(@FunNatFiscal,1,1) <> 'I'
          begin
             select @naticm = naticm,
                    @naticmreduz = naticmreduz,
                    @naticmcompra = naticmcompra,
                    @naticmredcmp = naticmredcmp,
                    @naticmdev = naticmdev,
                    @naticmreddev = naticmreddev,
                    @naticmredpj = natredpj,
                    @natmensagem = natmensagem
                from CADNAT
               where NATTABNAT = @FunTabNat and
                     NATESTADO = @FunOrigem and
                     NATCODIGO = @FunNatFiscal
             if @naticm <> null
                begin
                   set @FunCompraIcm = @naticm
                   set @FunCompraRed = @naticmredcmp
                end
          end
          
          set @FunMensagem = @natmensagem
                
          if SUBSTRING(@FunNatFiscal,1,1) = 'F'
             begin
                if @FunNecessidade = 1 or
                   @FunNecessidade = 4
                   begin
                      set @natestado = @FunOrigem
                   end
                else
                   begin
                      set @natestado = @FunDestino
                   end
                select @naticm = naticm,
                       @naticmreduz = naticmreduz,
                       @naticmcompra = naticmcompra,
                       @naticmredcmp = naticmredcmp,
                       @naticmdev = naticmdev,
                       @naticmreddev = naticmreddev,
                       @naticmredpj = natredpj,
                       @natmensagem = natmensagem
                   from CADNAT
                  where NATTABNAT = @FunTabNat and
                        NATESTADO = @natestado and
                        NATCODIGO = @FunNatFiscal
                if @naticm <> null
                   begin
                      set @FunIcmStE = @naticm
                      set @FunIcmStRedE = @naticmredcmp
                   end
             end
   end
 
insert into @FunTabela
   (FunCompraIcm,FunCompraRed,FunVendaIcm,
  FunVendaRed,FunIcmStP,FunIcmStRedP,
  FunIcmStE,FunIcmStRedE,FunMensagem)
  values (@FunCompraIcm,@FunCompraRed,@FunVendaIcm,
  @FunVendaRed,@FunIcmStP,@FunIcmStRedP,
  @FunIcmStE,@FunIcmStRedE,@FunMensagem)
  return   

End
Criado 11 de agosto de 2014
Ultima resposta 12 de ago. de 2014
Respostas 2
Participantes 2