0

I am using Spring mvc and hibernate.I want to put WHERE condition and get specific result then i want to convert in to LIst ,I did it like that..but give ERRORS..

Please simple tell if some one want to convert any table to list .do we shud change in MODEL

This is MODEL**

package pearson.dashboard.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Meetings {
    @Id
    @Column
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int meetingID;
    @Column
    private Date sheduleTime;
    @Column
    private String meetingHeading;
    @Column 
    private String comment;
    @Column
    private String roomName;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "meetingTypeID") 
    private MeetingTypes meetingTypes;



    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "releaseID") 
    private Releases releases;


    public Releases getReleases() {
        return releases;
    }

    public void setReleases(Releases releases) {
        this.releases = releases;
    }



    public MeetingTypes getMeetingTypes() {
        return meetingTypes;
    }

    public void setMeetingTypes(MeetingTypes meetingTypes) {
        this.meetingTypes = meetingTypes;
    }

    public Meetings() {

        // TODO Auto-generated constructor stub
    }

    public Meetings(int meetingID, Date sheduleTime, String meetingHeading,
            String comment, String roomName) {
        super();
        this.meetingID = meetingID;
        this.sheduleTime = sheduleTime;
        this.meetingHeading = meetingHeading;
        this.comment = comment;
        this.roomName = roomName;

    }

    public int getMeetingID() {
        return meetingID;
    }

    public void setMeetingID(int meetingID) {
        this.meetingID = meetingID;
    }

    public Date getSheduleTime() {
        return sheduleTime;
    }

    public void setSheduleTime(Date sheduleTime) {
        this.sheduleTime = sheduleTime;
    }

    public String getMeetingHeading() {
        return meetingHeading;
    }

    public void setMeetingHeading(String meetingHeading) {
        this.meetingHeading = meetingHeading;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }




}

Dao**

@Transactional
@Repository
public class MeetingTypeDaoImpl implements MeetingTypeDao  {
    @Autowired
    private SessionFactory sessionFactory;


    public List getAllMeetingTypes(int releaseID) {
        // TODO Auto-generated method stub
        Query query = sessionFactory.getCurrentSession().createQuery("from Meetings where releaseID = :releaseID");
        query.setParameter("releaseID", releaseID);
        List list = query.list();       
         if(list!=null && list.size()>0){
            return (List) list.get(0);//when i remove castin it give error that why i casted
        }else{
        return null;
        }



    }

}

Part of Erro***

Dec 04, 2013 2:19:53 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/controller] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: pearson.dashboard.model.Meetings cannot be cast to java.util.List] with root cause
java.lang.ClassCastException: pearson.dashboard.model.Meetings cannot be cast to java.util.List
    at pearson.dashboard.dao.impl.MeetingTypeDaoImpl.getAllMeetingTypes(MeetingTypeDaoImpl.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
Zcon
  • 153
  • 6
  • 18

3 Answers3

0

You may need to change type by casting like,

List<Meetings> list = (List<Meetings>)query.list();

your return object "return (List) list.get(0)" is wrong because you list contains list of Meetings object. So, it will be return (Meetings) list.get(0).

0

Based on the signature, method should return the list of all of the meetings. So the code should return the list instead of its first element.

public List getAllMeetingTypes(int releaseID) {
    // TODO Auto-generated method stub
    Query query = sessionFactory.getCurrentSession().createQuery("from Meetings where releaseID = :releaseID");
    query.setParameter("releaseID", releaseID);
    List list = query.list();       
    return list;
}
Farid A
  • 325
  • 1
  • 2
  • 8
  • no..i did but not working..as i think i cant ritrew data like that – Zcon Dec 04 '13 at 10:21
  • could you explain how it did not work? did not it return what you expected or did it throw an error? – Farid A Dec 04 '13 at 13:20
  • At first I thought you are trying to retrieve the list of the meetings. Reviewing your code again, you want the list of meeting types for all of the meetings related to a releaseID. Is that correct? – Farid A Dec 04 '13 at 13:28
  • you can change the query itself in the code above to 'select Meetings .MeetingTypes from Meetings where releaseID = :releaseID' and leave the rest as is. – Farid A Dec 05 '13 at 12:53
0

Change your query from from Meetings where releaseID = :releaseID to from Meetings where releases.id= :releaseID

As your query parameter should map to your domain classes properties not database properties(column name).

Naresh J
  • 2,087
  • 5
  • 26
  • 39