用xls导入数据(报错)

Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.boventech.learning.entity.Unit


public class UnitInitiallizer implements ApplicationListener<ContextRefreshedEvent>{
	
	
	/**
     * 避免刷新root-application容器和project-name容器时执行两次代码的类变量
     */
    private static boolean isStart=false;
    
    @Autowired
    private UnitService unitService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent arg0) {
        if(!isStart){
            isStart=true;
        }else{
            setUnit();
        }
    }

    /**
     * 设置单位
     */
    private void setUnit() {
		initUnit(unitService);
    }
	
	private void initUnit(UnitService unitService){
		int count=unitService.count();
		if(count<1){
			InputStream is=UnitInitiallizer.class.getClassLoader().getResourceAsStream("/unit.xls");
			List<Unit> units=new ExcelParser().parser(is, new ArrayList<String>());
			for(Unit unit:units){
				if("质量监测与评估中心".equals(unit.getName())){
					unit.setIsManageUnit('1');
				}
				unitService.save(unit);
			}
			if(null!=is){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}


}


package com.boventech.learning.parser;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.boventech.learning.entity.Unit;
import com.boventech.learning.entity.UnitType;
import com.google.common.collect.Lists;

public class ExcelParser {

	public List<Unit> parser(InputStream is, List<String> messages) {
		List<Unit> list = Lists.newArrayList();
		HSSFWorkbook workbook = null;
		try {
			workbook = new HSSFWorkbook(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (int k = 0; k < workbook.getNumberOfSheets(); k++) {
			HSSFSheet sheet = workbook.getSheetAt(k);
			if (sheet == null) {
				continue;
			}
			iterateRowsAndAppendParsed(list, sheet, messages);
		}
		return list;
	}

	private void iterateRowsAndAppendParsed(final List<Unit> list,final HSSFSheet sheet, List<String> messages) {
		for (int i = 1; i < sheet.getLastRowNum(); i++) {
			HSSFRow row = sheet.getRow(i);
			if (null == row) {
				break;
			}
			Unit unit=initUnit();
			for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
				HSSFCell hssfCell = row.getCell(cellNum);
				if (hssfCell == null) {
					break;
				}
				if(cellNum==0){
					unit.setName(hssfCell.getStringCellValue());
				}else if(cellNum==1){
					unit.setUnitCode(new DecimalFormat("#").format(hssfCell.getNumericCellValue()));   
				}else{
					if("教学单位".equals(hssfCell.getStringCellValue())){
						unit.setUnitType(UnitType.TEACHING_UNIT);
						System.out.println(123);
					}else if("科研机构".equals(hssfCell.getStringCellValue())){
						unit.setUnitType(UnitType.SCIENTIFIC_RESEARCH_INSTITUTION);
					}else if("党政机关".equals(hssfCell.getStringCellValue())){
						unit.setUnitType(UnitType.PARTY_GOVERNMENT_ORGANIZATIONS);
					}else if("直属单位".equals(hssfCell.getStringCellValue())){
						unit.setUnitType(UnitType.SUBORDINATE_UNIT);
					}else{
						unit.setUnitType(UnitType.SUBSIDIARY_UNIT);
					}
				}
				list.add(unit);
			}
		}
	}
	
	private Unit initUnit(){
		return new Unit();
	}

}

只有一条数据被导入进来了,猜测问题出在用spring这一块上,没多少流量,不好上网查

你可能感兴趣的:(用xls导入数据(报错))