File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Toplink Entity that references itself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Toplink Entity that references itself" Watch "Toplink Entity that references itself" New topic

Toplink Entity that references itself

Edwin Nels Johnson

Joined: Mar 13, 2011
Posts: 3
I have a db table called Task. It has a structure like this:

taskid bigint(20) autoincrement identity
taskname varchar(255)
parentTaskId bigint(20)

The idea is to have a series of tasks just like MS Project. Some tasks can be subtasks of other parent tasks. The parentTaskId column will hold a reference to the TaskId from the parent row. When it comes to ORM, then I will have one to many and many to one relationships that refer to the same Entity.

I am trying to figure out how to implement this using Toplink Essentials - I found a Hibernate example that recommended something like this:

private class Task{

private long TaskId

private Task parentTaskId;

private Collection<Task> subTasks;


I am missing a few pieces though - when I call a named native query to fetch Task objects, I want the parentTaskId property to be populated with a Task object, not just the id from the row. And if a task is a parent task, I would like the subTasks collection to be populated with all the Task objects that have their parentTaskId column set as that TaskId.

Could someone point me to an example of this using Toplink?

James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
TopLink and Hibernate both support JPA, so the below example will work on either.

Your parentTaskId is a Task, so it will be populated with a Task. You should probably rename the field to parentTask to make sense.

You may also consider using EclipseLink which is more up to date than TopLink Essentials (EclipseLink is part of TopLink 11g).

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
I agree. Here's the link:
subject: Toplink Entity that references itself
jQuery in Action, 3rd edition