gpt4 book ai didi

java - SpringMVC+Hibernate :criteria. list() 返回空

转载 作者:行者123 更新时间:2023-11-30 10:58:59 25 4
gpt4 key购买 nike

Spring :版本 3.2.9 hibernate :版本 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;
}


}

寻呼机

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;
}


}

实体: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;
}

}

我写 BaseDaoImpl.java实现interface BaseDao.java

当我调试项目时,我发现 te 的大小list是 0 。

findByPager BaseDaoImpl.java的方法| .为什么方法criteria.list()返回一个空的 list ?为什么不是 List<Student> list ?
困惑了我很久,我是新人,谁能帮我解决这个问题。

最佳答案

你的代码的问题是这一行:

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

因为当您使用标准查询时,您是在针对特定持久类的引用构建查询,而您的变量 entityClass 不是。

protected Class<T> entityClass;

构建条件查询的正确方法是:

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

然后您需要更改该行代码。您需要将 .class 添加到您的变量中:

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

我与您分享一些有关条件查询的链接。

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

希望这些信息对您有所帮助。

祝你好运。

关于java - SpringMVC+Hibernate :criteria. list() 返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32056365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com