我正在使用Spring mvc和hibernate.I想要放置WHERE条件并获得特定结果然后我想转换为LIst,我就这样做了..但是给了错误..
请简单告诉一下是否有人希望将任何一个表转换为列表。我们在MODEL中改变了
这是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;
}
}
道* * 的
@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;
}
}
}
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)
答案 0 :(得分:0)
您可能需要通过强制转换来改变类型,
List<Meetings> list = (List<Meetings>)query.list();
您的返回对象“return(List)list.get(0)”错误,因为您列出了包含Meetings对象的列表。所以,它将返回(会议)list.get(0)。
答案 1 :(得分:0)
根据签名,方法应返回所有会议的列表。所以代码应该返回列表而不是它的第一个元素。
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;
}
答案 2 :(得分:0)
将您的查询从from Meetings where releaseID = :releaseID
更改为from Meetings where releases.id= :releaseID
由于您的查询参数应映射到您的域类属性而不是数据库属性(列名称)。