We added categories to our NixMash Blogging app on GitHub. Our primary approach to taxonomy is Tagging, but since we are importing categories from WordPress in the WordPress Migrator we wanted to accommodate Categories in the new app as well. But unlike WordPress which supports multiple categories per post, we’re imposing the rule of ONE category per post.
So we’re importing WordPress Categories to our new schema and as a result have this lookup table, post_category_ids.
We want to create a @OneToOne relationship from Posts to Categories through our lookup table. A quick look at our categories table before getting started. We won’t show the posts table containing our post_id key.
We next construct our @OneToOne from our Post Entity to its corresponding post_id in post_category_ids.
We may have only one Category for each post, but in Hibernate Terms we have a @OneToMany on the Categories side of the relationship. Here is how that is constructed.
And that’s a @OneToOne with a Lookup Table in Hibernate!
Here’s a bonus screenpic of the NixMash Blog Posts list. The Category precedes the tags at right and is in the darker color.
Source Code Notes for this Post
Source code discussed in this post can be found in my NixMash Blog project located on GitHub.