Program: 98501
Project: 98501
Buggy source:
public CourseRatingVo rateCourse(long courseId,long userId,float value) throws MultipleMessagesException, DuplicityException {
  EntityManager entityManager=null;
  EntityTransaction transaction=null;
  CourseRatingVo rating=null;
  try {
    entityManager=getEntityManagerFactory().createEntityManager();
    CourseRatingVo existingRating=getServiceFactory().getCourseRatingsService().getByCourseIdAndUserId(entityManager,courseId,userId);
    if (value == 0.0) {
      if (existingRating != null) {
        transaction=entityManager.getTransaction();
        transaction.begin();
        getServiceFactory().getCourseRatingsService().delete(entityManager,existingRating.getId());
        transaction.commit();
      }
    }
 else {
      transaction=entityManager.getTransaction();
      transaction.begin();
      if (existingRating == null) {
        CourseRatingVo vo=new CourseRatingVo();
        vo.setCourseId(courseId);
        vo.setUserId(userId);
        vo.setValue(value);
        rating=getServiceFactory().getCourseRatingsService().create(entityManager,vo);
      }
 else {
        existingRating.setValue(value);
        rating=getServiceFactory().getCourseRatingsService().update(entityManager,existingRating);
      }
      transaction.commit();
    }
  }
 catch (  Exception exception) {
    getServiceFactory().getLogService().error(exception.getMessage(),exception);
    FacadesHelper.checkException(exception,MultipleMessagesException.class);
    FacadesHelper.checkDuplicityViolation(entityManager,transaction,exception);
    FacadesHelper.rollbackTransaction(entityManager,transaction,exception);
  }
 finally {
    FacadesHelper.closeEntityManager(entityManager);
  }
  return rating;
}

Fixed version (ground truth):
public CourseRatingVo rateCourse(long courseId,long userId,float value) throws MultipleMessagesException, DuplicityException {
  EntityManager entityManager=null;
  EntityTransaction transaction=null;
  CourseRatingVo rating=null;
  try {
    entityManager=getEntityManagerFactory().createEntityManager();
    CourseRatingVo existingRating=getServiceFactory().getCourseRatingsService().getByCourseIdAndUserId(entityManager,courseId,userId);
    if (value == 0.0) {
      if (existingRating != null) {
        transaction=entityManager.getTransaction();
        transaction.begin();
        getServiceFactory().getCourseRatingsService().delete(entityManager,existingRating.getId());
        transaction.commit();
      }
    }
 else {
      transaction=entityManager.getTransaction();
      transaction.begin();
      if (existingRating == null) {
        CourseRatingVo vo=new CourseRatingVo();
        vo.setCourseId(courseId);
        vo.setUserId(userId);
        vo.setValue(value);
        rating=getServiceFactory().getCourseRatingsService().create(entityManager,vo);
      }
 else {
        existingRating.setValue(value);
        rating=getServiceFactory().getCourseRatingsService().update(entityManager,existingRating);
      }
      transaction.commit();
    }
  }
 catch (  Exception exception) {
    getServiceFactory().getLogService().error(exception.getMessage(),exception);
    FacadesHelper.checkException(entityManager,exception,MultipleMessagesException.class);
    FacadesHelper.checkDuplicityViolation(entityManager,transaction,exception);
    FacadesHelper.rollbackTransactionAndCloseEntityManager(entityManager,transaction,exception);
  }
 finally {
    FacadesHelper.closeEntityManager(entityManager);
  }
  return rating;
}