piątek, 27 grudnia 2019

[DatabaseSpring] First think add two int

I want writing postgres simple function. I think a+b is good idea.
First I write function:
CREATE OR REPLACE FUNCTION public.addition (a INTEGER,b INTEGER)
RETURNS integer AS $$
BEGIN   RETURN a+b ;END;$$ LANGUAGE plpgsql;

Next I think how to sync in java. I create fake entity:
@Data@Entity@Immutable@Subselect(
        "SELECT 0 as ID")
public class FunctionRunner {

    @Id    @Column(name = "ID")
    private Integer id;
}

Always return 0. Simple by useful.

Run in spring jpa:
@Query(nativeQuery = true,value = "SELECT * FROM public.addition(:a,:b);")
Integer runAddition(@Param("a") Integer a,@Param("b") Integer b);


Link to github:
https://github.com/mikoxp/DbExamplesWithSpringBoot/commit/2917c00937b82ce7e7052b7887b14843ce7f083a

Brak komentarzy:

Prześlij komentarz