Skip to content
Advertisement

Can’t create method to get data from database hibernate mySQL + Spring mvc

my page

I really hope for your help! The situation is this: I’m trying to get the value of my disciplines from the database, these disciplines are related to the semester. In the display page, the user must select the semester he needs, after which he clicks on the selection button and all disciplines associated with this semester should be displayed. But I always get the same error “The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.” I am attaching all the code that I wrote to perform this action.

<tr>
    <td style="font-size: large;">Select semester</td>
    <td style="padding-left: 50px">
        <select>
            <c:forEach items="${semestr}" var="semestr">
                <c:url var="chooseButton" value="/chooseSemester">
                    <c:param name="semId" value="${semestr.id}"/>
                </c:url>
                <option value="${semestr.id}">${semestr.name}</option>
            </c:forEach>
        </select>
    </td>
    
    <td style="padding-left: 20px">
        <input type="submit" value="Select" id="button" onclick="window.location.href = 'chooseButton'">
    </td>
</tr>
@RequestMapping("chooseSemester")
public String chooseSemester(@RequestParam("semId")int semId,Model model){
    List<Discipline> allDisciplines = service.getDisciplineSemestrId(semId);
    model.addAttribute("allDisc", allDisciplines);
    return "semestr";
}
package com.kushnirmark.spring.project.DAO;

import com.kushnirmark.spring.project.entity.Discipline;
import com.kushnirmark.spring.project.entity.Semestr;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SemestrImpl implements SemestrDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<Semestr> getSemestr() {
        Session session = sessionFactory.getCurrentSession();
        List<Semestr> semestrs = session.createQuery("from Semestr", Semestr.class).getResultList();
        return semestrs;
    }

    @Override
    public List<Discipline> getDisciplineSemestr() {
        Session session = sessionFactory.getCurrentSession();
        List<Discipline> discSem = session.createQuery("select a.discipline from Discipline a" +
                " inner join Semestr_Disc b on a.id=b.id_discipline where b.id_semestr = 1 ").getResultList();
        return discSem;
    }

    @Override
    public List<Discipline> getDisciplineSemestrId(int semId) {
        Session session = sessionFactory.getCurrentSession();
        Query<Discipline>query = session.createQuery("select a.discipline from" +
                " Discipline a inner join Semestr_Disc b on a.id=b.id_discipline where b.id_semestr =:semId");
        query.setParameter("semId",semId);
        query.executeUpdate();
        return (List<Discipline>) query;
    }


    @Override
    public void saveNewSemester(Semestr semestr) {
        Session session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(semestr);
    }
}

Advertisement

Answer

The implementation of getDisciplineSemestrId in SemestrImpl looks fishy. You create select query, but then execute update on it, and return the query itself cast to List.

Try this:

    @Override
    public List<Discipline> getDisciplineSemestrId(int semId) {
        Session session = sessionFactory.getCurrentSession();
        Query<Discipline>query = session.createQuery("select a.discipline from" +
                " Discipline a inner join Semestr_Disc b on a.id=b.id_discipline where b.id_semestr =:semId");
        query.setParameter("semId",semId);
        return query.getResultList();
    }

Query getResultList() executes a select query and returns the result, while executeUpdate() executes update or delete statements.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement