-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Association field initializer
PostPosted: Sat Apr 12, 2008 3:22 am 
Newbie

Joined: Sat Apr 12, 2008 3:09 am
Posts: 3
My entity class looks like this:
Code:
public class Customer {
    private Set<Image> _images;

    @OneToMany(...)
    public Set<Image> getImages() {
        return _images;
    }

    public void setImages(Set<Image> images) {
        _images = images;
    }

    public void add(Image image) {
        if (_images == null) {
            _images = new HashSet<Image>();
        }
        _images.add(image);
    }
}


But documentation recommends association field initialization:
Code:
public class Customer {
    private Set<Image> _images = new HashSet<Image>();
    ...
}


Why do I need association field initialization?
For example, I can use the customer.getImages().add(image) instead customer.add(image) for a new entity. If an entity is loaded from the database then Hibernate initializes the field.
Is this related to restriction "return same collection instance from getter if association accessed through getter / setter"?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.