Get all updates of Java4s
Follow Us: Email Newsletter

Example On Composite Primary Keys In Hibernate

Hibernate » On Jun 24, 2011 { 35 Comments } By Sivateja

Composite primary keys means having more than one primary key, let us see few points on this concept

  • If the table has a primary key then in the hibernate mapping file we need to configure that column by using <id /> element right..!
  • Even though the database table doesn’t have any primary key, we must configure one column as id (one primary key is must)
  • If the database table has more than one column as primary key then we call it as composite primary key, so if the table has multiple primary key columns , in order to configure these primary key columns in the hibernate mapping file we need to use one new element called <composite-id …..> </composite-id>


Example On this__

Files required….

  • (Pojo)
  • (for our logic)
  • hibernate.cfg.xml
  • Product.hbm.xml





Eclipse output

In the database


  • see pojo class, in line number 3 i have implemented the interface,  this is the first time am writing this implementation for the pojo class right…!  we will see the reason why we use this serializable interface later.
  • But remember, if we want to use the composite primary keys we must implement our pojo class with Serializable interface
  • hibernate.cfg.xml is normal as previous programs, something like hello world program
  • come to Product.hbm.xml, see line number 9-12, this time we are using one new element<composite-id>
  • Actually if we have a single primary key, we need to use <id> element, but this time we have multiple primary keys, so we need to use this new element <composite-id>
  • Actually we will see the exact concept of this composite primary keys in the next example (loading an object with composite key)

You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


35 Responses to “Example On Composite Primary Keys In Hibernate”
  1. Sweet site, super pattern, really clean and utilize pleasant.

  2. says:

    Thank you Henry..!!

  3. Srikanth says:

    java4s ppl rock..!! gonna refer everyone interested in the technology..

  4. Java4s says:


    Thanks much friend :-)

  5. venkat says:

    clear and fantastic explanation, excellent site for beginners.. thank u very much for help us this information..

  6. Java4s says:


    You welcome my friend, am really happy to see such great feedback from you, feel free to share our blog with your friends, thanks.

  7. vishal says:

    can you please tell me which annotation we use for composite key in hibernate.if you give me an annotated class with composite key then is much better.

    Thanks & Regards
    Vishal Singh

  8. kiran says:

    Hi Java4s,

    Its a good tutorial for learning required technologies with very good and short examples.. i am on my journey to complete the Hibernate i read and got good knowledge up to this article (Composite-Primary Key). i’d say at last what Java4s is… when i finished this hibernate…

    Thanks & Regards

  9. vishal says:

    what if one of the field in composite key is foreign key…?

  10. Raghu says:

    Thanks for your grateful Informations. Here some Basic Hibernate Tutorial

  11. Good example. But there other ways of implementing composite primary key in hibernate. I mean you can use separate class class as composite primary key and there is also xml syntax for creating composite primary key that have its one of field having many-to-one association. Can you please throw light on overriding compareTo and equals method for primary key class.

  12. suresh says:

    thanks for ur good article.
    why we are implementing Serializable interface here. Serializable interface is a marker interface right

  13. why serializable interface is implemented in pojo classes of hibernate?

  14. usharani says:

    Nice Website and easy to understandable with out any help.
    Please provide the tutorial for webservices.

  15. olymohamed says:

    Awesome tutorial.. Keep it up

  16. samba siva rao chinta says:

    Hi, siva teja u r tutorial is amazing and it giving wonderful knowledge to many people…………!
    and can i ask a qustion on hibernate;

    What is a surrogate key and when can i use #surrogate in an application?

  17. Jagdish says:

    Crisp and Clear, to the point…Kudos to you :)

  18. John says:

    Hi Siva, I have a question. Can you show the example of select from some table which has left outer join on View by some id that belongs to View’s composite primary key? And preferably with Hibernate-4. Thank you.

  19. Ravi says:

    Give a sample for how to use composite primary key in ManytoOne mapping in hibernate

  20. Mohit says:

    Really awesome site for the beginners. Easy to understand.
    Thank you so much…

  21. Narasimha Reddy says:

    Really awesome tutorial..

    Thank u so much..

  22. Hung Nguyen says:

    hello, java4s team. Would u like to give me a annotation composite primary keys example ? thanks u so much….

  23. Chandrasekhar rao says:

    I want to use Composite id with generate class is it possible give a suggestion

  24. rajnish says:

    thank you very very much

  25. Harish Chandra says:

    Many many thanks for nice tutorial.

    In above example composite Product class should override hashCode() and equals() method or not?

  26. Mukesh says:

    Hey your materiel is just awesome
    it helps much for beginner
    thank you very much

  27. Swarna says:

    Hello siva,
    please explain why we must implement in case of composite id.

    Thanks in advance.

  28. Ravi says:

    why serializable interface is implemented in pojo classes of hibernate?

  29. sid says:

    really best Explanation……yar

  30. Ritz says:

    Why you have created serialVersionUID object in POJO class? What is use of it?

  31. Abhilash says:

    i think their is one problem in it, if we assign both the parameters as primary key. Then only one parameters works and the other wont

  32. priya says:

    Really goood…

  33. aditi says:

    Hi Nice example,
    May I know how to create the same concept using annotations, means how to create the composite-id concept using annotations.

  34. kolanjinathan says:

    Good Example


    can you tell update hql query for composite primary key


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.