user3696143
user3696143

Reputation: 301

org.springframework.dao.DataIntegrityViolationException: Duplicate entry Spring+Hibernate

I have added below code to Insert/Update record into table

public void createFacebookAccessTokenRecord(int userId, Long facebookId,
            String token, String secret) {
        Session session = getSessionFactory().getCurrentSession();
        UserAuthDetails userAuthDetails = (UserAuthDetails) session.get(
                UserAuthDetails.class, new Integer(userId));
        if (userAuthDetails != null) {
            userAuthDetails.setFacebookId(facebookId);
            userAuthDetails.setFacebookUserToken(token);
            userAuthDetails.setFacebookUserTokenSec(secret);
            session.merge(userAuthDetails);
        } else {
            userAuthDetails = new UserAuthDetails();
            userAuthDetails.setUserId(userId);
            userAuthDetails.setFacebookId(facebookId);
            userAuthDetails.setFacebookUserToken(token);
            userAuthDetails.setFacebookUserTokenSec(secret);
            userAuthDetails.setCreatedDt(new Date());
            session.save(userAuthDetails);
        }


    }

If data exist it will update table otherwise Insert into a new record,in my case data already exist so i am updating record so first if loop working but i am getting

org.springframework.dao.DataIntegrityViolationException: Duplicate entry '482186425258498' for key 'umul_facebook_id_UNIQUE'; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Duplicate entry '482186425258498' for key 'umul_facebook_id_UNIQUE'

Upvotes: 0

Views: 1329

Answers (1)

Jigar Joshi
Jigar Joshi

Reputation: 240996

you are looking up by userId and unique constraints gets violated about umul_facebook_id, so you need to check the uniqueness of FacebookId as well

Upvotes: 1

Related Questions