0

There are some related questions like:

However, it get the error: No query defined for that name [getTestFunction] and I son't know what to try anymore.

My Oracle Function

FUNCTION mySchema.MyPackage.getTestFunction(pParam1 VARCHAR2, pParam2 DATE) RETURN SYS_REFCURSOR AS
    result SYS_REFCURSOR; 
BEGIN
    OPEN result FOR
        SELECT 123 AS "ID", 'abc' AS "VALUE" FROM DUAL 
        UNION ALL
        SELECT 456 AS "ID", 'def' AS "VALUE" FROM DUAL 
        UNION ALL
        SELECT 789 AS "ID", 'ghi' AS "VALUE" FROM DUAL; 
    RETURN result; 
END getTestFunction;

My POJO

public class ResultPOJO {
    private String ID;
    private String VALUE;
}

My Repository

@Transactional
@Repository
@SqlResultSetMapping(
    name = "testmapping",
    classes = {
            @ConstructorResult(
                    targetClass = ResultPOJO.class,
                    columns = {
                            @ColumnResult(name = "ID"),
                            @ColumnResult(name = "VALUE")
                    }
            )
    }
)
@NamedNativeQuery(
    name = "getTestFunction", 
    callable = true, 
    query = "{? = call mySchema.MyPackage.getTestFunction(?,?)}",
    resultSetMapping = "testmapping"
    )
public class TestDao {
    public List<ResultPOJO> getValues(String pParam1, LocalDate pParam2) {
        TypedQuery<ResultPOJO> q = entityManager.createNamedQuery("getTestFunction",ResultPOJO.class);
        q.setParameter(0, pParam1);
        q.setParameter(1, pParam2);     
        return q.getResultList();       
    }   
}

What am I missing?

Alien
  • 15,141
  • 6
  • 37
  • 57
Thanthla
  • 526
  • 1
  • 8
  • 29

1 Answers1

0

JPA annotations are only valid on entities and I also think on package-info.java files, but some annotations can only be applied on classes, so you might need a kind of "dummy entity". You could define something like this:

@Entity
@Subselect("select 1 as id from dual")
public class Dual {
  @Id
  Integer id;
}

You can then define the @NamedNativeQuery on that class. Note though that you will need to enable the entity scanning in your Spring project though for the package that you will put this class into.

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58