From: David M. J. <dav...@nc...> - 2002-11-30 15:42:32
|
I've written a simple example application that shows how to create a simple web application (a newsfeed aggregator) with a pluggable persistence layer. I used the tried and true DAO pattern to hide the database access mechanism from the rest of the application. I implemented the DAO interface with both Hibernate and Castor. I did this to learn: - What are the drawbacks of hiding powerful persistence engine behind a simple DAO interface. What do you loose? Will long transaction support work? What about lazy loading? - What are the differences between and strenghts/weaknesses of Castor and Hibernate? - What is the best way to implement DAO with each of the tools. The code is very simple and (I hope) easy to follow, so if you have a chance please take a look. Look at the HibeAggregatorDAO and CastorAggregatorDAO implementations to see how I used Hiberate and Castor to implement the very same DAO. I'd love to get your opinions on the above topics. The readme is here: http://sourceforge.net/project/shownotes.php?release_id=125110 The download is here: http://sourceforge.net/project/showfiles.php?group_id=47722 Thanks, - Dave http://www.rollerweblogger.org/page/roller |
From: Gavin K. <ga...@ap...> - 2002-11-30 23:46:30
|
Looking over the source, I'm wondering about a comment in HibeAggregatorDAO // if cascade worked, we could just do this //removeObject(feed); this is followed by: ses.delete( "select newsfeed.subscriptions.elements " +"from newsfeed in class ag.Newsfeed" ) which should be ses.delete( "select newsfeed.subscriptions.elements " +"from newsfeed in class ag.Newsfeed " +"where newsfeed = ?", feed, Hibernate.association(feed) ); but you wouldn't need that whole line if you had: <set role="subscriptions" table="subscription" cascade="delete"> <key column="newsfeed_id" length="32" /> <one-to-many class="ag.Subscription" /> </set> in the mapping...... P.S. I'm not keen on the way you add items to bidirectional association. The correct idiom is: parent.getChildren().add(child); //yes, even if the children collection is readonly="true" child.setParent(parent); sess.save(child); omitting the first line could cause bugs in more complicated circumstances. ----- Original Message ----- From: "David M. Johnson" <dav...@nc...> To: <hib...@li...> Sent: Sunday, December 01, 2002 1:29 AM Subject: [Hibernate] Hibernate vs. Castor example > > I've written a simple example application that shows how > to create a simple web application (a newsfeed aggregator) > with a pluggable persistence layer. I used the tried > and true DAO pattern to hide the database access > mechanism from the rest of the application. > I implemented the DAO interface with both Hibernate > and Castor. > > I did this to learn: > > - What are the drawbacks of hiding powerful persistence > engine behind a simple DAO interface. What do you loose? > Will long transaction support work? What about lazy loading? > > - What are the differences between and strenghts/weaknesses > of Castor and Hibernate? > > - What is the best way to implement DAO with each of > the tools. > > The code is very simple and (I hope) easy to follow, > so if you have a chance please take a look. Look at the > HibeAggregatorDAO and CastorAggregatorDAO implementations > to see how I used Hiberate and Castor to implement the > very same DAO. I'd love to get your opinions on the > above topics. > > The readme is here: > http://sourceforge.net/project/shownotes.php?release_id=125110 > > The download is here: > http://sourceforge.net/project/showfiles.php?group_id=47722 > > > Thanks, > - Dave > > http://www.rollerweblogger.org/page/roller > > > > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Get the new Palm Tungsten T > handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en > _______________________________________________ > hibernate-devel mailing list > hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel |
From: David M. J. <dav...@nc...> - 2002-12-01 01:31:09
|
I tried adding the cascade="delete" attribute that you mentioned and the parent.getChildren().add(child) idiom, but when I did I got some strange behavior. When the removeObject(feed) call was made, the Newsfeed was deleted by the Items that refer to the feed were not deleted. Instead, all of the items had their newsfeed_id fields cleared. I'm on MySQL, does cascading delete work on MySQL? - Dave Gavin King wrote: >Looking over the source, I'm wondering about a comment in HibeAggregatorDAO > > // if cascade worked, we could just do this > //removeObject(feed); > >this is followed by: > >ses.delete( "select newsfeed.subscriptions.elements " > +"from newsfeed in class ag.Newsfeed" ) > >which should be > >ses.delete( "select newsfeed.subscriptions.elements " > +"from newsfeed in class ag.Newsfeed " > +"where newsfeed = ?", > feed, Hibernate.association(feed) ); > >but you wouldn't need that whole line if you had: > > <set role="subscriptions" table="subscription" cascade="delete"> > <key column="newsfeed_id" length="32" /> > <one-to-many class="ag.Subscription" /> > </set> > >in the mapping...... > > >P.S. I'm not keen on the way you add items to bidirectional association. The >correct idiom is: > >parent.getChildren().add(child); //yes, even if the children collection is >readonly="true" >child.setParent(parent); >sess.save(child); > >omitting the first line could cause bugs in more complicated circumstances. > >----- Original Message ----- >From: "David M. Johnson" <dav...@nc...> >To: <hib...@li...> >Sent: Sunday, December 01, 2002 1:29 AM >Subject: [Hibernate] Hibernate vs. Castor example > > > > >>I've written a simple example application that shows how >>to create a simple web application (a newsfeed aggregator) >>with a pluggable persistence layer. I used the tried >>and true DAO pattern to hide the database access >>mechanism from the rest of the application. >>I implemented the DAO interface with both Hibernate >>and Castor. >> >>I did this to learn: >> >>- What are the drawbacks of hiding powerful persistence >>engine behind a simple DAO interface. What do you loose? >>Will long transaction support work? What about lazy loading? >> >>- What are the differences between and strenghts/weaknesses >>of Castor and Hibernate? >> >>- What is the best way to implement DAO with each of >>the tools. >> >>The code is very simple and (I hope) easy to follow, >>so if you have a chance please take a look. Look at the >>HibeAggregatorDAO and CastorAggregatorDAO implementations >>to see how I used Hiberate and Castor to implement the >>very same DAO. I'd love to get your opinions on the >>above topics. >> >>The readme is here: >>http://sourceforge.net/project/shownotes.php?release_id=125110 >> >>The download is here: >>http://sourceforge.net/project/showfiles.php?group_id=47722 >> >> >>Thanks, >>- Dave >> >>http://www.rollerweblogger.org/page/roller >> >> >> >> >> >> >>------------------------------------------------------- >>This SF.net email is sponsored by: Get the new Palm Tungsten T >>handheld. Power & Color in a compact size! >>http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en >>_______________________________________________ >>hibernate-devel mailing list >>hib...@li... >>https://lists.sourceforge.net/lists/listinfo/hibernate-devel >> >> > > > > |
From: Dave J. <sno...@nc...> - 2002-12-01 14:59:54
|
I made a number of small changes to get my example working on HSQL with cascading deletes, then when I switched back to MySQL cascading deletes worked there as well! So yes, cascading deletes work on MySQL. I'd like my example to be using Hybernate in the best way possible (under the constraints of DAO), so if you guys see any other problems in my code please let me know. I'll post a revised version once I get more feedback from the Castor folks. Thanks for your help. - Dave David M. Johnson wrote: > I tried adding the cascade="delete" attribute that you mentioned and > the parent.getChildren().add(child) idiom, but when I did I got some > strange behavior. When the removeObject(feed) call was made, the > Newsfeed was deleted by the Items that refer to the feed were not > deleted. Instead, all of the items had their newsfeed_id fields cleared. > > I'm on MySQL, does cascading delete work on MySQL? > > - Dave > |
From: Paul S. <pau...@ne...> - 2002-12-01 02:46:52
|
Hi, that reminds me of something: is there bidirectional association support for List types? I can't seem to find it (it's there for sets and bags). Regards, PaulS. > > P.S. I'm not keen on the way you add items to bidirectional association. The > correct idiom is: > > parent.getChildren().add(child); //yes, even if the children collection is > readonly="true" > child.setParent(parent); > sess.save(child); > |
From: Max R. A. <ma...@eo...> - 2002-12-01 14:26:52
|
> P.S. I'm not keen on the way you add items to bidirectional association. The > correct idiom is: > > parent.getChildren().add(child); file://yes, even if the children collection is > readonly="true" > child.setParent(parent); > sess.save(child); > > omitting the first line could cause bugs in more complicated circumstances. Hmm - isn't this idiom inefficient ? Doesn't parent.getChildren().add() (or any method on the collection) result in loading all the children ? If the only intention is to add the child to the parent shouldn't the child.setParent(parent) be enough ? And then if I wish to have the parent in a "correct" state then I just do a s.load(parent, parent.getId()) ? /max |
From: Juozas B. <ba...@ce...> - 2002-12-01 15:13:21
|
It can be a problem in "transient state": Parent p = new Parent(); Child c = new Child(); c.setParent(p); assertTrue("parent must have a child", p.getChildren() != 0 ); there are two possible solutions : 1) implement "void setParent(Parent p){ this.p = p; p.getChildren().add(this); }" manualy; 2) code generation and "abstract" managed methods ( "transparence" ). I prefer "transparence", but it needs "Factory" if implementation is generated at runtime or JDO style postprocessing if "new" is used to create transient instances. ----- Original Message ----- From: "Max Rydahl Andersen" <ma...@eo...> To: "hibernate list" <hib...@li...> Sent: Sunday, December 01, 2002 4:26 PM Subject: Re: [Hibernate] Hibernate vs. Castor example > > P.S. I'm not keen on the way you add items to bidirectional association. > The > > correct idiom is: > > > > parent.getChildren().add(child); file://yes, even if the children > collection is > > readonly="true" > > child.setParent(parent); > > sess.save(child); > > > > omitting the first line could cause bugs in more complicated > circumstances. > > Hmm - isn't this idiom inefficient ? > Doesn't parent.getChildren().add() (or any method on the collection) result > in loading all the children ? > > If the only intention is to add the child to the parent shouldn't the > child.setParent(parent) be enough ? > > And then if I wish to have the parent in a "correct" state then I just do a > s.load(parent, parent.getId()) ? > > /max > > > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > hibernate-devel mailing list > hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel |
From: Jozsa K. <dy...@on...> - 2002-12-01 18:53:50
|
On Sun, Dec 01, 2002 at 10:44:33AM +1100, Gavin King wrote: > P.S. I'm not keen on the way you add items to bidirectional association. The > correct idiom is: > > parent.getChildren().add(child); //yes, even if the children collection is > readonly="true" > child.setParent(parent); > sess.save(child); > > omitting the first line could cause bugs in more complicated circumstances. So exactly what could happen if we use a bidirectional relationship without setting one way read-only and set both sides of the relationship explicitly? I believe I'm just doing that and *never* experienced any problems. Beside that, I feel it quite an overhead doing all this manually, so I'd like to know about both the drawbacks, the dangers, and why that's all unavoidable doing so, or why it cannot happen automatically in the background.. thanks for the explanation in advance, dyn -- .Digital.Yearning.for.Networked.Assassination.and.Xenocide |
From: Dave J. <sno...@nc...> - 2002-12-04 05:10:02
|
In the Hibernate implementation of the DAO, I implement the removeNewsfeed method shown below. Why is the call to ses.load() necessary? - Dave public void removeNewsfeed( Newsfeed feed ) throws DAOException { removeObject( ag.Newsfeed.class, feed.getId(), feed ); } private void removeObject( Class clazz, String id, Object obj ) throws DAOException { Session ses = null; try { ses = sessionFactory.openSession(); obj = ses.load(clazz,id); ses.delete(obj); ses.flush(); ses.connection().commit(); } catch (Exception e) { try { ses.connection().rollback(); } catch (Exception ex) { e.printStackTrace(); }; throw new DAOException(e); } finally { try { ses.close(); } catch (Exception ex) { ex.printStackTrace(); }; } } |