Menu

fetch or create method

Balaji
2007-12-10
2013-04-25
  • Balaji

    Balaji - 2007-12-10

    I am looking for a method which would try to retrieve a single record and if it is not available, the record is created and returned.  If such method is there, pls let me know. Otherwise, I am planning to add the following code in the manager.java.vm

        public $beanClass loadUniqueOrInsertUsingTemplate($beanClass bean) throws DAOException
        {
             $beanClass[] beans = this.loadUsingTemplate(bean);
             if (beans.length == 0)
                 return save(bean);
             if (beans.length > 1)
                 throw new ObjectRetrievalException("More than one element !!");
             return beans[0];
         }

    Just made a minor modification to loadUniqueUsingTemplate(); changed the "return null" into "return save(bean)".  I guess this will work; if you think there might be issues, please let me know.

    Thanks
    Balaji

     
    • nfdavenport

      nfdavenport - 2007-12-10

      Sounds to me like something that should be in a transaction or sychronized or whatever, otherwise you will get duplicates created under load.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.