Results 1 to 5 of 5

Thread: Grabbing next id before saving to the db?

  1. #1

    Default Grabbing next id before saving to the db?

    I'd like to be able to grab the next primary key id (or maybe the last id used + 1) from a table so I can set an image name that corresponds with that id (and subsequent row information). Is there something that HibernateDaoSupport provides to do this?
    PJ

  2. #2

    Default

    This is the solution I've come up with so far. It works, but I'm sure it could be better. I'm doing this because I've been told to store the images in the file system and not the database. I think that storing the pictures by their picture id should make displaying them trivial and eliminate any funky naming problems. Of course I could be wrong, so I look forward to any comments.

    Code:
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
    
            //all of this just to grab the image extension
            String name = file.getOriginalFilename();
            int extIndex;
            for&#40;extIndex=0;extIndex<name.length&#40;&#41;;extIndex++&#41;&#123;
            	if&#40;name.charAt&#40;extIndex&#41;=='.'&#41;&#123;
            		break;
            	&#125;
            &#125;
            String imageExt = name.substring&#40;extIndex&#41;;
            
            synchronized&#40;this&#41;&#123;
    	        //make dummy picture
    	        Picture blankPicture = new Picture&#40;&#41;;
    	        blankPicture.setLocation&#40;"notset"&#41;;
    	        pictureManager.savePicture&#40;blankPicture&#41;;
    	        
    	        //grab dummy picture and rename image
    	        Picture picture = pictureManager.getPictureByLocation&#40;"notset"&#41;;
    	        String picId = String.valueOf&#40;picture.getId&#40;&#41;&#41;;       
    	        String uploadDir = getServletContext&#40;&#41;.getRealPath&#40;"/uploads"&#41; + "/" + picId + imageExt;
    	        
    	        //save real picture
    	        picture.setId&#40;picture.getId&#40;&#41;&#41;;
    	        picture.setLocation&#40;uploadDir&#41;;
    	        pictureManager.savePicture&#40;picture&#41;;
    	        
    	        File dirPath = new File&#40;uploadDir&#41;;
    	        InputStream stream = file.getInputStream&#40;&#41;;
    	        OutputStream bos = new FileOutputStream&#40;uploadDir&#41;;
    	        int bytesRead = 0;
    	        byte&#91;&#93; buffer = new byte&#91;8192&#93;;
    	
    	        while &#40;&#40;bytesRead = stream.read&#40;buffer, 0, 8192&#41;&#41; != -1&#41; &#123;
    	            bos.write&#40;buffer, 0, bytesRead&#41;;
    	        &#125;
    	        bos.close&#40;&#41;;
    	        stream.close&#40;&#41;;
            &#125;
    PJ

  3. #3
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    In HB it depends a lot on the type of strategy you use for your id fields
    when mapping each POJO. See file:///c:/work/study/hibernate-2.1.7/doc/reference/en/html_single/index.html#mapping-declaration-id

    AFAIK HibernateDaoSupport doesn't support such a feature. In your case you can either work with an 'identity' id - you can keep the counter in a helper class and increment it yourself - however you have to make sure that no one else writes inside the table or your counter will be wrong.

    Another alternative would be to use a HB interceptor to write again on PostFlush or (PreFlush) but I don't know if that will work as there are some restricition (like not being able to modify the information that is going to be written).

    Ofc, you can always work with assign keys or write the information twice - first to get the id, second time to update the name field.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Don't forget to take this problem with the HB forums (do a search first). For sure you'll find there some good solutions to this problem.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    PJ,
    Let's refactor this code a little bit; the picture path is : getServletContext().getRealPath("/uploads") + id + ext:

    1. getServletContext().getRealPath("/uploads"):
    First of all, getServletContext().getRealPath("/uploads") is quite constant. So you can take it out, and inject it using Spring IoC. If this value, really, really does not change, there should be no need to save it any more in the database.

    2. id:
    id is, I guess, the primary key for the images table. So let just take it out from the image path column.

    3. ext:
    well, we have to keep this data, and perhaps just rename the table column to extention.

    Solution:
    now we can solve the initial "problem" as follows:
    1. save the picture row into the database and get the id returned by hibernate
    2. build the picture path: constant injected by Spring + id + extension
    3. save the uploaded file to the disk

    transactions you can refactor your code a bit to wrap 1., 2. and 3. in the same transaction. That way, if 3. fails, 1. will be rolled back.
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Saving Parent / Child Object Graph
    By matthewramella in forum Data
    Replies: 3
    Last Post: Nov 28th, 2007, 08:33 AM
  2. UpgradeAcegi Security System from 0.6.1 to 0.8.3
    By mannobug in forum Security
    Replies: 3
    Last Post: Sep 23rd, 2005, 07:00 PM
  3. saving LastLogin date
    By tsandor in forum Security
    Replies: 5
    Last Post: Aug 23rd, 2005, 07:50 PM
  4. Replies: 2
    Last Post: Jul 14th, 2005, 10:50 AM
  5. saving data via hibernate
    By tuor in forum Data
    Replies: 6
    Last Post: Sep 13th, 2004, 08:30 AM

Posting Permissions

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