Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Toplink Entity that references itself  RSS feed

Edwin Nels Johnson
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!