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

Help with a Mapping

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on refactoring a database and I could use a little help with one of the mappings - I'm just finding it a little confusing.

I have a "Case" table which contains an identify column for "caseNumber".

Associated to that table is a "Review" table, which has three columns:

1. ReviewID (Unique ID for the row)
2. CaseNumber (Foreign Key into the Case Table)
3. ReviewType (Discriminator)

Associated with the "Review" table are three tables that represent different types of reviews that can take place for each case - for simplicity sake, we'll just call those tables "Foo", "Bar", and "Baz". Those three tables are associated with the Review table in one-to-one relationships.

When looking at the documentation for Hibernate, I see that using a subclass tag allows me to use a "discriminator-value" attribute to determine what data type I have for a given row. That seems like it would work very well. However, in the documentation, it appears as if that is really for cases in which all of your subclasses exist within a single table. In my case, I've separated those subclasses into their own tables (Foo, Bar, and Baz) because they're really quite different and there are a lot of columns in each - combining them would have been a mess. The approach for subclasses in their own tables seems to be to use joined-subclass tags, but those don't allow for a discriminator.

So I'm a bit stuck. Can anyone shed a little light on how to map this out? I hope I described it well enough for people to get the idea. Perhaps a picture will help:

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, your inheritance strategy is not single table, or "Table per class heirarchy" which is what the discriminator value is used for.

here are the three inheritance strategies.

http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#inheritance-strategies

If the data model is try third normal form, then you want to use the table per concrete class strategy.

If you only have the three tables, one for each Subclass type of review then you want to use the table per subclass strategy. And the only real difference is the tag that you use in the mapping <joined-subclass> or <union-subclass>

And the easy way to know which one is the type of query that would need to be run to get one set of all the records from all the tables.

So if there is no "parent" table for "review" then you have a table for each subclass, so the query in the backend that needs to run will have Union of queries. One query for each table all unioned together.

If it is third normal form, then the tables in the query will all be in the from of a single query with joins.

Hope that helps

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also look at


section 9.1.3. Table per subclass, using a discriminator

In that link I put in my previous post if you need a discriminator column too. for a joined subclass.

Mark
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, Mark. I'm working on getting this set up using the joined-subclass approach (I don't think I actually need the discriminator, after all) but I've run into another question.

In my "Case" mapping, I'm going to have something like this, correct?



And in my CaseReview mapping, I'll have this:



If I'm understanding this properly, then my domain object for "Case" will have a single Set containing CaseReview objects. My problem is that, should I want to get all of the reviews for a particular case of type "Foo", I now need to iterate through that list looking for objects of type "FooReview". Seems like there should be a better way to handle this.

So is there a way I can modify this so that each review type is placed into its own set such that my domain object for "Case" would have properties like "fooReviews", "barReviews", and "bazReviews"?

Thanks.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is a way. But now you are going to make me have to break out my Java Persistence with Hibernate book to find all the xml mapping stuff. I never memorize the xml mapping because I copy and paste all the time. I also tend to use Annotations, and haven't needed to Annotate a class hierarchy before. ;)

Let me do some research and I will have an answer by this afternoon.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic