gpt4 book ai didi

java - session.createQuery() 在 spring 中不返回列表

转载 作者:行者123 更新时间:2023-11-29 07:34:13 25 4
gpt4 key购买 nike

我正在尝试从数据库中获取所有数据,但问题是 session.createQuery("Project")返回 null列表,所以这是我的 ProjectDao所有 session 查询都运行良好的地方。

我不知道我试过的代码有什么问题getCurrentSession() .当我调试它返回的代码时 List<Project>并抛出异常 null列表。

package np.com.drose.studentmanagementsystem.dao.impl;

import java.io.Serializable;
import java.util.List;
import javax.transaction.Transactional;
import np.com.drose.studentmanagementsystem.dao.ProjectDAO;
import np.com.drose.studentmanagementsystem.model.Project;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
*
* @author bibekshakya
*/

@Repository
public class ProjectDAOImpl implements ProjectDAO{

@Autowired
SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}


@Override
@Transactional
public int insertProject(Project project) {
Session session =sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(project);
tx.commit();
Serializable id= session.getIdentifier(project);
session.close();
return (Integer)id;
}

@Transactional
@Override
public List<Project> getProjectList() {
Session session =sessionFactory.openSession();
Transaction tx=session.beginTransaction();
List<Project> result =(List<Project>)session.createQuery("Project").list();
tx.commit();
session.close();
return result;

}
}

这是我的 dispatcher.xml hibernate 配置文件

<bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>np.com.drose.studentmanagementsystem.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.connection.isolation">2</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
</bean>
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

这是我的 Controller 类

@RequestMapping(value = "/list",method = RequestMethod.GET)
public ModelAndView listProject(){
List<Project> projectList=new ArrayList<Project>();
try {

log.info("Inside list of project");
projectList=projectService.getProjectList();

} catch (Exception e) {
log.info("error of getting list of project : "+e);
}
return new ModelAndView("/project/list","projectList",projectList);
}

这是 View

<c:forEach items="${listProject}" var="list" varStatus="status">
<tr>
<td><span style="margin: 20px; font-size: 14px; color: #101010;">${status.count}</span></td>
<td><span style="margin:30px;font-size: 14px;color: #101010;">${list.projectName}</span></td>
<td><span style="margin: 30px;font-size: 14px;color: #101010;">${list.projectResource}</span></td>
<td><span style="margin: 30px; text-align: center; font-size:12px;color: #101010;">${list.teamName}</span></td>
<td>
<span id="progressBar"></span>
<script>
$(function () {
$("#progressBar").progressbar({
value: 32
});
});
</script>

</td>
<td>
<a href="/StudentManagementSystem/project/edit/${list.projectId}" class="btn btn-info"><i class="glyphicon glyphicon-edit"></i> Edit</a></td><td>
<a href="/StudentManagementSystem/project/delete/${list.projectId})" class="btn btn-danger"><i class="glyphicon glyphicon-remove"></i> Project Finish</a>
</td>
</tr>
</c:forEach>

最佳答案

试试看它是否有效:

List<Project> result = session.createCriteria(Project.class).list();

您无需转换列表:(List<Project>)

关于java - session.createQuery() 在 spring 中不返回列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37967931/

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