简体   繁体   English

hibernate 5.4.12.Final“未定义注释类型NamedQuery的可缓存属性”,是否不支持cacheable = true?

[英]hibernate 5.4.12.Final "The attribute cacheable is undefined for the annotation type NamedQuery", is cacheable = true not supported?

I am trying to cache the findByName source query so i only look it up once during the session.我正在尝试缓存 findByName 源查询,因此我在会话期间只查找一次。 However when adding cacheable = true to the annotation, Eclipse reports that it does not support the cacheable setting in the annotation.但是,在注释中添加 cacheable = true 时,Eclipse 报告它不支持注释中的可缓存设置。 I am using Hibernate 5.4.12.Final我正在使用 Hibernate 5.4.12.Final

package aware.process.models;

import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import javax.persistence.Table;

import org.hibernate.Session;

import aware.process.HibernateUtil;
import lombok.Getter;
import lombok.Setter;

@Entity  
@Table(name= "source")  
@Cacheable  
@NamedQuery(query = "Select s from Source s where s.name = :name", name = "findSourceByName", cacheable = true)
@Getter @Setter
public class Source {

    @Id
    private String id;
    private String name;

    public static Source getByName(String parseType) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Query query = session.createNamedQuery("findSourceByName");

        query.setParameter("name", parseType);
        List<Source> sourceList = query.getResultList();

        Source source = (Source) sourceList.get(0);
        session.getTransaction().commit();
        return source;
    }
}

@NamedQuery annotation doesn't have cacheable attribute indeed. @NamedQuery注释确实没有cacheable属性。 Look at the javadoc .查看javadoc So Eclipse shows you this error correctly.因此 Eclipse 会正确地向您显示此错误。

In order to cache the query, you have 2 options:为了缓存查询,您有 2 个选项:

  1. Make hint for all queries created from this named query.为从此命名查询创建的所有查询提供提示。
@NamedQuery(
    query = "Select s from Source s where s.name = :name", 
    name = "findSourceByName", 
    hints={@QueryHint(name="org.hibernate.cacheable",value="true")})           
})
  1. Add hint only to specific queries when you create them:创建时仅向特定查询添加提示:
Query query = session.createNamedQuery("findSourceByName");
query.setHint("org.hibernate.cacheable", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM