[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();
    }
复制代码

 

posted @   Zhentiw  阅读(395)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-07-27 [Angular] Two ways to create Angular Animation, using animation() or using state()
2016-07-27 [GIF] Shape Objects in GIF Loop Coder
点击右上角即可分享
微信分享提示