只有最后一行插入表中的休眠

时间:2018-05-23 05:22:58

标签: java mysql hibernate

我正在使用hibernate开发一个java项目。我有一个包含200多个数据的csv文件。我已成功检索csv文件中的数据。现在我必须将这些数据插入表中。

问题是只有最后一行被添加到表中。其他行未插入。

表的架构如下:

INSERT INTO `attendence_table`
(`serial_no` int auto-increment, 
`employee_id` varchar2, 
`in_time` varchar2, 
`out_time` varchar2,
 `attend_date` date)

Attendence类如下:

   @Entity
   @Table(name = "attendence_table")
   public class Attendence {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "serial_no")
    private int id;
    @Column(name = "employee_id")
    private String employee_id;
    @Column(name = "in_time")
    private String inTime;
    @Column(name = "out_time")
    private String outTime;
    @Column(name = "attend_date")
    private String date;



    public String getEmployee_id() {
        return employee_id;
    }

    public void setEmployee_id(String employee_id) {
        this.employee_id = employee_id;
    }

    public String getInTime() {
        return inTime;
    }

    public void setInTime(String inTime) {
        this.inTime = inTime;
    }

    public String getOutTime() {
        return outTime;
    }

    public void setOutTime(String outTime) {
        this.outTime = outTime;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

   }

插入功能如下:

private static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

public static void hibernateInsertAttendenceSession(List<Attendence> collection) {


    Session session = sessionFactory.openSession();

    session.beginTransaction();

    for (Attendence obj : collection) {
        session.save(obj);
        System.out.println("Object Added");

    }

    session.getTransaction().commit();
    session.close();
}

为了您的方便,我还添加了csv文件的一瞥:

Test_company,TestId001,Test Name,2018/03/22,08:53:15,17:50:40
Test_company,TestId001,Test Name,2018/03/25,08:51:02,17:55:18
Test_company,TestId001,Test Name,2018/03/27,08:50:16,18:03:47
Test_company,TestId001,Test Name,2018/03/28,08:48:07,18:46:42
Test_company,TestId001,Test Name,2018/03/29,08:56:16,20:14:16

提前感谢您给我宝贵的时间来帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

您正在保存Attendence对象的引用,而您每次都在修改它的内容。

每次尝试保存时,您都应该实例化一个Attendence对象。

for (Attendence obj : collection) {
    Attendence newRef = new Attendence(obj);
    session.save(newRef);
    System.out.println("Object Added");

}

答案 1 :(得分:0)

抱歉,我的问题出在不同的地方。感谢大家的帮助。从csv文件中检索数据时,发生了一个小错误。谢谢大家的时间:))

在readfromcsv函数中,我之前执行了以下操作:

public static void readFromExcel(String path) {

    ArrayList<Attendence> attendences = new ArrayList<Attendence>();
    String csvFile = path;
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    Attendence attendenceLine=new Attendence();
    try {
        br = new BufferedReader(new FileReader(csvFile));
        //Attendence attendenceLine = new Attendence();
        line = br.readLine();
        while ((line = br.readLine()) != null) {
            String[] data = line.split(cvsSplitBy);

            if (data.length == 6) {

                attendenceLine.setEmployee_id(data[1]);
                attendenceLine.setDate(data[3]);
                attendenceLine.setInTime(data[4]);
                attendenceLine.setOutTime(data[5]);

            }
            else{
                attendenceLine.setEmployee_id(data[1]);
                attendenceLine.setDate(data[3]);
                attendenceLine.setInTime("no punch");
                attendenceLine.setOutTime("no punch");
            }
            attendences.add(attendenceLine);
        }
        for(Attendence attendence: attendences){
            HibernateOperation.hibernateInsertOneAttendenceSession(attendence);
        }
        //HibernateOperation.hibernateInsertAttendenceSession(attendences);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
    }
}

此处,attendenceLine String Variable仅将最后一行作为参考值。这就是每次迭代的原因,我需要再次创建对象。我做了以下事情来解决这个问题。

public static void readFromExcel(String path) {

    ArrayList<Attendence> attendences = new ArrayList<Attendence>();
    String csvFile = path;
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {
        br = new BufferedReader(new FileReader(csvFile));
        //Attendence attendenceLine = new Attendence();
        line = br.readLine();
        while ((line = br.readLine()) != null) {
            String[] data = line.split(cvsSplitBy);
            Attendence attendenceLine=new Attendence();
            if (data.length == 6) {

                attendenceLine.setEmployee_id(data[1]);
                attendenceLine.setDate(data[3]);
                attendenceLine.setInTime(data[4]);
                attendenceLine.setOutTime(data[5]);

            }
            else{
                attendenceLine.setEmployee_id(data[1]);
                attendenceLine.setDate(data[3]);
                attendenceLine.setInTime("no punch");
                attendenceLine.setOutTime("no punch");
            }
            attendences.add(attendenceLine);
        }
        for(Attendence attendence: attendences){
            HibernateOperation.hibernateInsertOneAttendenceSession(attendence);
        }
        //HibernateOperation.hibernateInsertAttendenceSession(attendences);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
    }
}