[英]Fetch results from session.load using Hibernate
I'm trying to fetch resultset by making the query in Hibernate like 我正在尝试通过在Hibernate中进行查询来获取结果集
String id = "10";
Person per = session.load(Person .class, id); // This is wrong, because it accepts only an integer, not a string.
But, I require to fetch results using session.load by passing a string, because second level cache is not triggering when I try to fetch as 但是,我需要通过传递字符串来使用session.load来获取结果,因为当我尝试以以下方式获取内容时,不会触发二级缓存
Criteria cr = session.createCriteria(Person.class);
cr.add(Restrictions.like("userId", id));
List<?> results = cr.list();
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Entity
@Table(name = "person")
public class Person {
@Override
public String toString() {
return "Personalisation [id=" + id + ", userId=" + userId
+ ", courseId=" + courseId + ", courseValue=" + courseValue
+ "]";
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String userId;
private String courseId;
private String courseValue;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getCourseValue() {
return courseValue;
}
public void setCourseValue(String courseValue) {
this.courseValue = courseValue;
}
} }
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" " http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd "> “-// Hibernate / Hibernate Configuration DTD 3.0 // EN”“ http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd ”>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/intu</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Ehcache config -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>
<!-- c3p0 Connection pool config -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.privilegeSpawnedThreads">true</property>
<property name="hibernate.c3p0.contextClassLoaderSource">library</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.intu.Dashboard" />
<mapping class="com.intu.Employee"/>
<mapping class="com.intu.Person"/>
The above code is working, but not by second level cache, each time its hitting DB. 上面的代码每次击中数据库时都在工作,但不能通过二级缓存工作。
What steps do I need to do to get it done? 我需要做什么步骤才能完成? Is there any other way 还有其他办法吗
请在cr.add(Restrictions.like("userId", id))
之后调用cr.setCacheable(true)
cr.add(Restrictions.like("userId", id))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.