Doing nested sets in your Lift snippet is pretty easy with JPA. I'm doing it on one of my Lift sites. One thing to remember, however, is to use a transaction. You don't want to do half the update to the tree and then have another use start an update before you've finished yours (or have half of it fail). Once it gets messed up it's a disaster.
Nested tables are good when you have lots of lookups, particularly pulling ancestors or descendants, but not too many updates. Inserts are not too bad, but if you have to move whole subtrees, that's a pain. I'm using one for, among other things, a forum, so messages once posted never get moved. Nested sets work great for that. But, honestly, it is astonishing to me that Java, Scala, and especially JPA/Hibernate apparently have no built in features for creating, manipulating, and persisting trees (and networks are even less supported). These problems were largely solved long ago. Why aren't they built in? Rails has a cool set of plugins -- acts_as_list, acts_as_tree, and acts_as_nested_set -- that make it drop-dead simple to implement trees. I was thinking that it would be cool if Lift had something similar but maybe as a trait? So my object extends ActsAsNestedSet? I tried to do this in Hibernate, but it would require the object to query for other similar objects. For example, if I had a Node object that I was persisting, I'd like to do class Node extends ActsAsNestedSet And then define methods such as moveToChildOf(node), moveToFirst, moveBack, moveForth, moveToLast, etc. But it appears that you need an EntityManager to run a query, and there doesn't seem to be a way to go backwards from the Node to the EntityManager (probably because, if I understand it correctly, there will only be one copy of a given Node in memory but there may be multiple EntityManagers?). So I put the code in the snippet. If you come up with any clever solutions, I'd love to hear them. Chas. Tim Perrett wrote: > Hey Derek, > > I've been playing around with this nested set stuff today - it's pretty > cool. I nearly have an app working that uses the db for page content and > maps the urls out however you want, it's pretty neat. > > I have a slight HQL problem with subquery's but I'll post that 2mro :-) > > Thanks for all your help > > Tim > > Sent from my iPhone > > On 30 Nov 2008, at 23:37, "Derek Chen-Becker" <[EMAIL PROTECTED] > <mailto:[EMAIL PROTECTED]>> wrote: > >> IIRC, JPA has support for batch updates, but I'm not 100% positive on >> that. I'll have more time tomorrow to look at this if you'd still like >> to discuss it. >> >> Derek >> >> On Sun, Nov 30, 2008 at 12:32 PM, Tim Perrett < >> <mailto:[EMAIL PROTECTED]>[EMAIL PROTECTED] >> <mailto:[EMAIL PROTECTED]>> wrote: >> >> >> Derek, >> >> Those links are *extremely* good - thank you! I've not really done >> anything with tree structures before and they are really, really >> usefull. >> >> One thing however, it appears the optimal way of doing the insert >> queries would be with stored procedures, however that im not down >> with, and ideally, i want to keep database vendor independent (part of >> the point of using JPA) so ideally i want to suck that up into the >> application logic. For instance: >> >> --START >> SELECT @myRight := rgt FROM nested_category WHERE name = >> 'TELEVISIONS'; >> >> UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myRight; >> UPDATE nested_category SET lft = lft + 2 WHERE lft > @myRight; >> >> INSERT INTO nested_category(name, lft, rgt) VALUES('GAME CONSOLES', >> @myRight + 1, @myRight + 2); >> --END >> >> This makes use of variables, and i could of course do this via 3 >> separate queries in JPA, but i wondered if there was a neater way to >> construct this type of thing with JPA? >> >> Any advice you guys have is most welcome >> >> Cheers >> >> Tim >> >> >> On Nov 30, 2:27 pm, "Derek Chen-Becker" <[EMAIL PROTECTED] >> <mailto:[EMAIL PROTECTED]>> wrote: >> > If you're heavily skewed towards reads and not writes (as it >> seems in the >> > case of a CMS), you might want to look at Celko nested sets: >> > >> > >> >> <http://www.intelligententerprise.com/001020/celko.jhtml>http://www.intelligententerprise.com/001020/celko.jhtml >> > >> >> <http://dev.mysql.com/tech-resources/articles/hierarchical-data.html>http://dev.mysql.com/tech-resources/articles/hierarchical-data.html >> > >> > A little more work on updates than an adjacency model, but a lot >> more >> > efficient. >> > >> > Derek >> >> >> >> >> > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~----------~----~----~----~------~----~------~--~---
