These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: proper DAO way to do a 'save if not in the db'?
PostPosted: Tue Jun 23, 2009 2:27 pm 
Newbie

Joined: Wed Feb 23, 2005 9:57 pm
Posts: 16
Code:
    public User addUser(final String name, final String uid) {
        final List<User> result = findByProperty("uid", uid);

        if (!result.isEmpty())
            return (result.get(0));

        final User user = new User(name, uid);

        saveOrUpdate(user);

        return (user);
    }

where findByProperty is using a Criteria and adding
Code:
Restrictions.eq(propertyName, propertyValue))

The uid in this case is not the table's primary key, it's from another system (ldap), although it is unique.

The main problem that I see with my code, and there may be others, is that there's window of opportunity for a collision, between the check for existence and the saveOrUpdate.

Thanks for your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.