Created
July 1, 2019 14:43
-
-
Save recursivecodes/d17b4573f032091db0b81675fca003ea to your computer and use it in GitHub Desktop.
UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public User save(User user) { | |
entityManager.getTransaction().begin(); | |
entityManager.persist(user); | |
entityManager.getTransaction().commit(); | |
return user; | |
} | |
public User get(String id) { | |
User user = entityManager.find(User.class, id); | |
return user; | |
} | |
public List<User> findAll() { | |
return entityManager.createQuery("from User").getResultList(); | |
} | |
public List<User> findAll(int offset, int max) { | |
Query query = entityManager.createQuery("from User"); | |
query.setFirstResult(offset); | |
query.setMaxResults(max); | |
return query.getResultList(); | |
} | |
public long count() { | |
Query queryTotal = entityManager.createQuery("Select count(u.id) from User u"); | |
long countResult = (long)queryTotal.getSingleResult(); | |
return countResult; | |
} | |
public void deleteById(String id) { | |
// Retrieve the movie with this ID | |
User user = get(id); | |
if (user != null) { | |
try { | |
entityManager.getTransaction().begin(); | |
entityManager.remove(user); | |
entityManager.getTransaction().commit(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment