Java Ehcache在Hibernate中仅对一个表工作
我试图对两个表使用二级缓存,但它只对一个表有效。我的ehcache.xml配置如下Java Ehcache在Hibernate中仅对一个表工作,java,mysql,hibernate,caching,ehcache,Java,Mysql,Hibernate,Caching,Ehcache,我试图对两个表使用二级缓存,但它只对一个表有效。我的ehcache.xml配置如下 <?xml version="1.0"?> <ehcache> <defaultCache maxElementsInMemory="100" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="200" /> <cache name="
<?xml version="1.0"?>
<ehcache>
<defaultCache
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="200" />
<cache name="com.cni.Employee"
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="200" />
<cache name="com.cni.Person"
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="200" />
</ehcache>
我的选择查询代码
public JSONObject getCourseDetails(String id) {
System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");
JSONObject obj = new JSONObject();
try (Session session = factory.openSession()) {
String hql = "FROM Person E WHERE E.userId = "+id;
Query query = session.createQuery(hql);
List<?> results = query.list();
Gson gson = new Gson();
String jsonCourseList = gson.toJson(results);
System.out.println("Course JSON Data: " + jsonCourseList);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
公共JSONObject GetCoursedDetails(字符串id){
set属性(“net.sf.ehcache.skipUpdateCheck”、“true”);
JSONObject obj=新的JSONObject();
try(Session Session=factory.openSession()){
String hql=“来自人员E,其中E.userId=“+id;
Query=session.createQuery(hql);
List results=query.List();
Gson Gson=新的Gson();
字符串jsonCourseList=gson.toJson(结果);
System.out.println(“课程JSON数据:+jsonCourseList”);
}捕获(例外e){
e、 printStackTrace();
}
返回obj;
}
JPQL/HQL始终在数据库中执行。然后根据查询结果集中的实体ID从二级缓存中提取实体
在使用
session.load
方法的情况下,通过提供的id直接从二级缓存中获取实体。无需在数据库中执行查询,因为id已为人所知。您对员工使用的查询是什么?@DraganBozanovic Employee emp=session.load(Employee.class,id);这是我用于Employee类的查询
public JSONObject getCourseDetails(String id) {
System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");
JSONObject obj = new JSONObject();
try (Session session = factory.openSession()) {
String hql = "FROM Person E WHERE E.userId = "+id;
Query query = session.createQuery(hql);
List<?> results = query.list();
Gson gson = new Gson();
String jsonCourseList = gson.toJson(results);
System.out.println("Course JSON Data: " + jsonCourseList);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}