<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Why can I only have one field of the type List in my entity class?</title><link>https://answers.axonivy.com/questions/912/why-can-i-only-have-one-field-of-the-type-list-in-my-entity-class</link><description>&lt;p&gt;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."&lt;/p&gt;
&lt;p&gt;I think the JEE Persistence API does not have this limitation.&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/912/why-can-i-only-have-one-field-of-the-type-list-in-my-entity-class" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 18 Aug 2014 12:41:11 -0400</lastBuildDate><item><title>Answer by Christian Strebel</title><link>https://answers.axonivy.com/questions/912/why-can-i-only-have-one-field-of-the-type-list-in-my-entity-class/922</link><description>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;The advice is to use no mapped by relations if not relay necessary, because this results in very bad performance.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;type Employee {
  field company Company MANY_TO_ONE;
}
type Company {
  field employees List&amp;lt;Employee&amp;gt; ONE_TO_MANY mapped by company;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;IF you now load just one employee (e.g. &lt;code&gt;find(Employee.class, 1)&lt;/code&gt;) 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.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Christian Strebel</dc:creator><pubDate>Mon, 18 Aug 2014 12:41:11 -0400</pubDate><guid>https://answers.axonivy.com/questions/912/why-can-i-only-have-one-field-of-the-type-list-in-my-entity-class/922</guid></item><item><title>Answer by Daniel Oechslin</title><link>https://answers.axonivy.com/questions/912/why-can-i-only-have-one-field-of-the-type-list-in-my-entity-class/914</link><description>&lt;p&gt;This does &lt;em&gt;not&lt;/em&gt; solve your problem but is some information to understand your problem:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;List: Ordered collection of entities,
   duplicate allowed. &lt;/li&gt;
&lt;li&gt;Set: Unordered
   collection of unique entities,
   duplicates not allowed. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In JPA there is no such restriction but:&lt;/p&gt;
&lt;p&gt;When defining entities you &lt;em&gt;should&lt;/em&gt; use 'set' instead of 'list' for collections. &lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Change all collections to sets when used in a domain-class.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Oechslin</dc:creator><pubDate>Thu, 14 Aug 2014 13:38:30 -0400</pubDate><guid>https://answers.axonivy.com/questions/912/why-can-i-only-have-one-field-of-the-type-list-in-my-entity-class/914</guid></item></channel></rss>