[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

Apply hibernate annotation on Product and Category design in Many to Many association

I am new and studying Hibernate Annotation but unfortunately I have no idea how to startup to design the Product & Category in Java. Hopefully someone can provide guidelines in order for me to proceed.

1) Design Product and Category classes with hibernate annotation so that I can easily add product, add category, and assign product to category on the user interface section.

2) A category has a many to many relationship with a product.

Table schema:
product_id (PK,autonumber)

cat_id (PK, autonumber)

cat_id (PK, FK references Category)
product_id (PK, FK references Product)
display_sequence (int)

So how do I map these associations in JAVA ? Also what are the best syntax to create the composite primary key on the Category_Product table ?

I  know there are few syntaxes in the following, but unsure which are the best to apply for what kind of scenario.
- annotate the component property as @Id and make the component class @Embeddable
- annotate the component property as @EmbeddedId
- annotate the class as @IdClass and annotate each property of the entity involved in the primary key with @Id

Can anyone help me with this. Will really appreciate it.
Thanks a lot in advance.

1 Solution
Well start by creating Product and Category bean classes
I'd suggest not using a composite key for Category_Product, and instead add a generated id to use as the pk

let me know when you have that and we'll go to the next step


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now