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. IFormattable,ICustomFormatter, IFormatProvider接口

    定                 义 1.IFormattable   提供一种功能,用以将对象的值格式化为字符串表示形式. 2.IFormatProvider  提供用于检索控制格式化的对象的机制 ...

  2. [ Nowcoder Contest 167 #C ] 部分和

    \(\\\) \(Description\) 给出一个长度为\(N\)的数组\(A[i]\),保证\(N\)为 \(2\) 的整次幂. 对于每个 \(i\ (i\in [0,N))\)求所有满足\(( ...

  3. js操作元素透明度以及浏览器兼容性

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  4. Atmel studio 7.0 安装成功

    Atmel studio 7.0 安装成功 转载:   http://www.eeworld.com.cn/mcu/article_2017112436241.html 2018-07-2610:27 ...

  5. centos如何离线安装部署node&pm2?

    最近我们项目要上即时通讯,因为项目对安全要求比较高,所以选择了即时通讯云服务器yun2win,他们提供了数据服务器让我们自己安装部署.那么问题来了,我们服务器是放在内网,完全无法访问外网,而yun2w ...

  6. Pytorch 加载保存模型【直播】2019 年县域农业大脑AI挑战赛---(三)保存结果

    在模型训练结束,结束后,通常是一个分割模型,输入 1024x1024 输出 4x1024x1024. 一种方法就是将整个图切块,然后每张预测,但是有个不好处就是可能在边界处断续. 由于这种切块再预测很 ...

  7. ViewData丶ViewBag和TempData

    案例: public ActionResult Index() { ViewData[; ViewData.Add(); ViewBag.myNum = ; TempData[; Student st ...

  8. scala学习(3)-----wordcount【sparksession】

    参考: spark中文官方网址:http://spark.apachecn.org/#/ https://www.iteblog.com/archives/1674.html 一.知识点: 1.Dat ...

  9. Linux学习笔记记录(六)

  10. 截取命令cut命令、awk命令、sed命令

    cut命令 截取以制表符tab为分隔符的第一列 cut -f 1test.txt 截取以":"为分隔符的第一列,如果比较规律的文件,可以自己设定分隔符 cut -f 1 -d ': ...