Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Adriano Meis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

PS:
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:

mytree.lastBranch.branchName

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
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!