Issue
I import the line from xlsx just the last line saved in a database!
here the result after I run the code the first line is an insert and the second and third line update!
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into libraries (account_name, area, branch, currency, deported, employee, invoice_number, monetary, month, movement_type, metal, profit_local_currency, profit_ratio, total, total_local_currency, total_metal, libraries_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update libraries set account_name=?, area=?, branch=?, currency=?, deported=?, employee=?, invoice_number=?, monetary=?, month=?, movement_type=?, metal=?, profit_local_currency=?, profit_ratio=?, total=?, total_local_currency=?, total_metal=? where libraries_id=?
Hibernate: update libraries set account_name=?, area=?, branch=?, currency=?, deported=?, employee=?, invoice_number=?, monetary=?, month=?, movement_type=?, metal=?, profit_local_currency=?, profit_ratio=?, total=?, total_local_currency=?, total_metal=? where libraries_id=?
this is my code to import from xlsx:
public void button(ActionEvent actionEvent) {
String s = "";
try {
File file = new File("C:\\Users\\yosef\\Desktop\\test.xlsx");
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> itr = sheet.iterator();
while (itr.hasNext()) {
Row row = itr.next();
Iterator<Cell> cellIterator = row.cellIterator();
int count = 0;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
s = cell.getStringCellValue() + "";
if (count == 0) {
lib.setMonth(s);
} else if (count == 2) {
lib.setMonetary(null);
} else if (count == 3) {
lib.setDeported(s);
} else if (count == 4) {
lib.setMovementType(s);
} else if (count == 5) {
lib.setArea(s);
} else if (count == 6) {
lib.setAccountName(s);
} else if (count == 7) {
lib.setCurrency(s);
} else if (count == 9) {
lib.setMtal(s);
} else if (count == 15) {
lib.setEmployee(null);
lib.setBranch(s);
count = 0;
}
break;
case Cell.CELL_TYPE_NUMERIC:
s = cell.getNumericCellValue() + "";
if (count == 1) {
lib.setInvoiceNumber(Double.parseDouble(s));
} else if (count == 8) {
lib.setTotal(s);
} else if (count == 10) {
lib.setTotalMetal(s);
} else if (count == 11) {
lib.setTotalLocalCurrency(Double.parseDouble(s));
} else if (count == 12) {
lib.setProfitLocalCurrency(s);
} else if (count == 13) {
lib.setProfitRatio(Double.parseDouble(s));
}
break;
default:
}
if (count != 15)
count++;
}
if (!ConnectionUtil.getEntityManager().getTransaction().isActive()) {
ConnectionUtil.getEntityManager().getTransaction().begin();
}
ConnectionUtil.getEntityManager().persist(lib);
ConnectionUtil.getEntityManager().getTransaction().commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Solution
I should but this line
libraries lib = new libraries();
under this line
switch (cell.getCellType()) {
because I use one object for all rows, and when I create one object for one row, I solved this problem.
Answered By - Yousef R Hammad
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)