Get all updates of Java4s

Composite Primary Key In Hibernate With Select Query

Composite primary keys means having more than one primary key right..?

Example On this__

Files required….

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

All files are same like previous program, but is the new file for loading an object from the database





Eclipse output


    • regarding actually for loading an object we have to pass the primary key column value like…

    • Actually that is the case with single primary key, but see in this example we are using multiple primary keys (2 primary key values), so while loading an object how we will give two values….?

  • :-) which is absolutely wrong……………!!!!!!!!!
  • so how we can pass the multiple primary key values for loading that object.. ?
      • First we need to set the values just like in line numbers 19,20 , ensure you are setting the values which already have in the database for that particular object.
      • Then we need to pass that object as the parameter to load the object.

      • What am saying is that the second parameter of the get method is always a Serializable object, so in the line number 3 i just implemented with
      • Remember, if we have single primary key we used to give

    here new Integer(101) is the wrapper, all wrappers are Serializable by default.

You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


23 Responses to “Composite Primary Key In Hibernate With Select Query”
  1. Srinivas.G says:

    Excellent explanation….thanks

  2. Java4s says:


    You bet, thanks for your feedback :-)

  3. Amarnath says:

    Thanks for the nice expalnation with clarity.

  4. Hai, this is venkatarao , you r really god to me and all java developers. , i love your website( . really thanqs . sir please provide webservicess meterial

  5. Kanna says:

    Very nice explanation.
    I stuck up with these in some interviews

  6. ramu says:

    Hi Thank you for clear explanation. Actually i have one doubt here,If we have only one single primary key in database table and can we use get method by making the pojo as serializable?

  7. basha says:


    Your explanation is very clear,My doubt is without using save() method how could you get data from database by using get() method.
    –>what is difference between get() & load().

  8. really excellent explanation regarding serializable object.

    thnx a lot..

  9. Swamy Nadhan says:


    This tutorial was very helpfull…….


  10. charan says:

    hello boss ur teaching style is very good…can u provide jquery and ajax clealy

  11. Chandan Prasad says:

    How to get more than one row data from database.
    currently i have got “org.hibernate.HibernateException : More than one row with the given identifier was found: str.Product@b59fd9, for class: str.Product”
    when ever i get more than one row of data.


  12. Aamir says:

    Hi Sivateja how r u, I just have one question, what if we have a reference in composite key class, then how can we do mapping in composite-id tag for it.


    class Address {
    private String city;
    private String state;

    //getters and setters.

    class CompKey implemenst Serializable {

    private String firstName;
    private String email;
    private Address address;

    now, how to use address ref. variable in composite-id tag???

  13. Ashok says:

    Why we write Product p1=(Product)o;……..without directly print object o.ProName();

    I want to Know what type of data is there in object o and P1…….please tell me.

    thanks a lot.

  14. madhu says:

    really,very nice explanation.

  15. Raju Kapadne says:

    Hello Sir ,

    Really is extremely good and easy to learn the tutorials . . Thanks a lot sir !!

  16. BoopathyCovai says:

    Hi Java4s,
    your website is awesome for new comers.
    I am having one doubt, is this only for multiple primary key ?
    suppose if i want to pass 3 or 4 parameters(those or not primarykey).
    what will do for this ?

  17. sudheer k says:

    Excellent website for java learners.. But, one small doubt, why Product class must be serialize ? and without save object how can we get that Object from DB using session.get() method.. PLz let me know..

  18. aashish soni says:

    why composite id class must implement serializable??
    Then why single-id class doesn’t need to be Serializable? Could you elaborate on that?

  19. Rohit says:

    Excellent explanation…thanks :)

  20. usha says:

    Good Explanation and thank you. could you please give some example on updating record of a table having composite keys(of varChar type(String)).

  21. Tirupathi says:

    Here i am getting NullPointerException when i am retring the data from DB. can you plz explain

    Exception in thread “main” java.lang.NullPointerException
    at str.ForOurLogic4Load.main(

  22. prasanna says:

    iam also getting the Exception is

    Exception in thread “main” java.lang.NullPointerException
    at str.ForOurLogic4Load.main(

    can u pls help me.

  23. Deepa says:

    @Prasanna and @Tirupathi
    You have update the code in file at line number 20, to p.setProName("iphone") .
    As product id and product name are composite primary keys, so we need to pass both the values in order to get price.


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Spring-Boot-tutorials Hibernate tutorials Spring tutorials Struts tutorials Servlets tutorials Ajax tutorials Log4j tutorials jQuery tutorials Json tutorials Core Java tutorials Web Services tutorials AngularJs tutorials
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Contact | About Us | Privacy Policy | Advertise With Us

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