PersistenceService persistenceSvc = PersistenceService.getInstance();
try {
persistenceSvc.beginTx();
EntityManager em = persistenceSvc.getEntityManager();
HealthRecord hr = em.find(HealthRecord.class, healthRecordId);
UserPreferences preferences = hr.getPreferences();
String username = req.getRemoteUser();
boolean updateHRisRequired = false;
if (preferences == null) {
preferences = new UserPreferences(username);
preferences.setShowCarenotebook(careNotebookVal);
hr.setPreferences(preferences);
updateHRisRequired = true;
} else {
preferences.setLastUpdatedUser(username);
preferences.setShowCarenotebook(careNotebookVal);
}