These are the tables I want to map using Hibernate:
user (PK UserId)
article (PK ArticleId, FK CreatorUserId)
category (PK CategoryId, FK ParentCategoryId)
I have been looking at the documentation for Hibernate, but I really cannot choose what type of mapping is the most suitable for my tables.
Any help would be greatly appreciated! 🙂
For user-articles relationship you can use either Set or List mapped as bag; List tends to be a bit more convenient to deal with on java side especially if article has an attribute by which you may want to sort the collection (e.g. creation date). Sample mapping is:
The above maps association as bidirectional with Article being responsible for maintaining the relationship (this avoids an unnecessary update during collection changes). Details are here. Replace
<set>with<bag>if you’d rather use List in place of Set; you can then add “order-by” attribute to have your list sorted in the database.For Category you could in theory do the same, however if your category hierarchy is deep and you need to retrieve specific levels / sub-trees rather often, consider using nested set model instead.