如何加入多个表使用hibernate Criteria,其中实体关系是多对多的

时间:2017-04-23 09:48:57

标签: java hibernate criteria

我有以下三个表,其中包含多对多关系中的任务和选项

任务表

@Entity
@Table(name = "TASK")
public class Task implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "TASK_ID")
    private int task_ID;

    @Column(name = "TASK_NAME")
    private String task_Name;

    @OneToMany(mappedBy="task")
    private Set<Task_Options> task_options;

选项表

@Entity
@Table(name = "OPTION")
public class Options implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "OPTION_ID")
    private int option_ID;

    @Column(name = "OPTION_VALUE")
    private String option_Value;

    @OneToMany(mappedBy = "options")
    private Set<Task_Options> task_options;

task_options表

@Entity
@Table(name = "TASK_OPTIONS")
public class Task_Options implements Serializable {

    @EmbeddedId
    private pk pk;

    @ManyToOne
    private Task task;

    @ManyToOne
    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;
    }

    public pk getPk() {
        return pk;
    }

    public void setPk(pk pk) {
        this.pk = pk;
    }



    /*Inner Class for Composite primary key*/
    @Embeddable
    public static class pk implements Serializable {

        private int Task_ID;        
        private int Option_ID;

        public int getTask_ID() {
            return Task_ID;
        }

        public void setTask_ID(int Task_ID) {
            this.Task_ID = Task_ID;
        }

        public int getOption_ID() {
            return Option_ID;
        }

        public void setOption_ID(int Option_ID) {
            this.Option_ID = 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

任何建议?

1 个答案:

答案 0 :(得分:0)

我认为这样做会:

Criteria criteria = session.createCriteria(Task_Options.class);
criteria.setFetchMode("options", FetchMode.EAGER);
criteria.setFetchMode("task", FetchMode.EAGER);
criteria.createAlias("task", "tsk")
criteria.add(Restrictions.eq("tsk.TASK_ID", 2));

请参阅this回答,了解FetchMode.EAGER

的用法

这将选择Task_Options您可以从Options获取Optionsoption_Value获取function doSomething (isGoingToResolve = true) { return new Promise((resolve, reject) => { if (isGoingToResolve) { resolve("something") } else { reject("something else") } }).then(response => { console.log("in my function",response) }).catch(error => { console.log("in my function",error) }) } doSomething().then(response => { console.log("in my main call", response) })