0

We moved to spring data JPA recently (using Spring-boot 2.1.5-RELEASE) and in need to execute stored procedures with multiple input parameters and map to the non-entity POJO (I will be setting the value to the entity class while saving the objects). Can anyone please provide an example/description on how can it be achieved? I didn't get much out of documentation or may be I missed.

Any help would be appreciated.

404or505
  • 165
  • 2
  • 3
  • 12
  • I'm trying to wrap my head around why you want to walk around with only one shoe on your feet. Do you really mean you want to use a Class that is not annotated with `@Entity`? A POJO marked with the `@Entity` annotation is, well..., just a POJO. If you don't use the annotation you lose all the benefit of Spring Data _JPA_ Repositories. If you don't use `@Entity` you might as well go back to plain _Spring Data_ Repositories. Can you clarify what you mean by "_non-entity POJO_"? – Randy Casburn Jul 06 '19 at 21:04
  • @RandyCasburn I apologize for not being clear on my question. First of all, yes, by non-entity POJO, I mean a plain POJO that is not related to any tables in the database. Second, the reason I want to do this is because I want to execute the stored procedure that gives me objects that I want to save in the plain POJO and then use those objects wherever they are required. My issue is similar to question asked here https://stackoverflow.com/questions/56859799/illegalargumentexception-type-cannot-be-null-while-calling-stored-procedure-sp – 404or505 Jul 06 '19 at 22:39
  • and sql query(https://stackoverflow.com/questions/29082749/spring-data-jpa-map-the-native-query-result-to-non-entity-pojo). Basically, need a placeholder to hold the objects returned from the stored procedure. – 404or505 Jul 06 '19 at 22:40

1 Answers1

1

The most direct, and simplest solution is to user SimpleJDBCCall and forget about JPA for this data resource. The documentation covers parameters extensively.

Something like this will work:

public class MyDao implements SomeDao {

    private JdbcTemplate jdbcTemplate;
    private SimpleJdbcCall procReadStuff;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        this.procReadStuff = new SimpleJdbcCall(dataSource)
                .withProcedureName("my_procedure");
    }

    public void readStuff(Long id) {
        SqlParameterSource in = new MapSqlParameterSource()
                .addValue("in_id", id);
        Map out = procReadStuff.execute(in);
        Stuff stuff = new Stuff();
        stuff.setId(id);
        stuff.setSomeString((String) out.get("out_some_string"));

        // ...do something with Stuff POJO
    }

}

This sample was pulled/modified from the sample in the docs: Documentation

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • Cashburn, thank you for your response. However, after converting to Spring Boot, we are only implementing Spring Data JPA as it was convenient to write the queries and much cleaner. Is there no alternative in Spring data JPA to perform the same task as the SimpleJdbcCall? – 404or505 Jul 07 '19 at 05:09
  • Sure, an alternative you either don't understand or refuse to take advantage of. Spring Data JPA is by far the recommendation - but your POJO that you want to hold your data will need to be an `@Entity` with corresponding `@Repository`. It's just a POJO - I just don't understand your reluctance to use JPA. the way it is designed. Good luck to you. – Randy Casburn Jul 07 '19 at 05:24
  • Thank you for your input. I will certainly do some more research on JPA. – 404or505 Jul 07 '19 at 06:56