• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate - another way to model One-To-Many relationship with Java classes?

 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a simplified example of what I want to achieve:

I have 3 tables:



1. STUDENT_REF colums have a foreign key referring to STUDENT_ID column.
2. There is a One-To-Many relationship between STUDENT and HOBBY/AWARD.

The standard way to model this in Java classes is:



However, I want to introduce another containing class - StudentDetails - which does not have a mapping to any table and model it thus:




Note that Hobby and Award classes have a reference back to Student and not to StudentDetails.

How can I achieve this? Should I be using embedded objects? I haven't (yet) figured out how. Any hints will be appreciated and devoured.

Thanks in advance
Praveen
 
Vladimir Ozerov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is almost exactly what Embeddables are used for.
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vladimir Ozerov wrote:This is almost exactly what Embeddables are used for.


Not sure Embeddables are a solution, or like I said, I haven't figured it out yet. To quote from http://en.wikibooks.org/wiki/Java_Persistence/Embeddables:

In an application object model some objects are considered independent, and others are considered dependent parts of other objects. In UML a relationship to a dependent object is consider an aggregate or composite association. In a relational database this kind of relationship could be modeled in two ways, the dependent object could have its own table, or its data could be embedded in the independent objects table.

In JPA a relationship where the target object's data is embedded in the source object's table is considered an embedded relationship, and the target object is considered an Embeddable object. Embeddable objects have different requirements and restrictions than Entity objects and are defined by the @Embeddable annotation or <embeddable> element.


The way I understand this is, an Embeddable should have corresponding columns in a Database. In my case, the StudentDetails is merely a containing structure which holds data that would otherwise have been in the Student class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic