From 0ec2e527fc40ee79b38c1e44f43e340128859331 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sun, 22 Aug 2004 10:44:54 +0000 Subject: [PATCH] minor changes to collections git-svn-id: https://svn.jboss.org/repos/hibernate/trunk/Hibernate3/doc@4413 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- reference/en/modules/collection_mapping.xml | 44 ++++++++++++--------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/reference/en/modules/collection_mapping.xml b/reference/en/modules/collection_mapping.xml index 0668636f32..98b973ae01 100644 --- a/reference/en/modules/collection_mapping.xml +++ b/reference/en/modules/collection_mapping.xml @@ -572,10 +572,11 @@ kittens = cat.getKittens(); //Okay, kittens collection is a Set Collections (other than arrays) may be lazily initialized, meaning they load their state from the database only when the application needs to access it. - Initialization happens transparently to the user so the application would not - normally need to worry about this (in fact, transparent lazy initialization is - the main reason why Hibernate needs its own collection implementations). - However, if the application tries something like this: + Initialization of collections owned by persistent instances happens transparently + to the user, so the application would not normally need to worry about this (in + fact, transparent lazy initialization is the main reason why Hibernate needs its + own collection implementations). However, if the application tries something like + this: It could be in for a nasty surprise. Since the permissions collection was not - initialized when the Session was committed, - the collection will never be able to load its state. The fix is to move the + initialized when the Session was closed, the collection + will not be able to load its state. Hibernate does not support lazy + initialization for detached objects. The fix is to move the line that reads from the collection to just before the commit. (There are other more advanced ways to solve this problem, however.) - Alternatively, use a non-lazy collection. Since lazy initialization can lead to + It's possible to use a non-lazy collection. Since lazy initialization can lead to bugs like that above, non-laziness is the default. However, it is intended that - lazy initialization be used for almost all collections, especially for - collections of entities (for reasons of efficiency). + lazy initialization be used for almost all collections, especially for collections + of entities (for reasons of efficiency). If you define too many non-lazy associations + in your object model, Hibernate will end up needing to fetch the entire database + into memory in every transaction! @@ -662,16 +666,15 @@ Integer accessLevel = (Integer) permissions.get("accounts"); // Error!]]> - You can use the filter() method of the Hibernate Session API to - get the size of a collection without initializing it: + You can use a collection filter to get the size of a collection without initializing it: - + - filter() or createFilter() are also used to - efficiently retrieve subsets of a collection without needing to initialize the whole - collection. + The createFilter() method is also used to efficiently retrieve subsets + of a collection without needing to initialize the whole collection. (And the new + <filter> functionality is a more powerful approach.) @@ -905,9 +908,9 @@ session.update(category); // The relationship will be saved]]>Ternary Associations - There are two possible approaches to mapping a ternary association. One approach is to use - composite elements (discussed below). Another is to use a Map with an - association as its index: + There are three possible approaches to mapping a ternary association. One approach + is to use composite elements (discussed below). Another is to use a Map + with an association as its index: @@ -922,6 +925,11 @@ session.update(category); // The relationship will be saved]]> ]]> + + A final alternative is to simply remodel the association as an entity class. This + is the approach we use most commonly. + +