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();
}

[JavaEE] JTA, Java Transaction API, Repository for DB opreations的更多相关文章

  1. 分布式事务(二)Java事务API(JTA)规范

    一.引子 既然出现了分布式场景(DTP模型), 大java也及时制定出一套规范来给各大应用服务器.数据库/mq等厂商使用,以方便管理互通--->JTA闪亮登场.JTA(Java Transact ...

  2. .NET C#到Java没那么难,DB篇

    前言 .NET C#到Java没那么难,都是面向对象的语言,而且语法还是相似的,先对比一下开发环境,再到Servlet,再到MVC,都是一样一样的,只是JAVA的配制项比较多而已,只要配好一个,后面都 ...

  3. JAVA PERSISTENCE API (JPA)

    13.2.1. About JPA The Java Persistence API (JPA) is the standard for using persistence in Java proje ...

  4. Java EE (4) -- Java EE 6 Java Persistence API Developer Certified Expert(1z0-898)

    Overview of the Java Persistence API Describe the basics of Object Relational Mapping (ORM) Define t ...

  5. Java mongodb api疑问之MongoCollection与DBCollection

    在学习Java mongodb api时发现,可以调用不同的java mongodb api来连接数据库并进行相关操作. 方式一: 该方式使用mongoClient.getDB("xxx&q ...

  6. 没想到吧,Java开发 API接口可以不用写 Controller了

    本文案例收录在 https://github.com/chengxy-nds/Springboot-Notebook 大家好,我是小富~ 今天介绍我正在用的一款高效敏捷开发工具magic-api,顺便 ...

  7. 关于c#调用java中间件api的几个问题

    由于项目需要,做的c#客户端数据库连接串首先肯定不能写死的程序里(数据库很容易被攻击,我们的项目半年改了几次密码...) 放置在配置文件内,都可以看得到,最开始想法将配置文件加密,老师说加密过的文件还 ...

  8. Kylin Java RESTful API

    最近在做大数据方面的开发, 学习研究了一段时间的kylin系统, 对于前端开发需要使用 RESTful API ,但是官网并没有提供详细的Java  API. 经过几天的看文档,最终写出了 Java ...

  9. Java 2D API - 2. Graphics 入门

    Java 2D API强大而复杂,不过大多时候我们只需使用java.awt.Graphcis类的部分功能.下面的内容将覆盖大多数的常见应用. Graphics 类中的方法大致可以分为两类: Draw ...

随机推荐

  1. 使用jstack精确找到异常代码的

    https://blog.csdn.net/mr__fang/article/details/68496248

  2. poj3411 Paid Roads

    思路: 搜索.注意点和边都有可能经过多次. 实现: #include <iostream> #include <cstdio> #include <vector> ...

  3. 【工具】Webpack

    远程仓库建立 码云创建组织项目 git clone ssh 切换到主分支mmall-fe后git remote add origin ssh git pull origin master把master ...

  4. spark查看stage和tasks信息

    spark提供了web-ui接口.外部命令等多种方法监视spark程序的执行状态.利用spark的监视功能,可以方便的查看spark应用程序执行的状态,具体包括:1)stage和tasks列表信息  ...

  5. os下,vs code 自动编译ts

    nodejs安装ts npm install -g typescript 进入工程目录,用命令初始化ts(生成tsconfig.json) tsc --init 如果要指定生成目录,打开tsconfi ...

  6. php第二十五节课

    详情删除 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  7. 『 Luogu P3205 』 HNOI2010 合唱队

    解题思路 设置两个二维数组 $f$ 和 $g$,含义如下. $f[l][r]$ 表示在期望得到的队形中 $l\rightarrow r$ 这段区间初始队形排列的方案数,并且最后一个加入进去的是第 $l ...

  8. 配置Struts2后运行jsp出现404的解决方法

    更新:善用控制台查看错误信息 --------------------------------------------- 原因:Java Build Path没有导入正确的jar包或者导入了但没有把相 ...

  9. 自定义Realm

    [单Realm] 1) jar包 2) 实现自定义Realm public class RealmOne implements Realm{ /** * 获取基本类名 */ @Override pub ...

  10. RequestMapping_请求方式

    [映射请求参数.请求方法或请求头] 1.@RequestMapping 除了可以使用请求URL 映射请求外,还可以使用请求方法.请求参数及请求头映射请求. 2.@RequestMapping 的val ...