1

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>

3 Answers 3

3

Try this it works:

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You sir, It did Work but can you tell me why my previous didnot work
1

Try

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

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

2 Comments

I have did this to sir. but still same result
Did you check an exception?
1

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

5 Comments

Thank You sir, It did Work but can you tell me why my previous didnot work
can you check the database and see if there are values in the table defined in the Person class
Yes there is data in database and sorry for my previous misunderstood comment. My question is I have done this code pattern in my another object it is working fine but In this Object Why it is not working, Thank you once again
@B'bekShakya Can you please check if the class Project is in package np.com.drose.studentmanagementsystem.model. Also, you stated there is an exception, can you share the stack trace?
@B'bekShakya Sorry, misread the comment earlier. Kindly ignore the above comment. The previous did not work as you should have used session.createHQLQuery("from Project").list()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.