tree 结构很常见,当persist 到数据库中。

有些操作,在db 中更好。

1。取得所有的叶子节点。

SELECT Name FROM Projects p WHERE NOT EXISTS( SELECT * FROM Projects WHERE Parent=p.VertexId)

2。multilevel operation ,用数据库的辅助表, 用triger 。
CREATE TABLE ProjectPaths(

VertexId INTEGER,

Depth INTEGER,

Path VARCHAR(300) 。



)
3. 用 hibernate 时,如果 stack over flow,考虑用 stack 代替recursive algrithm

public void traverseDepthFirst( AST ast )
{
// Root AST node cannot be null or
// traversal of its subtree is impossible.
if ( ast == null )
{
throw new IllegalArgumentException(
"node to traverse cannot be null!" );
}
// Map to hold parents of each
// AST node. Unfortunately the AST
// interface does not provide a method
// for finding the parent of a node, so
// we use the Map to save them.

Map parentNodes = new HashMap();

// Start tree traversal with first child
// of the specified root AST node.

AST currentNode = ast.getFirstChild();

// Remember parent of first child.

parentNodes.put( currentNode , ast );

// Iterate through nodes, simulating
// recursive tree traversal, and add them
// to queue in proper order for later
// linear traversal. This "flattens" the
// into a linear list of nodes which can
// be visited non-recursively.

while ( currentNode != null )
{
// Visit the current node.

strategy.visit( currentNode );

// Move down to current node's first child
// if it exists.

AST childNode = currentNode.getFirstChild();

// If the child is not null, make it
// the current node.

if ( childNode != null )
{
// Remember parent of the child.

parentNodes.put( childNode , currentNode );

// Make child the current node.

currentNode = childNode;

continue;
}

while ( currentNode != null )
{
// Move to next sibling if any.

AST siblingNode = currentNode.getNextSibling();

if ( siblingNode != null )
{
// Get current node's parent.
// This is also the parent of the
// sibling node.

AST parentNode = (AST)parentNodes.get( currentNode );

// Remember parent of sibling.

parentNodes.put( siblingNode , parentNode );

// Make sibling the current node.

currentNode = siblingNode;

break;
}
// Move up to parent if no sibling.
// If parent is root node, we're done.

currentNode = (AST)parentNodes.get( currentNode );

if ( currentNode.equals( ast ) )
{
currentNode = null;
}
}
}




参考:

http://wordhoard.northwestern.edu/userman/hibernatechanges.html

《Tansact Sql cookbook.》








西津渡