-3

I've always seen people using interfaces of collection framework when there is a One-to-many relationship in Entity classes. For Example if Author has written many books then it Looks like this:

@OneToMany(mappedBy = "author")
private List<Book> books;

and why not

@OneToMany(mappedBy = "author")
private ArrayList<Book> books;

Or

@OneToMany(mappedBy = "author")
private Set<Book> books;

and not

@OneToMany(mappedBy = "author")
private HashSet<Book> books;

So why to use interfaces and not implementations of collection framework?

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
Parth Savaliya
  • 353
  • 6
  • 13

1 Answers1

2

The recommended way is to use Interfaces rather than concrete types as Hibernate behind the scene injects it's own implementations ( like PersistenBag, PersistentSet etc.) to provide it's lazy loading / dirty check and other magic. Checkout this answer too.

Shailendra
  • 8,874
  • 2
  • 28
  • 37