2014-09-23 65 views
0

我正在讀取hibernate中的文本文件中的數據,但問題在於最後2列(即年齡和薪金列)未插入到人員表中。我在設置<property name = hibernate.hbm2ddl.auto>create</property>create,同時使用ClientPerson1類如下所示,並使用ClientPerson2檢索此表,將其更改爲update在hibernate中插入數據Java

// 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....."); 
     } 

    } 
} 

//現在,我從該表中檢索數據,但對於最後兩列(年齡,工資)返回零的每一行,當我檢查了d-去年基數兩列不添加。

// 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); 
    } 

    } 
} 

回答

1

我猜你在person.hbm.xml一個錯字:

 
    <properties name="age"/> 

    <properties name="salary"/> 

代替

 
    <property name="age"/> 

    <property name="salary"/> 
+0

謝謝,我太stupid.It工作,再次感謝。 – Aamir 2014-09-23 19:34:58

相關問題