Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you have a List or Set-based collection and you add a new object into your collection, Hibernate will always hit the database because it compare one by one object by using equals implementation before saving or updating - when using a Set - or by comparing a index column when using a List. This behavior is needed because of the Set and List semantic. Because of that, the performance of your application can decrease significantly whether you have a bunch of records.</p> <p>Some workaround to overcome this issue</p> <p><strong>1º</strong> Conversion pattern by using a encapsuled Bag collection plus your desired Set or List exposed as a property</p> <pre><code>@Entity public class One { private Collection&lt;Many&gt; manyCollection = new ArrayList&lt;Many&gt;(); @Transient public Set&lt;Many&gt; getManyCollectionAsSet() { return new HashSet&lt;Many&gt;(manyCollection); } public void setManyCollectionAsSet(Set&lt;Many&gt; manySet) { manyCollection = new ArrayList&lt;Many&gt;(manySet); } /** * Keep in mind that, unlike Hibernate, JPA specification does not allow private visibility. You should use public or protected instead */ @OneToMany(cascade=ALL) private Collection&lt;Many&gt; getManyCollection() { return manyCollection; } private void setManyCollection(Collection&lt;Many&gt; manyCollection) { this.manyCollection = manyCollection; } } </code></pre> <p><strong>2º</strong> Use ManyToOne instead of OneToMany</p> <pre><code>@Entity public class One { /** * Neither cascade nor reference */ } @Entity public class Many { private One one; @ManyToOne(cascade=ALL) public One getOne() { return one; } public void setOne(One one) { this.one = one } } </code></pre> <p><strong>3º</strong> Caching - when applied because of, depending on your requirements, your configuration can increase or decrease the performance of your application. See <a href="http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#performance-cache-readonly" rel="nofollow noreferrer">here</a></p> <p><strong>4°</strong> SQL constraint - If you want a collection that behaves like a Set, you can use a SQL constraint, which can be applied to a column or <em>set of columns</em>. See <a href="http://www.w3schools.com/sql/sql_unique.asp" rel="nofollow noreferrer">here</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload