/** * Updates the given subject with the given data. * * @param subject The subject to be updated * @param code The subject code * @param name The subject name * @param educationType */ public void update(Subject subject, String code, String name, EducationType educationType) { EntityManager entityManager = getEntityManager(); subject.setCode(code); subject.setName(name); subject.setEducationType(educationType); entityManager.persist(subject); }
/** * Creates a new subject. * * @param code The subject code * @param name The subject name * @param educationType * @return The created subject */ public Subject create(String code, String name, EducationType educationType) { EntityManager entityManager = getEntityManager(); Subject subject = new Subject(); subject.setName(name); subject.setCode(code); subject.setEducationType(educationType); entityManager.persist(subject); return subject; }
/** * Returns the subject corresponding to the given code. * * @param code The subject code * @return The subject corresponding to the given code */ public Subject findByCode(String code) { // TODO How to add a case sensitive restriction with Hibernate and MySQL? // List<Subject> subjects = s.createCriteria(Subject.class).add(Restrictions.eq("code", // code)).list(); EntityManager entityManager = getEntityManager(); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Subject> criteria = criteriaBuilder.createQuery(Subject.class); Root<Subject> root = criteria.from(Subject.class); criteria.select(root); criteria.where(criteriaBuilder.equal(root.get(Subject_.code), code)); List<Subject> subjects = entityManager.createQuery(criteria).getResultList(); for (Subject subject : subjects) { if (code.equals(subject.getCode())) return subject; } return null; }