Reputation: 1
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
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
Reputation: 19910
I think the best answer would be to simply follow this tutorial on Baeldung, the tl;dr would be:
Create your entity class with the 3 fields.
Change the return type of fetchResult()
to your newly created entity.
Change addRepository
from a class
to an interface
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")
@Procedure(name = "up_Getdata)
would be validUpvotes: 1