Importance Of Wrapper And Primitive Types In Hibernate

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.xmlhibernate.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:

1234567891011121314151617181920212223242526272829303132333435package 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

1234567891011121314<?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

1234567891011121314151617181920<?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:

123456789101112131415161718192021222324252627282930package 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:

    12345678910111213141516171819202122232425262728package 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:

    123456789101112131415161718192021222324252627282930package 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.

Related Articles

post a comment