kaiscript
kaiscript

Reputation: 21

SpringMVC+Hibernate :criteria.list() return empty

Spring:version 3.2.9 Hibernate:version 4.2.19

BaseDaoImpl.java

public class BaseDaoImpl<T> implements BaseDao<T> {

    public SessionFactory sessionFactory;
    protected Class<T> entityClass;

    @Override
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public BaseDaoImpl() {
        Class c = getClass();
        Type type = c.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
            this.entityClass = (Class<T>) parameterizedType[0];
        }
    }

    @Resource(name="sessionFactory")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    @Override
    public boolean save(T entity) {
        try {
            getSession().save(entity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean delete(T entity) {
        try {
            getSession().delete(entity);
            return true;
        } catch (Exception e) {

            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean update(T entity) {

        try {
            getSession().update(entity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    public T get(Integer id) {
        return (T) getSession().get(entityClass, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<T> findByIdSet(Integer[] ids) {
        Criteria criteria=getSession().createCriteria(entityClass);
        criteria.add(Restrictions.in("id", ids));       
        return (List<T>)criteria.list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<T> findAllList() {

        return getSession().createCriteria(entityClass).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public Pager findByPager(Pager pager) {
        Criteria criteria=getSession().createCriteria(entityClass);


        criteria.setProjection(Projections.rowCount());
        int totalCount = ((Long) criteria.uniqueResult()).intValue();
        pager.setTotalCount(totalCount);

        criteria.setProjection(null);

        pager.init();


        criteria.setFirstResult((pager.getPageIndex()-1)*pager.getPageSize()); 
        criteria.setMaxResults(pager.getPageSize());    
        List<T> te =(List<T>)criteria.list();
        pager.setDatas((List<T>)criteria.list());
        return pager;
    }


}

Pager.java

import java.util.List;
    public class Pager {


        private int pageSize;

        private int pageIndex;

        private int totalCount;

        private int totalPage;

        private List<?> datas;

        private boolean hasNextPage;

        private boolean hasPreviousPage;

        public void init(){
            pageIndex=1;

        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getPageIndex() {
            return pageIndex;
        }

        public void setPageIndex(int pageIndex) {
            this.pageIndex = pageIndex;
        }

        public int getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }

        public int getTotalPage() {
            return totalPage;
        }

        public void setTotalPage(int totalPage) {
            this.totalPage = totalPage;
        }

        public List<?> getDatas() {
            return datas;
        }

        public void setDatas(List<?> datas) {
            this.datas = datas;
        }

        public boolean isHasNextPage() {
            return hasNextPage;
        }

        public void setHasNextPage(boolean hasNextPage) {
            this.hasNextPage = hasNextPage;
        }

        public boolean isHasPreviousPage() {
            return hasPreviousPage;
        }

        public void setHasPreviousPage(boolean hasPreviousPage) {
            this.hasPreviousPage = hasPreviousPage;
        }


    }

Entity:Student.java

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


@Entity
@Table(name="student")
public class Student {

    private int stu_id;
    private String stu_name;
    private String stu_password;
    private Department department;
    private Major major;
    private String stu_sex;
    private String stu_per_sig;
    private String stu_head_img;
    private Date stu_regist_time;
    private String stu_attn_crs_ids;
    private String stu_pw_question;
    private String stu_pw_answer;


    public Student() {

    }

    public Student(int stu_id, String stu_name, String stu_password,
            Department department, Major major, String stu_sex,
            String stu_per_sig, String stu_head_img, Date stu_regist_time,
            String stu_attn_crs_ids, String stu_pw_question,
            String stu_pw_answer) {
        this.stu_id = stu_id;
        this.stu_name = stu_name;
        this.stu_password = stu_password;
        this.department = department;
        this.major = major;
        this.stu_sex = stu_sex;
        this.stu_per_sig = stu_per_sig;
        this.stu_head_img = stu_head_img;
        this.stu_regist_time = stu_regist_time;
        this.stu_attn_crs_ids = stu_attn_crs_ids;
        this.stu_pw_question = stu_pw_question;
        this.stu_pw_answer = stu_pw_answer;
    }


    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getStu_id() {
        return stu_id;
    }
    public void setStu_id(int stu_id) {
        this.stu_id = stu_id;
    }

    @Column(length=20,nullable=false)
    public String getStu_name() {
        return stu_name;
    }
    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    @Column(length=20,nullable=false)
    public String getStu_password() {
        return stu_password;
    }
    public void setStu_password(String stu_password) {
        this.stu_password = stu_password;
    }


    @Column(length=2,nullable=false)
    public String getStu_sex() {
        return stu_sex;
    }
    public void setStu_sex(String stu_sex) {
        this.stu_sex = stu_sex;
    }

    @Column(length=255,nullable=true)
    public String getStu_per_sig() {
        return stu_per_sig;
    }
    public void setStu_per_sig(String stu_per_sig) {
        this.stu_per_sig = stu_per_sig;
    }

    @Column(length=255,nullable=false)
    public String getStu_head_img() {
        return stu_head_img;
    }
    public void setStu_head_img(String stu_head_img) {
        this.stu_head_img = stu_head_img;
    }

    @Temporal(value=TemporalType.TIMESTAMP)
    @Column(nullable=false)
    public Date getStu_regist_time() {
        return stu_regist_time;
    }
    public void setStu_regist_time(Date stu_regist_time) {
        this.stu_regist_time = stu_regist_time;
    }

    @Column(length=255,nullable=true)
    public String getStu_attn_crs_ids() {
        return stu_attn_crs_ids;
    }
    public void setStu_attn_crs_ids(String stu_attn_crs_ids) {
        this.stu_attn_crs_ids = stu_attn_crs_ids;
    }

    @Column(length=64,nullable=false)
    public String getStu_pw_question() {
        return stu_pw_question;
    }
    public void setStu_pw_question(String stu_pw_question) {
        this.stu_pw_question = stu_pw_question;
    }

    @Column(length=64,nullable=false)
    public String getStu_pw_answer() {
        return stu_pw_answer;
    }
    public void setStu_pw_answer(String stu_pw_answer) {
        this.stu_pw_answer = stu_pw_answer;
    }


    @ManyToOne()
    @JoinColumn(name="stu_dept_id",nullable=false)
    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    @ManyToOne()
    @JoinColumn(name="stu_major_id",nullable=false)
    public Major getMajor() {
        return major;
    }

    public void setMajor(Major major) {
        this.major = major;
    }

}

I write BaseDaoImpl.java to implement the interface BaseDao.java

When I debug the project ,I found that the size of te list is 0 .

In findByPager method of BaseDaoImpl.java. Why the method criteria.list() return a empty list? Why is it not a List<Student> list ?
It puzzles me a long time.I'm a fresh man,who can help me to solve this problem.

Upvotes: 2

Views: 1264

Answers (1)

hmrojas.p
hmrojas.p

Reputation: 582

The problem of your code is this line:

Criteria criteria=getSession().createCriteria(entityClass);

Because when you use criteria queries, you are building a query against a reference of a particular persistent class and your variable entityClass isn't.

protected Class<T> entityClass;

The correct way to build a criteria query is:

Criteria criteria=getSession().createCriteria(Entity.class);

Then you need to change that line of your code. You need to add the .class to your variable:

   Criteria criteria=getSession().createCriteria(entityClass.class);

I share you some links about criteria queries.

https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Criteria.html

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html

I hope this information helps you.

Good luck.

Upvotes: 1

Related Questions