0

I am trying to call java sp , which is having select query like

Select a.name, b.age from tablea a, tableb b where a.emp=b.emp;

I am not sure how I can create Entity class for this and how to call.

I have done similar kind of coding where in java sp is taking one input and select query involve only one table.

please find demo code for same

Entity class

    @NamedStoredProcedureQuery
    (name="test",
    procedureName="GETALLOP",
    resultClasses=AfltModel.class,
    parameters={
@StoredProcedureParameter(mode = ParameterMode.IN, name = "someinput", type = Integer.class),
            @StoredProcedureParameter(mode = ParameterMode.OUT, name = "OUT_RETURN_CODE", type = Integer.class),
            @StoredProcedureParameter(mode = ParameterMode.OUT, name = "OUT_RETURN_MESSAGE", type = String.class),
            @StoredProcedureParameter(mode = ParameterMode.OUT, name = "OUT_MESSAGE_SOURCE", type = String.class)
    })


    @Entity
    @Data
    public class AfltModel {

        @Id
        @Column(name="FUTR.CORP_CUST_ID")
        int corpcustid;
        @Column(name="FUTR.AFLT_CUST_ID")
        String afltcustid;
    }

execution class

@Component
public class ServiceClass {

    @Autowired
    EntityManager entityManager;

    public List<List<AfltAcctModel>>  getdata(List<Integer> inputList) {

        List<List<AfltAcctModel>> list = inputList.stream()
                .map(id -> entityManager
                .createNamedStoredProcedureQuery("test").setParameter("IN_ACCT_ID", id)
                .getResultList())
                .collect(Collectors.toList());

        return list;

    }
}

Please let me know how we can achieve same if we have to call java sp using entitymanger and sp is not taking any input and having multiple table as I mention above.

R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
rocky
  • 737
  • 4
  • 11
  • 19
  • 2
    Go have a look here. https://www.thoughts-on-java.org/call-stored-procedures-jpa/. They explain how it works and how to call a stored procedure. – fluffy Jun 07 '17 at 06:45
  • There is one example here as well https://stackoverflow.com/questions/3572626/calling-stored-procedure-from-java-jpa and https://examples.javacodegeeks.com/enterprise-java/jpa/jpa-sql-stored-procedure-example/ – srp321 Jun 07 '17 at 07:48
  • Calling part I understood but here main problem is "How can i create entity class if java sp select query involving multiple tables as in my question i have mentioned" – rocky Jun 07 '17 at 10:00
  • The question is very confusing. Your query lists two columns `name` and `age`, your procedure annotations lists a procedure called `GETALLOP`, the entity uses different attributes, whereas your actual call seems to have nothing to do with the entity. Can you show a consistent set of examples? I understand the question is probably outdated... could be deleted? – Lukas Eder Sep 07 '22 at 14:29

0 Answers0