Hibernate HQL Join Fetch返回重复的行

时间:2017-06-26 11:14:40

标签: java hibernate orm hql

ProcessSolution实体:

@Entity
@Table(name="process_solution")
public class ProcessSolution implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="process_id", columnDefinition="INT(10) UNSIGNED")
    private Integer processId;

    @Column(name="process_name", length=120, nullable=false)
    private String processName;

    @ElementCollection(fetch=FetchType.LAZY)
    //@LazyCollection(LazyCollectionOption.FALSE)
    //@Fetch(FetchMode.Select)
    @JsonIgnore
    @CollectionTable(name="process_solution_step", 
        joinColumns=@JoinColumn(name="process_id"),
        foreignKey=@ForeignKey(name="fk_process_solution_step_process_id")
    )
    @Column(name="solution_step", length=200, nullable=false)
    private List<String> processSolutionSteps = new ArrayList<>();

    @ManyToOne
    @JoinColumn( name="category_id", columnDefinition="INT(10) UNSIGNED",nullable=false,
        foreignKey=@ForeignKey(name="fk_process_solution_category")
    )
    private Category category;

    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="process_solution_employee",
        joinColumns={@JoinColumn(name="process_id")},
        inverseJoinColumns={@JoinColumn(name="emp_id",columnDefinition="INT(10) UNSIGNED")},
        foreignKey=@ForeignKey(name="fk_process_employee_process_solution_process_id"),
        inverseForeignKey=@ForeignKey(name="fk_process_employee_employee_emp_id")
    )
    private Set<Employee> employees = new HashSet<>();

    // Getters/Setters
}

我正在 DAO中执行HQL查询:

@Override
public ProcessSolution getProcessSolution(Integer processId) {
    Session session = this.sessionFactory.openSession();
    final String GET_PS = "SELECT ps FROM ProcessSolution ps JOIN FETCH ps.processSolutionSteps JOIN FETCH ps.employees WHERE ps.processId = :processId";
    //ProcessSolution processSolution = session.get(ProcessSolution.class, processId);
    ProcessSolution processSolution = ( ProcessSolution ) session.createQuery(GET_PS)
                                        .setInteger("processId", processId).uniqueResult();
    session.close();
    return processSolution;
}

我的问题是我正在ElementCollection processSolutionSteps重复(多行)
所以我将其从 List<> 更改为 Set<> ,现在我得到了正确的结果,但订单未保留

我尝试过:

  1. 对于Set,我尝试LinkedHashSet,但问题仍然存在。
  2. 来自here的@LazyCollection(LazyCollectionOption.FALSE)
  3. 来自其他 SO 来源的@Fetch(FetchMode.Select)
  4. 任何想法如何解决这个问题。

    更新:
    示例数据:

    **process_solution**
    
    +---------------+----------------+
    |  process_id   |  process_name  |
    +---------------+----------------+
    |      3        |  process 1     |
    +---------------+----------------+
    
    **process_solution_step**
    
    +---------------+----------------+
    |  process_id   |  solution_step |
    +---------------+----------------+
    |      3        |    step 1      |
    +---------------+----------------+
    |      3        |    step 2      |
    +---------------+----------------+
    

    如果我打印处理解决方案步骤,我会得到结果

    1. 第1步
    2. 第1步
    3. 第2步
    4. 第2步
    5. 如果我打印员工Lenth,我得到了正确的结果。

0 个答案:

没有答案