有两张表,父表是MeetingTypes子表是Meetings子表是Meetings子表,它们有1:m映射
会议有releaseID属性,所以我想按releaseID筛选它。但问题是,它在子表中。。。
如果它是父表,我们可以简单地执行它并且它正在工作
Query query = session.getCurrentSession().createQuery("from MeetingTypes where releaseID= :releaseID");
query.setParameter("releaseID", releaseID);
List list = query.list();
if(list!=null && list.size()>0){
return list;
}else{
return null;
}
我是这样试的
Query query = session.getCurrentSession().createSQLQuery(
"from MeetingTypes mt join mt.Meetings m " +
"where m.releaseID = :releaseID");
query.setParameter("releaseID", releaseID);
return query.list();
但是给贝娄一个错误
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from MeetingTypes mt join mt.Meetings m where m.releaseID = 192' at
line 1
相关标签
package pearson.dashboard.model;
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.OneToMany;
@Entity
public class MeetingTypes {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO)
private int meetingTypeID;
@Column
private String typeName;
@OneToMany(mappedBy = "meetingTypes",fetch = FetchType.EAGER)
private List<Meetings> meetings;
public List<Meetings> getMeetings() {
return meetings;
}
public void setMeetings(List<Meetings> meetings) {
this.meetings = meetings;
}
public MeetingTypes() {
// TODO Auto-generated constructor stub
}
public MeetingTypes(int meetingTypeID, String typeName
) {
super();
this.meetingTypeID = meetingTypeID;
this.typeName = typeName;
}
public int getMeetingTypeID() {
return meetingTypeID;
}
public void setMeetingTypeID(int meetingTypeID) {
this.meetingTypeID = meetingTypeID;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
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;
import org.springframework.context.annotation.Lazy;
@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;
}
}
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;
import javax.persistence.OneToMany;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
@Entity
public class Releases {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO)
private int releaseID;
@Column
private String orcleCode;
@Column
private String status;
@Column
private Date staging;
@Column
private Date cabCall;
@Column
private Date rrr;
@Column
private String remarks;
@Column
private String releaseName;
@Column
private Date prodDate;
@ManyToOne( fetch = FetchType.EAGER)
@JoinColumn(name = "teamID")
private Teams teams;
public Teams getTeams() {
return teams;
}
public void setTeams(Teams teams) {
this.teams = teams;
}
@OneToMany(mappedBy = "releases",fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<Meetings> meetings;
public List<Meetings> getMeetings() {
return meetings;
}
public void setMeetings(List<Meetings> meetings) {
this.meetings = meetings;
}
public Releases() {}
public Releases(int releasID, String orcleCode, String status,
Date staging, Date cabCall, Date rrr, String remarks,
String releaseName,Date prodDate) {
super();
this.releaseID = releasID;
this.orcleCode = orcleCode;
this.status = status;
this.staging = staging;
this.cabCall = cabCall;
this.rrr = rrr;
this.remarks = remarks;
this.releaseName = releaseName;
this.prodDate = prodDate;
}
public int getReleaseID() {
return releaseID;
}
public void setReleaseID(int releaseID) {
this.releaseID = releaseID;
}
public String getOrcleCode() {
return orcleCode;
}
public void setOrcleCode(String orcleCode) {
this.orcleCode = orcleCode;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getStaging() {
return staging;
}
public void setStaging(Date staging) {
this.staging = staging;
}
public Date getCabCall() {
return cabCall;
}
public void setCabCall(Date cabCall) {
this.cabCall = cabCall;
}
public Date getRrr() {
return rrr;
}
public void setRrr(Date rrr) {
this.rrr = rrr;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getReleaseName() {
return releaseName;
}
public void setReleaseName(String releaseName) {
this.releaseName = releaseName;
}
public Date getProdDate() {
return prodDate;
}
public void setProdDate(Date prodDate) {
this.prodDate = prodDate;
}
}
控制器的一部分******
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pearson.dashboard.dto.NewMeeting;
import pearson.dashboard.service.MeetingService;
import pearson.dashboard.service.MeetingTypeService;
import pearson.dashboard.service.MemberService;
import pearson.dashboard.service.SwarmingScheduleService;
@Controller
public class MeetingTypesController {
@Autowired
private MeetingTypeService meetingTypeService;
//@Autowired
//private MeetingService meetingService;
@Autowired
private SwarmingScheduleService swarmingScheduleService;
@Autowired
private MemberService memberService;
@RequestMapping(value="/detailsPage",method=RequestMethod.POST)
public String getAllmeeting(@ModelAttribute NewMeeting newMeeting,BindingResult result,Map<String, Object> map){
int releaseID = newMeeting.getReleaseID();
map.put("meetingList", meetingTypeService.getAllMeetingTypes(releaseID));
map.put("swarmingScheduleList",swarmingScheduleService.gettAllSwarming() );
map.put("memberList",memberService.gettAllMembers() );
return "details";
}
}
最佳答案
您使用的是HQL而不是sql,因此在HQL中没有类似join的关键字,或者您可以使用objecta.objectb
等实现join,因此您必须创建如下查询Query query = session.getCurrentSession().createQuery("FROM MeetingTypes mt WHERE mt.meetings.releases.id = :releaseID");
有关详细信息,请参阅下面的链接
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-joins-forms
关于java - 当键属性在子表中时如何过滤数据+ spring MVC和Hibernate,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20400230/