Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping annotation -> one class per table question

 
Alex Bleasdale
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic