简体   繁体   中英

Fetch results from session.load using Hibernate

I'm trying to fetch resultset by making the query in Hibernate like

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

Criteria cr = session.createCriteria(Person.class);
cr.add(Restrictions.like("userId", id));
List<?> results = cr.list();

Person class

@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-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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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