Inserting data in hibernate Java

I am reading data from a text file in hibernate, but problem is that last 2 columns(i.e age and salary column) are not inserted into the person table. Intially I set the <property name = hibernate.hbm2ddl.auto>create</property> to create while using ClientPerson1 class as shown below and on retrieving this table using ClientPerson2 as below changed it to update.

//Pojo class.

package com.aamir;

public class Person {

private int id;
private String name;
private String address;
private String education;
private int age;
private int 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;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getEducation() {
    return education;
}
public void setEducation(String education) {
    this.education = education;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public int getSalary() {
    return salary;
}
public void setSalary(int salary) {
    this.salary = salary;
}

public String toString() {

    StringBuffer sb = new StringBuffer();
    sb.append("id = " + id);
    sb.append(", name = " + name);
    sb.append(", address = " + address);
    sb.append(", education = " + education);
    sb.append(", age = " + age);
    sb.append(", salary = " + salary);

    return sb.toString();
}

}

//person.txt

1, aamir, btm, BCA, 24, 30000
2, saqib, jpnagar, MCA, 22, 40000
3, holly, btm, ME, 34, 29090
4, sandy, jpnagar, ME, 20, 8000
5, mathew, btm, BCA, 42, 8000
6, kim, jpnagar, MCA, 38, 80000
7, Amjid, btm, BCA, 22, 30000
8, Landy, jpnagar, ME, 32, 40000
9, Andy, btm, BCA, 46, 80000
10, simon, btm, MCA, 52, 20000

//hibernate.cfg.xml

<!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="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="hibernate.connection.username">system</property>
    <property name="hibernate.connection.password">mandriva</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
     <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="hibernate.show_sql">true</property>
    <mapping resource="com/aamir/person.hbm.xml"/>
</session-factory>
</hibernate-configuration>

//person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

    <hibernate-mapping package="com.aamir">

    <class name="Person" table="tbl_person">

        <id name="id"/>      

        <property name="name"/>

        <property name="address"/>

        <property name = "education"/>

       <properties name="age"/>

       <properties name="salary"/>

     </class>

</hibernate-mapping>

//ClientPerson1.java

package com.aamir;
import org.hibernate.Session;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ClientPerson1 {

public static void main(String[] args) {

     Configuration c1 = new Configuration();
       c1.configure();

     SessionFactory sf = c1.buildSessionFactory();
     Session s1 = sf.openSession();
     FileReader fr = null;
     BufferedReader br = null;

     try {
           fr = new FileReader("person.data");
           br = new BufferedReader(fr);
           Person p1 = null;
           String [] data = null;
           String line = null;
           s1.beginTransaction();

           while((line = br.readLine()) != null) {

                p1 = new Person();
                data = line.split(",");
                p1.setId(Integer.parseInt(data[0].trim()));
                p1.setName(data[1].trim());
                p1.setAddress(data[2].trim());
                p1.setEducation(data[3].trim());
                p1.setAge(Integer.parseInt(data[4].trim()));
                p1.setSalary(Integer.parseInt(data[5].trim()));

                s1.save(p1);

           }

           s1.getTransaction().commit();
     } catch(IOException e) {
         e.printStackTrace();
     }
      finally {

          try {
             if(br != null) {
                  br.close();
                  br = null;
              }
           } catch(IOException e)  {
               e.printStackTrace();
           }

          try {
                 if(fr != null) {
                      fr.close();
                      fr = null;
                  }
               } catch(IOException e)  {
                   e.printStackTrace();
            }

          s1.flush();
          s1.close();
          System.out.println("done.....");
         }

    }
 }

//Now I am retrieving data from this table, but for last two columns (age, salary) zero is returned for every row and when I checked the d-base last two columns were not added.

//ClientPerson2

  package com.aamir;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Criteria;
 import java.util.List;

public class ClientPerson2 {

public static void main(String[] args) {

    Configuration c1 = new Configuration();
         c1.configure();
    SessionFactory sf = c1.buildSessionFactory();
    Session s1 = sf.openSession();

     Criteria ctr = s1.createCriteria(Person.class);
     List<Person> list = ctr.list();

     for(Person p : list) {

         System.out.println(p);
     }

    }
}

Answers


I guess you have a typo in person.hbm.xml:

   <properties name="age"/>

   <properties name="salary"/>

instead of

   <property name="age"/>

   <property name="salary"/>

Need Your Help

How to update a D3 pie chart

d3.js pie-chart

http://jsfiddle.net/z25SU/