File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes embedded class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "embedded class question" Watch "embedded class question" New topic

embedded class question

jim li
Ranch Hand

Joined: May 20, 2008
Posts: 180
hi guys

could you please give me some hints on what is embedded class ?

i do not quite understand the use of the embedded in the hibernate annotation!

thank you
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Well, when we're doing Java class to database table mappings, we always talk about one table to one database table. That's the ideal. But sometimes, you have two classes that must map to ONE database table. But we only mark one class as the entity to be associated with the table. The second class, we say is embedded in the first class, and as such, the embedded class will map to the same database table as the embedded class.

On my website, I use an example where a Thing and a ThingDetail class, two separate classes in a Java domain model must map to a single database table, with the database table being named thing.

Mapping Multiple Java Classes to a Single Database Table with Hibernate and JPA Annotations

So, in this situation, I made the Thing the entity class that maps to the thing table.

But we also want the ThingDetail class to map to the same table, so we mark the ThingDetail class as being embeddable, namely, embeddable in the Thing class:

This is a simple example. In more complicated scenarios, compound primary keys are defined in a separate class, and those compound primary key classes become embeddable in the class for which they will act as a primary key. That's where you often see the embeddable annotation being used - in compound primary key classes.

Here's a little tutorial on compound primary keys, if you find yourself needing to use the Embeddable annotation in this type of scenario:

Tutorial on Using Hibernate and JPA Annotations to define Compound Primary Key Classes

-Cameron McKenzie
[ June 08, 2008: Message edited by: Cameron Wallace McKenzie ]
I agree. Here's the link:
subject: embedded class question
It's not a secret anymore!