-->
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.  [ 5 posts ] 
Author Message
 Post subject: Working with Collections
PostPosted: Mon Jul 23, 2007 10:52 pm 
Beginner
Beginner

Joined: Fri Jul 28, 2006 12:01 pm
Posts: 21
I have an entity (InvalidAddress) with a many to one relationship with another entity (ServiceRequest). I am trying to add an InvalidAddress entity to the InvalidAddressCollection owned by ServiceRequest
Code:
    InvalidAddress invalidAddress = new InvalidAddress();
     
    //perform setters on invalidAddress
     
    invalidAddress.setServiceRequest(serviceRequest);
    serviceRequest.getInvalidAddressCollection().add(invalidAddress);
    entityManager.persist(invalidAddress);


I am getting a NPE because serviceRequest.getInvalidAddressCollection() is returning null when there are no related InvalidAddress entities. I know this makes sense but it seems like a waste to have to do this before I add any related entity to any collection

Code:
    if (serviceRequest.getInvalidAddressCollection() == null) {
       serviceRequest.setInvalidAddressCollection(new java.util.ArrayList();
    }
    serviceRequest.getInvalidAddressCollection().add(invalidAddress);


Is this very repetitive boiler-plate required when working with collections?

It would seem like the EntityManager should initialize the Collection and just return an empty list instead of a null list. Or is that up to me when I implement the getter on ServiceRequest?

Thanks for any advice.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 2:46 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
No the EntityManager should not change the semantic.
But what people do is

Code:
public class ServiceRequest {
   private Set<InvalidAddress> invalidAddressCollection = new HashSet<InvalidAddress>();
   ...
}

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 3:56 pm 
Beginner
Beginner

Joined: Fri Jul 28, 2006 12:01 pm
Posts: 21
So is this common practice?

I am just learning JPA and it is not shown like that in any of the examples I can find. Is that going to cause excess memory consumption since a new Set/List/Collection is created when the entity is created?

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 4:13 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Just do it...

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 4:42 pm 
Beginner
Beginner

Joined: Fri Jul 28, 2006 12:01 pm
Posts: 21
Nike.... :-)

Thanks


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

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.