We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


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

Medium Priority
Last Modified: 2012-06-21
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.

Watch Question

Java Developer
Top Expert 2010
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.