-1

In Hibernate Entities, in what cases is it the better to use List, as opposed to Set?

Example of Set

public class Department {

  private Long departmentId;

  private Set<Employee> employees;

  // Getter and Setter methods
}

Example of List

public class Department {

  private Long departmentId;

  private List<Employee> employees;

  // Getter and Setter methods
}
icedtrees
  • 6,134
  • 5
  • 25
  • 35
  • My personal opinion : I prefer sets becauses they automatically remove duplicates. – Arnaud Denoyelle Apr 11 '14 at 08:38
  • For Set public class A { private Long aId; private Set bS; // Getter and Setter methods } For List public class A { private Long aId; private List lB; // Getter and Setter methods } – user3522394 Apr 11 '14 at 09:32

2 Answers2

0

It depends on your requirements - in this case I suppose Set would be more suitable because I don't think duplicated employees should exist in department.

IMHO basic rule is - if you allow duplicates use List, otherwise use Set.

Blekit
  • 663
  • 4
  • 7
  • For Set public class A { private Long aId; private Set bS; // Getter and Setter methods } For List public class A { private Long aId; private List lB; // Getter and Setter methods } – user3522394 Apr 11 '14 at 09:32
0

The main difference is that a list has ordering while a set does not. see Hibernate Set Or List

Community
  • 1
  • 1
mel3kings
  • 8,857
  • 3
  • 60
  • 68