This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Mapping annotation -> one class per table question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Mapping annotation -> one class per table question" Watch "Mapping annotation -> one class per table question" New topic
Author

Mapping annotation -> one class per table question

Alex Bleasdale
Greenhorn

Joined: Jun 27, 2009
Posts: 2
I'm hoping someone can help -

I'm getting Hibernate to generate my tables for me (using new SchemaExport(config).create(true, true);).

I have about 4 beans (annotated with @Entity) right now - each with an @Id @GeneratedValue id - and Hibernate is creating tables for each of them, which is exactly what I want.

I'm running into issues when I try to persist one of the beans however:

I've created a bean called "Person" which contains an "Address", "Location" and "ContactDetails" (which are separate beans):

e.g.
public class Person {

private Address address;
private ContactDetails contactDetails;
private Location location;
(...)
}

If I just run it as is (with no extra annotations), I get this error:
Could not determine type for: com.example.beans.Address, at table: Person, for columns: [org.hibernate.mapping.Column(address)]

So I've added the @Embedded attribute above the getters for those external entities, like so:

@Embedded
public Address getAddress() { [...] }

- at least this way hibernate is able to write everything out to the database. However, using @Embeddded means that hibernate "flattens" the bean structure when it creates the table (so the "Person" table contains every field for each of the Entities).

Is there a way to tell hibernate to create joins, so it can mirror the bean structure when it writes out the fields? I guess this must be quite a popular requirement but so far haven't managed to find a way to do this.

Any help / advice appreciated - thanks in advice.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

No, you don't want to embed. You have one to many and many to many relationships, and you'll want to map those. It's a bit of work, but not too much.

I actually put together a little tutorial on an advance mapping that is very similar to your problem domain:



As you can see, I have the many addresses and the one to one detail. There's also a many to many in there with Skills for good measure.

And this is the table mapping that gets created, which is probably what you want - multiple talbes with relational joins:



The completely annotated Client class, which is the center of this whole things ends up getting mapped like this:



From my Tutorial on Mapping Table Realtionships with Hibernate and JPA Annotations

-Cameron McKenzie
Alex Bleasdale
Greenhorn

Joined: Jun 27, 2009
Posts: 2
Thanks so much for that info, Cameron - exactly what I needed!

In case anyone else is interested, this annotation:

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "id")
public Address getAddress() { [...] }

Was what was needed - thanks again for your prompt reply; much appreciated!
A

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Yeah, well, it was very fortuitous that my example was fairly similar to your problem domain.

Note that the example I gave above only shows the central, Client class. There are mappings on the other ends of the relationships that point back to the client, which you may end up needing as well.

It's always great to be able to help!

-Cameron McKenzie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping annotation -> one class per table question