alex
alex

Reputation: 705

Hibernate automatic versioning not working (with Spring)

I am trying to use the automatic versioning of Hibernate but when the update method f of the Session is called I do not see the version field in the where clause of the query nor is the version incremented in the database. I am doing something fundamentally wrong probably, but what? Is calling getCurrentSession of sesssionFactory an issue?

I have the following entity class:

package dslibweb.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;

@Entity
@Table(name = "dsXCT_Recalls")
public class DsXCT_Recalls {

@Id
public String recallId;

public int version;
public String status;
    //...... more properties.....


@Version
public int getVersion() {
    return version;
}

public void setVersion(int version) {
    this.version = version;
}

public String getRecallId() {
    return recallId;
}

public void setRecallId(String recallId) {
    this.recallId = recallId;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}


}

My controller:

package recalls.controller;

@Controller
public class RecallsDataController {

@Autowired
RecallsService recallsManager;
@Autowired
AuthenticationService authService;

private static final Logger logger = Logger.getLogger(RecallsDataController.class);
private static final String SAVE_RECALLS = "MODIFY XCT RECALLS";
RecallsGrid recalls;

@RequestMapping(value = "/showRecallsGrid")
@ResponseBody
public RecallsGrid showRecallsGrid( HttpSession session, HttpServletResponse response) {
    recalls = recallsManager.getRecallsDataGrid((String) session.getAttribute("socketToken"), new GridFilters(0, 0, "", "", "", "", ""));
    if (recalls.getError() == null || recalls.getError().equals("")) { // no error
        return recalls;
    } else {
        try {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, recalls.getError());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return recalls;
    }
}

@RequestMapping(value = "/saveRecalls" , method= RequestMethod.POST)
@ResponseBody
public String saveRecalls( HttpSession session, @RequestParam(value="ids[]", required = false) String [] ids, @RequestParam(value="statuses[]", required = false) String [] statuses){
    boolean result = authService.validateUserAction((String) session.getAttribute("socketToken"), SAVE_RECALLS);
    if(result)
        return recallsManager.saveRecalls(ids, statuses, recalls);
    else 
        return "You do not have authority to perform this action.";
}

}

Where I retrieve a collection of DsXCT_Recalls and show them to the user. The collection is stored in the controller. The user then changes status in one or more entities and I call the saveRecalls method of the recallManager which creates a list of only the changed entities (comparing with the collection stored in the controller).

The recallsManager (service layer) is:

package recalls.service.defaultimpl;

@Service("recallManager")
public class HibernateRecallsDataService implements RecallsService {

@Autowired
JsonRpcRequest jsonReq;
@Autowired
JsonRpcSocketWriterReader socketWriterReader;
@Autowired
JsonRpcRequestConstructor reqConstructor;
@Autowired
RecallsDao hibernateRecallsDao;
private static final Logger logger = Logger.getLogger(HibernateRecallsDataService.class);

@Transactional
public RecallsGrid getRecallsDataGrid(String socketToken, GridFilters filters) {
    List<DsXCT_Recalls> recalls = hibernateRecallsDao.findRangeOfRecordsFiltered(filters);
    return new RecallsGrid(recalls);
}

@Transactional()
public String saveRecalls(String[] ids, String[] statuses, RecallsGrid recalls) {

    List<DsXCT_Recalls> recallList = recalls.getRecalls();
    List<DsXCT_Recalls> updatedRecallList = new ArrayList<DsXCT_Recalls>();

    for (int i = 0; i < ids.length; i++) {
        for (DsXCT_Recalls recall : recallList) {
            if (recall.recallId.equals(ids[i])) { // recall is found in the list
                if (!statuses[i].equals(recall.getStatus())) { // status has changed
                    recall.setStatus(statuses[i]);
                    updatedRecallList.add(recall);
                }

            }
        }
    }

    return hibernateRecallsDao.saveAll(updatedRecallList);
}

 }

The saveAll method of my DAO calls one update method of hibernate session by entity changed:

package recalls.dao.hibernate;




@Repository
public class HibernateRecallsDao implements RecallsDao {

@Autowired(required = true)
@Resource(name = "mySessionFactory")
private SessionFactory sessionFactory;

@SuppressWarnings("unchecked")
public List<DsXCT_Recalls> findRangeOfRecordsFiltered(GridFilters filters) {
    return sessionFactory.getCurrentSession().createQuery("from DsXCT_Recalls r WHERE SID = 0 ORDER BY Org, Bank, BIC, SetlDate").list();
}

public String saveAll(List<DsXCT_Recalls> recallList){
    int count = 0;
    for(DsXCT_Recalls recall:recallList){
        sessionFactory.getCurrentSession().update(recall);
        count++;
    }
    return count + " recalls were modified.";
}
}

Upvotes: 1

Views: 778

Answers (1)

alex
alex

Reputation: 705

So apparently the @Version must be above the attribute declaration and not above the getter method.. I am sure I saw this somewhere though. So much time wasted :(

Upvotes: 1

Related Questions