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:

package str;

public class Product{

	private int productId;
	private String proName;
	private double price;

	public void setProductId(int productId)
	{
	    this.productId = productId;
	}
	public int getProductId()
	{
	    return productId;
	}

	public void setProName(String proName)
	{
	    this.proName = proName;
	}
	public String getProName()
	{
	    return proName;
	}

	public void setPrice(double price)
	{
	    this.price = price;
	}
	public double getPrice()
	{
	    return price;
	}
}

Product.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="str.Product" table="products">

<id name="productId" column="pid"  />
<property name="proName" column="pname" length="10"/>
<property name="price"/>

</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<mapping resource="Product.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

ClientProgram.java:

package str;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class ClientForSave { 

    public static void main(String[] args)
    {

        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml"); 

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
        Product p=new Product(); 

        p.setProductId(105);
        p.setProName("watch");
        //p.setPrice(35000);       see am not setting any value to Price

        Transaction tx = session.beginTransaction();
        session.save(p);
        System.out.println("Object saved successfully.....!!");
        tx.commit();
        session.close();
        factory.close();
    }

}

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:

package str;

public class Product{

	 Integer productId;
	 String proName;
	 Integer price;

	public Integer getProductId() {
		return productId;
	}
	public void setProductId(Integer productId) {
		this.productId = productId;
	}
	public String getProName() {
		return proName;
	}
	public void setProName(String proName) {
		this.proName = proName;
	}
	public Integer getPrice() {
		return price;
	}
	public void setPrice(Integer price) {
		this.price = price;
	}

}

ClientProgram.java:

package str;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class ClientForSave { 

    public static void main(String[] args)
    {

        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml"); 

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
        Product p=new Product(); 

        p.setProductId(106);
        p.setProName("watch");
        //p.setPrice(35000);       see am not setting any value to Price

        Transaction tx = session.beginTransaction();
        session.save(p);
        System.out.println("Object saved successfully.....!!");
        tx.commit();
        session.close();
        factory.close();
    }

}

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 the Author .::

Java4s_Author
- Java/J2EE Full Stack 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.

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.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
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.