difference between wrapper and primitive types in hibernate


Difference between wrapper and primitive types in hibernate

Let’s understand difference between wrapper and primitive types in hibernate ,  When we will use primitive data type in persistent class and we will not provide any field value at the time of inserting some value then default value of that data type will be saved in database table but when we will use wrapper class like Integer , Double then blank value will inserted in database table.

See by example

  1. Employee.java
  2. hibernate.cfg.xml
  3. Employee.hbm.xml
  4. Run.java

Employee.java

package com.javagf.bean;
public class Employee { 
 private int id;
 private String name;
 private int salary;
 public int getSalary() { 
  return salary; 
 }
 public void setSalary(int salary) { 
  this.salary = salary; 
 }
 public int getId() { 
  return id;
 }
 public void setId(int id) { 
   this.id = id; 
 }
 public String getName() { 
   return name; 
 }
 public void setName(String name) {
   this.name = name;
 }
}

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
 <property name="hbm2ddl.auto">update</property>
 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
 <property name="connection.username">system</property>
 <property name="connection.password">manager</property>
 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 <mapping resource="com/javagf/bean/Employee.hbm.xml" />
 </session-factory>
</hibernate-configuration>

Employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
 <property name="hbm2ddl.auto">update</property>
 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
 <property name="connection.username">system</property>
 <property name="connection.password">manager</property>
 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 <mapping resource="com/javagf/bean/Employee.hbm.xml" />
 </session-factory>
</hibernate-configuration>

Run.java

package com.javagf.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.javagf.bean.Employee;
public class Run {
 public static void main(String[] args) { 
 Configuration cfg = new Configuration();
 cfg.configure("com/javagf/configuration/hibernate.cfg.xml");
 SessionFactory factory = cfg.buildSessionFactory();
 Session session = factory.openSession();
 Transaction t = session.beginTransaction();
 Employee employee = new Employee(); 
 employee.setId(101);
 employee.setName("Vikas"); 
 //We will not save salary value
 //employee.setSalary(10000);
  session.persist(employee);
   t.commit(); session.close();
   System.out.println("Data saved."); }
}

Please check in employee table in oracle database

select * from employee;

Output

101 Vikas 0

Here 101 is id of Employee , Vikas is name of Employee and 0 is salary of Employee.

0 is there in above output in salary column of Employee table because we used primitive data type int we will use Integer wrapper class then salary column of Employee table will be blank.

Employee.java

package com.javagf.bean;
public class Employee { 
 private int id;
 private String name; 
 private Integer salary;
 public Integer getSalary() {
  return salary;
 }
 public void setSalary(Integer salary) {
   this.salary = salary; 
 }
 public int getId() {
  return id; 
}
 public void setId(int id) { 
  this.id = id;
 }
 public String getName() { 
  return name; 
}
 public void setName(String name) {
   this.name = name;
 }
}

Please check in employee table in oracle database

select * from employee;

Output

101 Vikas

Here 101 is id of Employee , Vikas is name of Employee and no data is in salary column of Employee table.

Conclusion

When we will use primitive data type and we will not pass some field value then default value will be saved in database but in case of wrapper class blank value will be inserted in database.

Related Posts

  1. Why hibernate is better than JDBC
  2. Hibernate Architecture
  3. Steps to create hibernate application in eclipse
  4. Hibernate Mapping and Configuration file
  5. Hibernate SQL Dialects List
  6. Hibernate Query Language
  7. Hibernate ID generator
  8. insert data into table using hibernate example
  9. update record database using hibernate
  10. Delete record database using hibernate
  11. hibernate select query example
  12. how to get all record from table in hibernate






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229