Results 1 to 2 of 2

Thread: Code not saving changes to database. (very frustrating)

  1. #1
    Join Date
    Jul 2012
    Location
    NYC
    Posts
    1

    Default Code not saving changes to database. (very frustrating)

    Code:
    	@SuppressWarnings("unchecked")
    	//@Override
    	public SerialNumber getCounter(String id) {
    		Session cs = sessionFactory.getCurrentSession();     
    		SerialNumber sn = (SerialNumber) cs.get(SerialNumber.class, id);
    		if (sn == null) {
    			//TODO Throw an error (maybe). The code below creates a new Serial number counter
    			log.debug("NO Serial Number for: " + id + " was found.");
    			sn = new SerialNumber();
    			sn.setSerialNumberId(id);
    			sn.setName("Unspecified");
    			sn.setValue(0);
    		}
    		sn.setValue(sn.getValue()+1);
    		cs.saveOrUpdate(sn);
    		cs.close();
    		return sn;
    	}
    This line seems to be ignored:

    Code:
    		cs.saveOrUpdate(sn);
    After this line, I would assume the database is updated but this is not the case. What am I doing wrong?

    Thanks.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Please use the search as this question has been answered numerous times before.

    To insert/update/delete data in the database you need transactions, without a transaction your insert/update/delete statement is useless. Also don't mess around with the session yourself, don't close it let spring handle that for you (that is why you want to use springs transaction management).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •