user9140475
user9140475

Reputation: 1

Invoking a Stored procedure in spring boot application and assign the result to RowMapper

I have a stored procedure which returns a List. I need to invoke it and assign the result to a model call.

Below is the Repository class i have written

@Repository
public class addRepository {

@Procedure(name = "up_Getdata(?)"
public List<String> fetchResult(@Param("inParam") String inParam){

}

My model class has 3 fields name,age,departName

Any help on how to map this will he helpful

Upvotes: 0

Views: 1803

Answers (2)

alex87
alex87

Reputation: 549

Another approach without @Procedure but works perfectly also in spring boot:

    @Repository
    public class YourRepository {

    @PersistenceContext
    private EntityManager entityManager;

    public List<YourModelClass> executeStoredProcedure(String inParam) {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery(
                "up_Getdata", YourMapper.MAPPING_NAME);

        query.registerStoredProcedureParameter("@inParam", String.class, ParameterMode.IN);
        query.setParameter("@inParam", inParam);

        return query.getResultList();
    }
}

Your Mapper:

@MappedSuperclass
@SqlResultSetMapping(name = YourMapper.MAPPING_NAME, classes = {
        @ConstructorResult(targetClass = YourModelClass.class, columns = { @ColumnResult(name = "name"),
                @ColumnResult(name = "age"), @ColumnResult(name = "departName") }) })
public abstract class YourMapper {
    public static final String MAPPING_NAME = "MappingName";
}

Upvotes: 0

Lino
Lino

Reputation: 19910

I think the best answer would be to simply follow this tutorial on Baeldung, the tl;dr would be:

  1. Create your entity class with the 3 fields.

  2. Change the return type of fetchResult() to your newly created entity.

  3. Change addRepository from a class to an interface

  4. Be sure that you chose the correct procedure name, you can choose between one of the following ways

    • @Procedure("up_Getdata")
    • @Procedure(procedureName = "up_Getdata")
    • @Procedure(value = "up_Getdata")
    • I am not sure if @Procedure(name = "up_Getdata) would be valid

Upvotes: 1

Related Questions