-->
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.  [ 2 posts ] 
Author Message
 Post subject: Adding child to parent entity in many-to-one
PostPosted: Tue Oct 17, 2006 3:15 pm 
Newbie

Joined: Wed Sep 20, 2006 10:15 pm
Posts: 12
Hi folks,

I have a simple entity relationship where a Person can have many Photo. The Person entity has a List<Photo>, and Photo has a Person class member.

At the moment things work just fine as long as I do the following:

Person.getPhotos().add(Photo);
Photo.setPerson(Person);

Do I always have to do it from both sides manually? Is there any way I can get this done automatically?

My somewhat-hairy idea (I'm new to Java) was to extend ArrayList<> and create my own class that raises an event when items get added or removed. That didn't work since Hibernate didn't seem to know anything about my custom generic collection. The error was something like "expecting someSupaDupaList, actual was PersistentBag"...Things just got more and more complicated from there on, which made me think there must be an easier way of doing this.

Has anybody encountered this kind of situation before? And what did you implement to handle this?

Any hints/suggestions would be greatly appreciated.

Many thanks,
D.

UPDATE:
Nevermind - I can't believe how complicated I made the issue. Found the solution in http://forum.hibernate.org/viewtopic.php?t=965856 - incredibly simple!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 17, 2006 6:27 pm 
Newbie

Joined: Tue Jul 04, 2006 3:42 am
Posts: 9
Dear danywu

As a matter of OO programming, first of all i think that you can implement an "addPhoto" method to Person class, since it shall be person class behavioural responsibility to handle such as issue. Once i have read that it is not a good practice to change the state of an object externally, let it handle it self (pragmatic programmers site).

Therefore in the add method of Person class,

class Person{
...
addPhoto(Photo aP)
{
mPhotosList.add(p);
p.setPerson(this);
}
...

}

would be a good solution which will remove code duplications. This is again not a perfect fit to "do not change state externally", since Person changes state of Photo, but still an approximation to the ideal.

I know the answer has nothing to do with hibernate, but with OO programming.

Hope it helped you.

Regards,

Siyamed


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.