I'm building a tree structured Object model. I.o.w. there a single POJO (ResourceEJB) with a parent reference (Many to One) and a collection of child references (One to Many) which are of the same type.
I have a method which determines all ResourceEJB object that share a certain ResourceEJB parent:
public Collection<ResourceEJB> findByParentID(Long aParentID) throws DAOException { try { ResourceEJB myParent = null; if(aParentID != null) myParent = (ResourceEJB)findByID(aParentID); return theManager.createQuery( "from ResourceEJB resource where resource.parent = :parent") .setParameter("parent", myParent).getResultList(); } catch(Exception anException) { throw new DAOException(anException); } }
if( aParentId == null ) { return theManager.createQuery( "from ResourceEJB resource where resource.parent IS NULL") .getResultList(); }