我有两个类Task和TaskComponents。 TaskComponents包含由Task_ID映射的任务。我正在尝试选择加入TaskComponents表的任务。我已经尝试了许多不同的SQL语句,但是所有这些语句都返回QuerySyntaxException Path Expected to join。
任务POJO
@Entity
@Table(name = "task")
public class Task implements Serializable {
@Id
@Column(name = "ID")
private int Id;
@Column(name = "Name")
private String name;
@Column(name = "Description")
private String description;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Site_ID")
private Site site;
public int getId() {
return Id;
}
public void setId(int Id) {
this.Id = Id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Site getSite() {
return site;
}
public void setSite(Site site) {
this.site = site;
}
}
TaskComponents POJO
@Entity
@Table(name = "taskcomponents")
public class TaskComponents implements Serializable {
@Id
@Column(name = "ID")
private int Id;
@Column(name = "VersionName")
private String versionName;
@Column(name = "Live", nullable = false)
@Type(type = "org.hibernate.type.NumericBooleanType")
private boolean live;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Task_ID")
private Task task;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Group_ID")
private GroupDB group;
public int getId() {
return Id;
}
public void setId(int Id) {
this.Id = Id;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public Task getTask() {
return task;
}
public void setTask(Task task) {
this.task = task;
}
public GroupDB getGroup() {
return group;
}
public void setGroup(GroupDB group) {
this.group = group;
}
}
我试图查询。
Query query = session.createQuery("SELECT t FROM Task t INNER JOIN TaskComponents tc ON t.Id=tc.task.Id");
Query query = session.createQuery("SELECT t FROM Task t INNER JOIN TaskComponents tc ON t=tc.task");
答案 0 :(得分:1)
你不应该使用explicitely ON来定义连接,Hibernate会从映射推断它,只需将你的查询写成
SELECT tc.task FROM TaskComponents tc INNER JOIN tc.task
这是您的错误消息Path expected for join
后面引用的内容,查询期望从一个实体到另一个实体的属性定义路径