• Post Reply Bookmark Topic Watch Topic
  • New Topic

embedded class question

jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!