Search in sources :

Example 1 with UnitOfWork

use of ninja.jpa.UnitOfWork in project ninja by ninjaframework.

the class ArticleDao method getAllArticles.

@UnitOfWork
public ArticlesDto getAllArticles() {
    EntityManager entityManager = entitiyManagerProvider.get();
    TypedQuery<Article> query = entityManager.createQuery("SELECT x FROM Article x", Article.class);
    List<Article> articles = query.getResultList();
    ArticlesDto articlesDto = new ArticlesDto();
    articlesDto.articles = articles;
    return articlesDto;
}
Also used : EntityManager(javax.persistence.EntityManager) Article(models.Article) ArticlesDto(models.ArticlesDto) UnitOfWork(ninja.jpa.UnitOfWork)

Example 2 with UnitOfWork

use of ninja.jpa.UnitOfWork in project ninja by ninjaframework.

the class ArticleDao method getArticle.

@UnitOfWork
public Article getArticle(Long id) {
    EntityManager entityManager = entitiyManagerProvider.get();
    Query q = entityManager.createQuery("SELECT x FROM Article x WHERE x.id = :idParam");
    Article article = (Article) q.setParameter("idParam", id).getSingleResult();
    return article;
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) Article(models.Article) UnitOfWork(ninja.jpa.UnitOfWork)

Example 3 with UnitOfWork

use of ninja.jpa.UnitOfWork in project ninja by ninjaframework.

the class ArticleDao method getOlderArticlesForFrontPage.

@UnitOfWork
public List<Article> getOlderArticlesForFrontPage() {
    EntityManager entityManager = entitiyManagerProvider.get();
    Query q = entityManager.createQuery("SELECT x FROM Article x ORDER BY x.postedAt DESC");
    List<Article> articles = (List<Article>) q.setFirstResult(1).setMaxResults(10).getResultList();
    return articles;
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) Article(models.Article) List(java.util.List) UnitOfWork(ninja.jpa.UnitOfWork)

Example 4 with UnitOfWork

use of ninja.jpa.UnitOfWork in project ninja by ninjaframework.

the class ArticleDao method getFirstArticleForFrontPage.

@UnitOfWork
public Article getFirstArticleForFrontPage() {
    EntityManager entityManager = entitiyManagerProvider.get();
    Query q = entityManager.createQuery("SELECT x FROM Article x ORDER BY x.postedAt DESC");
    Article article = (Article) q.setMaxResults(1).getSingleResult();
    return article;
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) Article(models.Article) UnitOfWork(ninja.jpa.UnitOfWork)

Example 5 with UnitOfWork

use of ninja.jpa.UnitOfWork in project ninja by ninjaframework.

the class UserDao method isUserAndPasswordValid.

@UnitOfWork
public boolean isUserAndPasswordValid(String username, String password) {
    if (username != null && password != null) {
        EntityManager entityManager = entityManagerProvider.get();
        TypedQuery<User> q = entityManager.createQuery("SELECT x FROM User x WHERE username = :usernameParam", User.class);
        User user = getSingleResult(q.setParameter("usernameParam", username));
        if (user != null) {
            if (user.password.equals(password)) {
                return true;
            }
        }
    }
    return false;
}
Also used : EntityManager(javax.persistence.EntityManager) User(models.User) UnitOfWork(ninja.jpa.UnitOfWork)

Aggregations

EntityManager (javax.persistence.EntityManager)5 UnitOfWork (ninja.jpa.UnitOfWork)5 Article (models.Article)4 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 List (java.util.List)1 ArticlesDto (models.ArticlesDto)1 User (models.User)1