我试图从数据库中获取所有数据,但问题是session.createQuery("Project")
返回null
列表,所以这里是我的ProjectDao
所有会话查询都在执行好。
我不知道我尝试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;
}
}
这是我的hibernate
的dispatcher.xml文件配置<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);
}
这是视图
<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>
答案 0 :(得分:3)
试试这个有效:
List<Project> result = session.createCriteria(Project.class).list();
你不需要施放名单:(List<Project>)
答案 1 :(得分:1)
尝试
session.createQuery("from Project").list();
您错过了HQL中用于解析查询的from
子句。
答案 2 :(得分:1)
您可以使用session.createCriteria(Project.class).list();
这将返回所有可用的行。