1

ProcessSolution Entity :

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

And I'm Executing HQL Query in DAO as:

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

My Problem is I'm Getting ElementCollection i.e. processSolutionSteps repeated (Multiple Rows).
So I changed it From List<> to Set<>, now I'm getting correct result but its order is not preserved.

What I have tried:

  1. For Set I have tried LinkedHashSet but problem still persist.
  2. @LazyCollection(LazyCollectionOption.FALSE) from here
  3. @Fetch(FetchMode.Select) from another SO source

Any Idea how to solve this problem.

Updated :
Sample Data :

**process_solution**

+---------------+----------------+
|  process_id   |  process_name  |
+---------------+----------------+
|      3        |  process 1     |
+---------------+----------------+

**process_solution_step**

+---------------+----------------+
|  process_id   |  solution_step |
+---------------+----------------+
|      3        |    step 1      |
+---------------+----------------+
|      3        |    step 2      |
+---------------+----------------+

If I print Process Solution Steps I get the result as

  1. step 1
  2. step 1
  3. step 2
  4. step 2

If I print Employee Lenth I got correct result.

Shantaram Tupe
  • 1,646
  • 3
  • 16
  • 42

0 Answers0