Get all updates of Java4s
Follow Us: Email Newsletter

Importance Of Wrapper And Primitive Types In Hibernate

Hibernate » On Jun 14, 2011 { 7 Comments } By Sivateja

Now we are going to see the main advantages of wrapper types over primitives in the hibernates, will see with an example

Files required to execute this program..

  • Product.java (My POJO class)
  • Product.hbm.xml  (Xml mapping file )
  • hibernate.cfg.xml  (Xml configuration file)
  • ClientProgram.java (java file to write our hibernate logic)

Normally Product.hbm.xml, hibernate.cfg.xml are same as first program hibernate hello world program(Saving an object), actually mapping files almost same for all the programs, but i will specify if we need to change these files right..,

First we will see the output if we use the primitives_______

Product.java:

Product.hbm.xml

hibernate.cfg.xml

ClientProgram.java:

Notes:

  • See in the Product.java, line numbers 5,6,7 am using primitives just like previous programs
  • In ClientProgram.java, i have been written setters for productId, proName, but i have not written setter for price
  • But once you execute this program in the database it will saves the price as 0(zero), so misunderstanding of data will happen like watch price is zero :-) [ free of cost hah ]

 

Eclipse Output

 

In the database

We will see the output if we use the Wrapper types_______

Product.java:

ClientProgram.java:

Eclipse output

In the database


Notes:

  • See in this case if we forget to write the setter for the price, in the database its not inserting any thing [ actually it has to insert NULL value, as of now leave it ],  no way of data misunderstanding.
  • But in the first case (primitive types) it inserted zero, see the above screen short

Hope you understand the importance and difference between wrapper types and primitives in hibernates.

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
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.

Comments

7 Responses to “Importance Of Wrapper And Primitive Types In Hibernate”
  1. first of all thank u for giving us such a wonderful learning site. i personally like it very much and it is one of my preferable site.

    but in this topic there is something that i want to mention as per my personal experience is concerned.

    it does not matter weather we write setter or getter method database insert the value “null” for the wrappers and default values for the primitive(ex default value of int is zero) because if we do not assign the wrapper jvm assign wrapper with the “null” value and assign primitives with their default values.
    and while we want to save that object then hibernate fetch the value of the object and store them in to the database.

    if we restrict the hibernate not to insert anything in to the database then it will insert a null value always irrespective of primitive or wraper in our pojo class.
    we can do so by

    hopefully i am write.
    with regard
    bibhuti

  2. Manish says:

    Can you please explain why it did not insert anything (0 or null) using wrapper type but with primitive it inserted 0 ??

  3. Sai says:

    which is the most preferred way to use wrapper type or primitive and why?

  4. Ashok says:

    @bibhuti bhusan pradhan

    You have mentioned that, we can restrict the hibernate not to insert the any data in the data base if it is primitive or not in the pojo.
    Can you please explain me/sample code snipet how you can restrict.

  5. srujan says:

    if we use wrapper classes, then no need to use private in pojo class??

  6. Ashok says:

    Then Sivateja for which one to we have give preference Primitive or Wrapper???

  7. Annapoorneshwari says:

    But here i am getting null as in input in the database table….plz help me out y???

Name*
Mail*
Website



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
Upcoming
  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.