Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementation of association classes and mapping in hibernate

 
Kristofer Hindersson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm a total beginner at hibernate and I'm trying to learn how to use it together with the spring framework. I'm having trouble understanding what to do with association classes. First of all (and this is probably a more general question that has nothing to do with hibernate) how do you implement association classes as pojos/beans in java? What if I have three classes Student, Course and Enrollment in an UML-diagram:



And what if I wanted to implement these as pojos and make everything persist against a database. What would the pojos/beans look like? And what sort of mapping should I use in hibernate?

Thanks!
 
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
Howdy Greenhorn!

When you're learning something new, it's not a bad idea to start off with the basics. I'm afraid you've picked a pretty darned complicated object model to diagram there.

I map a "Student has many Courses" scenario on my website. You should check it out:

Mapping Many to Many Relationships

Here's the erd:



And here's the class diagram. As you see, they look pretty similar:



And as you can see, the code is pretty basic:



But that's too hard a scenario to start with. Check my website for some simple examples of configuring and running some Hibernate code. Start small and work up. That's the best way to learn!

Getting Started with Hibernate

-Cameron McKenzie


 
Kristofer Hindersson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cameron, thanks for the quick reply. I don't really have to go to your site to see that particular example, seeing as I already own a copy of "Hibernate made easy". But isn't that a specific case having to do with join-tables? The Enrollment-class that associates Student and Course contain additional information (the date attribute) and I just don't understand how that attribute would be translated into code.

Neither



nor



seems right...

(Great book btw, I got it this week so I've only read the intro part but I've skipped through it a couple of times and it looks full of useful and practical information.)
 
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
This scenario comes up quite a bit. The desire is for a many to many realation with an extra attribute in the join table. Most attempts I've seen at doing this end up failing. Instead, if you break this scenario down into a one to many relationship on both sides, instead of a single many to many relationship, you end up having more success.

Enjoy the book!

-Cameron McKenzie
 
Kristofer Hindersson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would this work?


(I'm still not sure what kind of code this would yield, an Employee-class with a hashmap perhaps?)
 
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
That is very much the approach I have seen to tackling this particular problem. I'd love to hear input from other ranchers?
 
Babji Reddy
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Splitting the many-to-many into two one-to-many helps in processing the additional attributes.
Supppose if Hibernate is defined on an existing data model where "EmployeeCompetence" has its own primary key like "EmployeeCompetenceId" instead of a combined key (for some reason), the above mentioned split process does not have any impact. right? (just checked one my old code and yes there is no impact)
 
Kristofer Hindersson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Follow up question.

Would this model:


Translate to this Java-code + these hibernate annotations:






?

What I find confusing is having to use Lists in every class to map the associations between classes. And another thing: What if there's already a relational database in place (With tables and constraints.) Do I still have to do the association mapping inside my classes or will hibernate automagically get this information from the db?
 
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
Lists in every class to map the associations between classes.


Well, how else would you manifest a 'many' relationship in Java code? You gotta have that List, or collection, of associated objects, right? Is there something you think would be a better approach?

There are a variety of tools that can do a reverse mapping, when it goes to the database and creates the hibernate mappings for you. These tools would do just that if you had a database in place, with the 'constraints and all.' I'm not sure if any create annotated POJOs. My last involvement with them, they created the POJOs and hibernate XML mapping files. They were pretty effective though.

-Cameron McKenzie
 
Kristofer Hindersson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Wallace McKenzie wrote:
Well, how else would you manifest a 'many' relationship in Java code? You gotta have that List, or collection, of associated objects, right? Is there something you think would be a better approach?


Nah, I was thinking that maybe hibernate would do that part (internally add those collections) for me if I already had a db running. I guess I'm just lazy...

Thanks for the reply.

/Kristofer
 
Kristofer Hindersson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again

I'm mucking about with inheritance now and I was wondering about how to use the @Table annotation in this case.

What if I have Ancestor, Parent and Child and I want to use the single table strategy to map inheritance among these classes. Now what if I wanted the single table thus created to be called "family", how would I go about it? Is it enough to to put @Table(name="family") in the Ancestor-class?



Or must I have @Table(name="family") in every class?

/Kristofer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic