[JavaEE] JTA, Java Transaction API, Repository for DB opreations

Mainly two things:

1. For all the creating and deleting opreations for the DB, we want to use 'REQUIRED' for the transaction.

2. For all the read only opreations for the DB, we want to use 'SUPPORTS' for the transaction.

 

package com.pluralsight.bookstore.repository;

import com.pluralsight.bookstore.model.Book;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
import java.util.List;

import static javax.transaction.Transactional.TxType.REQUIRED;
import static javax.transaction.Transactional.TxType.SUPPORTS;


/**
 * @author Antonio Goncalves
 *         http://www.antoniogoncalves.org
 *         --
 */

// For readonly methods we want to using SUPPORTS
@Transactional(SUPPORTS)
public class BookRepository {

    // ======================================
    // =          Injection Points          =
    // ======================================

    @PersistenceContext(unitName = "bookStorePU")
    private EntityManager em;

    // ======================================
    // =          Business methods          =
    // ======================================

    public Book find(Long id) {
        return em.find(Book.class, id);
    }

    public List<Book> findAll() {
        // For complex SQL, we can also using Query language
        TypedQuery<Book> query = em.createQuery("SELECT b FROM Book b ORDER BY b.title DESC", Book.class);
        return query.getResultList();
    }

    public Long countAll() {
        TypedQuery<Long> query = em.createQuery("SELECT COUNT(b) FROM Book b", Long.class);
        return query.getSingleResult();
    }

    // For creating and deleting methods, we want to use REQUIRED
    @Transactional(REQUIRED)
    public Book create(Book book) {
        em.persist(book);
        return book;
    }

    @Transactional(REQUIRED)
    public void delete(Long id) {
        em.remove(em.getReference(Book.class, id));
    }
}

 

For the normal (simple) DB opreations, we can see, it uses 'find, persist, remove(getReference)'. Those methods are all from 'EntityManager'. You can think 'EntityMangaer' is a utils service, which can be injected to the Repository to preform operations. In this case, Repository is something similar to 'Effect' (NgRX).

@Transactional(SUPPORTS)
public class BookRepository {

    // ======================================
    // =          Injection Points          =
    // ======================================

    @PersistenceContext(unitName = "bookStorePU")
    private EntityManager em;

    // ======================================
    // =          Business methods          =
    // ======================================

    public Book find(Long id) {
        return em.find(Book.class, id);
    }

    public List<Book> findAll() {
        // For complex SQL, we can also using Query language
        TypedQuery<Book> query = em.createQuery("SELECT b FROM Book b ORDER BY b.title DESC", Book.class);
        return query.getResultList();
    }

    public Long countAll() {
        TypedQuery<Long> query = em.createQuery("SELECT COUNT(b) FROM Book b", Long.class);
        return query.getSingleResult();
    }

    // For creating and deleting methods, we want to use REQUIRED
    @Transactional(REQUIRED)
    public Book create(Book book) {
        em.persist(book);
        return book;
    }

    @Transactional(REQUIRED)
    public void delete(Long id) {
        em.remove(em.getReference(Book.class, id));
    }
}

 

 

Also for some complex SQL operations, we can wirte SQL to query the DB:

    public List<Book> findAll() {
        // For complex SQL, we can also using Query language
        TypedQuery<Book> query = em.createQuery("SELECT b FROM Book b ORDER BY b.title DESC", Book.class);
        return query.getResultList();
    }

    public Long countAll() {
        TypedQuery<Long> query = em.createQuery("SELECT COUNT(b) FROM Book b", Long.class);
        return query.getSingleResult();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值