1
我有關係多對多下表訪問複合鍵
任務表:含有複合主鍵
[OPTION_ID] pk
,[OPTION_VALUE]
和task_options表:
[TASK_ID] pk
,[TASK_NAME]
選項表來自前表的兩個外鍵
[TASK_ID]
,[OPTION_ID]
和它的映射如下:
Task_Options.java
@Entity
@Table(name = "TASK_OPTIONS")
public class Task_Options implements Serializable {
@EmbeddedId
private pk pk;
@ManyToOne
@MapsId("Task_ID")
private Task task;
@ManyToOne
@MapsId("Option_ID")
private Options options;
public Task getTask() {
return task;
}
public void setTask(Task task) {
this.task = task;
}
public Options getOptions() {
return options;
}
public void setOptions(Options options) {
this.options = options;
}
/*Inner Class for Composite primary key*/
@Embeddable
public static class pk implements Serializable {
private int Task_ID;
private int Option_ID;
public pk(int Task_ID, int Option_ID) {
this.Task_ID = Task_ID;
this.Option_ID = Option_ID;
}
@Override
public int hashCode() {
int hash = 7;
hash = 47 * hash + this.Task_ID;
hash = 47 * hash + this.Option_ID;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final pk other = (pk) obj;
if (this.Task_ID != other.Task_ID) {
return false;
}
if (this.Option_ID != other.Option_ID) {
return false;
}
return true;
}
}
}
我想創建Hibernate的標準來執行以下查詢
select OPTION_VALUE from [dbo].[OPTION] AS op
INNER JOIN [dbo].[TASK_OPTIONS] AS TSOP ON op.OPTION_ID = TSOP.OPTION_ID
INNER JOIN [dbo].[TASK] AS tsk ON tsk.TASK_ID = TSOP.TASK_ID
Where Tsk.TASK_ID = 2
我已經嘗試了本
Criteria criteria = session.createCriteria(Task_Options.class, "Tsop");
criteria.createAlias("Tsop.Task_ID", "task");
criteria.createAlias("Tsop.Option_ID", "op");
criteria.add(Restrictions.eqProperty("Tsop.task.Task_ID", "task.task_ID"));
criteria.add(Restrictions.eqProperty("Tsop.options.Option_ID", "op.option_ID"));
不是因爲TASK_ID工作Task_options無法解析。