Showing posts with label query. Show all posts
Showing posts with label query. Show all posts

Tuesday, November 21, 2017

Disable hibernate query cache

Do you have a problem, that hibernate returns by calling stored procedure with the same parameters the same result also if you put id column is native query?

For example you have stored procedure which delivers last modified date of recent change for some object:
CREATE PROCEDURE GET_LAST_MODIFIED_DATE
(
    @objectId [bigint]
)
AS
BEGIN
    SET NOCOUNT ON

    SELECT
        a.id,
        top(1) a.lastModified
    FROM
        Table a
    WHERE
        a.deleted = 0
        AND a.objectId = @objectId
    ORDER BY
        a.lastModified DESC
END
GO
Solution is simple. You can change cache mode for single query:
    String sql = "exec dbo.GET_LAST_MODIFIED_DATE @objectId=123";
    Query query = entityManager.createNativeQuery(sql);
    query.setHint("javax.persistence.cache.storeMode", "REFRESH");