Issue
I have a procedure that given some input will produce a set of output parameters. I am able to call the procedure and get the results (though as a hashmap), but despite my best efforts unable to produce an entity or a POJO from the repository.
To elaborate on what I am trying to achieve:
Given entity:
@NamedStoredProcedureQuery(
name = "someProcedure",
procedureName = "SOME_PROCEDURE",
parameters = [
StoredProcedureParameter(mode = ParameterMode.IN, name = "IN_ONE", type = String::class),
StoredProcedureParameter(mode = ParameterMode.IN, name = "IN_TWO", type = String::class),
StoredProcedureParameter(mode = ParameterMode.IN, name = "IN_THREE", type = String::class),
StoredProcedureParameter(mode = ParameterMode.OUT, name = "OUT_ONE", type = String::class),
StoredProcedureParameter(mode = ParameterMode.OUT, name = "OUT_TWO", type = String::class),
StoredProcedureParameter(mode = ParameterMode.OUT, name = "OUT_THREE", type = String::class)],
resultClasses = [ResultClass::class])
@Entity
data class ResultClass(
@Id
@GeneratedValue
val id: Long
@Column(name = "OUT_ONE")
val outOne: String,
@Column(name = "OUT_TWO")
val outTwo: String,
@Column(name = "OUT_THREE")
val outThree: String
)
With repo accordingly:
@Repository
interface ResultClassRepo : CrudRepository<ResultClass, Long> {
@Procedure(name = "someProcedure")
@Transactional
fun someProcedure(
@Param("IN_ONE")
inOne: String,
@Param("IN_TWO")
inTwo: String,
@Param("IN_THREE")
inThree: String): ResultClass
}
Even with the resultclass supplied, the result is still a hashmap, so I get a cast exception. I have tried approaches where I define a mapping with @SqlResultSetMapping, but the result is the same.
It is as if out parameters cannot be mapped without "manual" intervention, so my question is if this is at all doable or if the @NamedStoredProcedureQuery
only supports hashmap as a result in this case?
Disclaimers: There is no associated table I can select from at the end of the procedure and to associate the entity with, as I have seen that as a possible solution to similar questions.
I would like to avoid a custom implementation for the repository, so I would not consider that a valid solution in this case.
Solution
For anyone viewing this in the future. I did not find a solution that uses the exact approach described, but using @Query
works just fine:
@Repository
interface ResultClassRepo : CrudRepository<ResultClass, Long> {
@Query(value = "exec [procedure] @IN_ONE = :IN_ONE, @IN_TWO = :IN_TWO, @IN_THREE = :IN_THREE", nativeQuery = true)
fun someProcedure(
@Param("IN_ONE")
inOne: String,
@Param("IN_TWO")
inTwo: String,
@Param("IN_THREE")
inThree: String): ResultClass
}
Where ResultClass
is built like a normal entity (just without a table).
Answered By - drue
Answer Checked By - Timothy Miller (JavaFixing Admin)