I do not understand why only one row is deleted by the below code. The same happens for inserting elements. Can anyone tell me why can't I use the same route reference over & over again?

//Begin transaction
        Long id=1;
        Route route = new Route();
        RouteDAO routeDAO = new RouteDAOImpl();
        route.setRouteId(id);
        routeDAO.delete(route);
        route.setRouteId(++id);
        System.out.println(route.getRouteId());
        routeDAO.delete(route);
        route.setRouteId(++id);
        routeDAO.delete(route);
        route.setRouteId(++id);
        routeDAO.delete(route);
//Commit Transaction

Thanks.

有帮助吗?

解决方案

You expect that a distinct row/route is focused when you change the id but it's not.
Any mutation on a persisted object causes its eligibility to update.

You always use the same route reference, although you're mutating it by changing its id, causing an update each time after commit is made.
In reality, Hibernate makes an optimization after commit, to avoid those useless updates, since you precised a delete.

Those updates concerns the same row, explaining why after commit, just one delete occurs.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top