1

I have created stored procedure to get the data from database and converting the response to Java Object. I am having problem when I am trying to catch the response in custom Object. (NOTE : The example I have given is just for testing. I know there is simple way to perform the operation that I am trying to do it here. But it is just for explanation)

Stored Procedure :

CREATE PROCEDURE [dbo].[getVersionByName] @name nvarchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    -- Insert statements for procedure here
    SELECT * FROM [version] where [name] = @name
END

Version Entity :

@Entity
@NoArgsConstructor
@Data
@NamedStoredProcedureQuery(name = "Version.getVersionListedName"
                , procedureName = "getVersionByName"
                , parameters = {
                    @StoredProcedureParameter(mode = ParameterMode.IN,name = "name", type = String.class)})
public class Version {
    @Id
    private Long id;
    private String name;
}

Repository :

public interface VersionRepositoryExtended extends CrudRepository<Version, Long> {

    @Procedure(procedureName = "getVersionByName")
    public Version getVersionListedName(@Param("name") String name);
}

Question : If you see my stored procedure, there is a 'SELECT * ---' statement. I am trying to capture that output into my Java code as a return value. How can I do that ?

UPDATE

When I try to compile the code I get this below error : org.springframework.data.mapping.PropertyReferenceException: No property getVersionListedName found for type Version!

5
  • Have you tried SELECT id, name instead of SELECT *? Commented Apr 29, 2019 at 23:34
  • I am getting this error : org.springframework.data.mapping.PropertyReferenceException: No property getVersionListedName found for type Version! Commented Apr 30, 2019 at 16:35
  • @Taylor I need to fix this error before I can test it. Commented Apr 30, 2019 at 16:37
  • Forgive me if this is wrong but it seems like a stored procedure for this query is a little overkill? You could make the repository method name findByName and spring-data should generate the correct query for you based on the method name. See these docs for more details. Commented Apr 30, 2019 at 18:17
  • @Taylor I mentioned in question (as a Note) that I have created this SP for test purpose only and I am aware of that solution. Trying out Stored Procedure. Commented Apr 30, 2019 at 19:05

1 Answer 1

2

true for Oracle DB

@Entity
@NoArgsConstructor
@Data
@NamedStoredProcedureQuery(name = "Version.NamedQuery_getVersionListedName"
                , procedureName = "getVersionByName"
                , parameters = {
                    @StoredProcedureParameter(
                        mode = ParameterMode.IN,
                        name = "name", 
                        type = String.class),
                    @StoredProcedureParameter(
                        mode = ParameterMode.REF_CURSOR,
                        name = "OUT", 
                        type = Class.class)
                  })
public class Version {
    public static final String NamedQuery_getVersionListedName = "getVersionListedName";
    @Id
    private Long id;
    private String name;
}
public interface VersionRepositoryExtended extends CrudRepository<Version, Long> {

    @Procedure(name = Version.NamedQuery_getVersionListedName)
    public Version getVersionListedName(@Param("name") String name);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.