Spring Hibernate ERROR - 无法获取当前线程的事务同步会话

时间:2018-03-19 08:58:13

标签: java spring hibernate spring-mvc

productDaoImpl.java

@Repository

public class ProductDao implements ProductDaoo {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Boolean save(ProductInfo productInfo) {
        Boolean flag = null;
        if(productInfo!=null) {
        Long id = productInfo.getid();
        Product product = null;
        boolean isNew = false;
        if (id != null) {
            product = this.get(id);
        }
        if (product == null) {
            isNew = true;
            product = new Product();
        }
        product.setName(productInfo.getName());
        product.setPrice(productInfo.getPrice());
        product.setBrand(productInfo.getBrand());
        product.setCategory(productInfo.getCategory());

        if (productInfo.getFileData() != null) {
            byte[] image = productInfo.getFileData().getBytes();
            if (image != null && image.length > 0) {
                product.setImage(image);
            }
        }
        if (isNew) {
            this.sessionFactory.getCurrentSession().persist(product);
        }

        this.sessionFactory.getCurrentSession().flush();
        return Boolean.TRUE;
        }else {
            return Boolean.FALSE;
        }
    }
}

ProductService.java

@Transactional

@Service

public class ProductService {


    @Autowired
    private ProductDao productDao;


    public Boolean save(ProductInfo productInfo) {
        return productDao.save(productInfo);
    }
}

AdminController.java

@Controller

@Transactional

@EnableWebMvc

public class AdminController {


    @Autowired
    private ProductDao productDAO;

    @Autowired
    private ProductService productservice;

    @Autowired
    private ProductInfoValidator productInfoValidator;

    @InitBinder
    public void myInitBinder(WebDataBinder dataBinder) {
        Object target = dataBinder.getTarget();
        if (target == null) {
            return;
        }
        System.out.println("Target=" + target);

        if (target.getClass() == ProductInfo.class) {
            dataBinder.setValidator(productInfoValidator);

            dataBinder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
        }
    }


    @RequestMapping(value = { "/createproduct" }, method = RequestMethod.GET)
    public String product(Model model, @RequestParam(value = "id", defaultValue = "") Long id) {

        ProductInfo productInfo = null;

        if (id != null ) {
            productInfo = this.productservice.findProductInfo(id);
        }
        if (productInfo == null) {
            productInfo = new ProductInfo();
            productInfo.setNewProduct(true);
        }
        model.addAttribute("productForm", productInfo);
        return "createproduct";
    }


    @RequestMapping(value = { "/createproduct" }, method = RequestMethod.POST)
    @Transactional
    public String productSave(Model model, 
            @ModelAttribute("productForm") @Validated ProductInfo productInfo, 
            BindingResult result, 
            final RedirectAttributes redirectAttributes) {

        if (result.hasErrors()) {
            return "createproduct";
        }
        try {
            this.productservice.save(productInfo);
        } catch (Exception e) {

            String message = e.getMessage();
            model.addAttribute("message", message);

            return "createproduct";

        }
        return "redirect:/productList";
    }
}

请帮助我,我失败了无法获得当前线程的事务同步会话

0 个答案:

没有答案