File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Toplink Entity that references itself" Watch "Toplink Entity that references itself" New topic
Author

Toplink Entity that references itself

Edwin Nels Johnson
Greenhorn

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:

@Entity
private class Task{

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long TaskId

@ManyToOne
private Task parentTaskId;

@OneToMany(mappedBy="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
 
 
subject: Toplink Entity that references itself
 
Similar Threads
Tree structure in Hibernate
Recursive Mapping of collections
mapping One-to-many relationships using NetBeans and Not Hibernate
Adding many-to-many relationships using TopLink
Regarding apply JOIN by using JPQL.