It's not a secret anymore!
The moose likes Object Relational Mapping and the fly likes @OneToMany/ManyToOne and @OneToOne relationship between same entities. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "@OneToMany/ManyToOne and @OneToOne relationship between same entities." Watch "@OneToMany/ManyToOne and @OneToOne relationship between same entities." New topic

@OneToMany/ManyToOne and @OneToOne relationship between same entities.

Adriano Meis

Joined: Dec 10, 2007
Posts: 2
I explain quickly what I should do:

I've trees, every tree has many branches. That's easy.

in pseudo-code:

@Entity Tree
-> @Id Integer treeId;
-> @OneToMany List<Branch> branches;
-> String treeName;

PK: [treeId]

@Entity Branch
-> @Id @ManyToOne Tree tree
-> @Id Integer branchId
-> String branchName;

PK: [tree, branchId]
(of course i used an embeddedId for the entity Branch.)

Well, what I wrote, works.
Now.. my problem:
I want to be able to know which is the last grown branch of a specific tree.

From an Entity-Relation point of view, I want to add a @OneToOne relationship between Tree and Branch: a Tree will have just ONE lastBranch.

So the Tree entity should become something like this:

@Entity Tree
-> @Id Integer treeId;
-> @OneToMany List<Branch> branches;
-> String treeName;
-> @OneToOne Branch lastBranch;

But i'm not very sure about this:

1) a relationship to a Branch should be mapped on the DB schema with two columns: the Branch composite id [TREE_ID, BRANCH_ID], in the case of Tree entity, it already have the TREE_ID value (since it is its primary key), so it will be so clever to not duplicate the TREE_ID column?

2) How should I map the inverse @OneToOne relationship "lastBranch" from Branch to Tree? Should not map it because it's already mapped by the @ManyToOne tree?

Surely some of you would suggest to dinamically extract in someway the last branch from the branches list, using some "age" attribute, sorting it, and getting last element.. but I want a more performant solution, and a solution i can use easily from HQL and OGNL, something like this:


I think that the @Formula solution can have some performance issues.. so i would like to phisically add this @OneToOne relationship on the tables..

Thank you
Ray Johnson

Joined: Dec 14, 2007
Posts: 13
can think a simple solution to the problem
- use order by in braches by date (in hbm file) and pick the last one from collection wherever needed.

otherwise, write a getLastBranch in Tree class which loops through your branches to find the last branch and return. - These is a very light weight solution.

afik, for one to one you must find a unique identifier in the child table as well that differentiates the unique rows involved (in your case last created branches). I hope date field or id field might not help in your case. Hope this helps.

"It's been so long since I've done a good thing--the right thing" : Tears Of Sun
I agree. Here's the link:
subject: @OneToMany/ManyToOne and @OneToOne relationship between same entities.
It's not a secret anymore!