简体   繁体   中英

session.createQuery() doesnot return list in spring

I'm trying to get all of the data from database but problem is that session.createQuery("Project") return null list, so here is my ProjectDao where all the session query is performing well.

I don't know what's the problem with my code i have tried getCurrentSession() . When I debug the code it returns List<Project> and throws an exception with null list.

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;

    }
}

here is my dispatcher.xml file configuration for 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"/>

Here is my Controller class

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

Here is the 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>

Try this it works:

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

you dont need to cast the List: (List<Project>)

Try

session.createQuery("from Project").list();

You have missed from clause used in HQL to parse a query.

You can use session.createCriteria(Project.class).list(); This will return all the rows available.

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