When I create a new entity class and want to have more than one List in it, Xpert.ivy says: "If an entity class has more than one collection you have to use 'Set' instead of 'List' as type."

I think the JEE Persistence API does not have this limitation.

asked 14.08.2014 at 11:32

HaraldWeber's gravatar image

accept rate: 33%

edited 14.08.2014 at 11:32

The JPA itself also has this limitation if the list's are eagerly fetched (which is like this in ivy). Eager means also all entries in the list's are fetched/loaded if the main entity is loaded. If there is more than one list, all relations are loaded with a big join (bad performance) and then jpa (hibernate) has to find the entries for each list, but then this list would have multiple entries of the same id because of the big join, so it is necessary that the type is Set.

The advice is to use no mapped by relations if not relay necessary, because this results in very bad performance.


type Employee {
  field company Company MANY_TO_ONE;
type Company {
  field employees List<Employee> ONE_TO_MANY mapped by company;

IF you now load just one employee (e.g. find(Employee.class, 1)) the result is the employee with its company and all employee's in the list of employees of the company. So do not use the employees list of the company, remove it. If you wan to get all employees of a company use a query to find them all.


answered 18.08.2014 at 12:41

Christian%20Strebel's gravatar image

Christian St... ♦
accept rate: 88%

This does not solve your problem but is some information to understand your problem:

  • List: Ordered collection of entities, duplicate allowed.
  • Set: Unordered collection of unique entities, duplicates not allowed.

In JPA there is no such restriction but:

When defining entities you should use 'set' instead of 'list' for collections.

This follows the exact same rules as using different collection types anywhere else throughout your code. You could use Object or Collections for all your references, yet in most cases you use more concrete types.

For example, when I see a List, I know it comes sorted in some way, and that duplicates are either acceptable or irrelevant for this case. When I see a Set, I usually expect it to have no duplicates and no specific order (unless it's a SortedSet). When I see a Collection, I don't expect anything more from it than to contain some entities.

Solution: Change all collections to sets when used in a domain-class.


answered 14.08.2014 at 13:38

Daniel%20Oechslin's gravatar image

Daniel Oechslin
accept rate: 39%

Its a bit strange.. The method <T> java.util.List<T> findAll(java.lang.Class<t> entityClass)
in IIvyEntityManager returns a List. Shouldn't it return a Set if we should always use Sets in entity classes?

(14.08.2014 at 14:12) HaraldWeber HaraldWeber's gravatar image

Well that's a good question. Maybe ivy wants it as easy as possible so they tried not to confuse a user with different collections. We should wait for an ivy-team asnwer...

(14.08.2014 at 16:51) Daniel Oechslin Daniel%20Oechslin's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 14.08.2014 at 11:32

Seen: 1,581 times

Last updated: 18.08.2014 at 12:41