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.

  1. package com.pluralsight.bookstore.repository;
  2.  
  3. import com.pluralsight.bookstore.model.Book;
  4.  
  5. import javax.persistence.EntityManager;
  6. import javax.persistence.PersistenceContext;
  7. import javax.persistence.TypedQuery;
  8. import javax.transaction.Transactional;
  9. import java.util.List;
  10.  
  11. import static javax.transaction.Transactional.TxType.REQUIRED;
  12. import static javax.transaction.Transactional.TxType.SUPPORTS;
  13.  
  14. /**
  15. * @author Antonio Goncalves
  16. * http://www.antoniogoncalves.org
  17. * --
  18. */
  19.  
  20. // For readonly methods we want to using SUPPORTS
  21. @Transactional(SUPPORTS)
  22. public class BookRepository {
  23.  
  24. // ======================================
  25. // = Injection Points =
  26. // ======================================
  27.  
  28. @PersistenceContext(unitName = "bookStorePU")
  29. private EntityManager em;
  30.  
  31. // ======================================
  32. // = Business methods =
  33. // ======================================
  34.  
  35. public Book find(Long id) {
  36. return em.find(Book.class, id);
  37. }
  38.  
  39. public List<Book> findAll() {
  40. // For complex SQL, we can also using Query language
  41. TypedQuery<Book> query = em.createQuery("SELECT b FROM Book b ORDER BY b.title DESC", Book.class);
  42. return query.getResultList();
  43. }
  44.  
  45. public Long countAll() {
  46. TypedQuery<Long> query = em.createQuery("SELECT COUNT(b) FROM Book b", Long.class);
  47. return query.getSingleResult();
  48. }
  49.  
  50. // For creating and deleting methods, we want to use REQUIRED
  51. @Transactional(REQUIRED)
  52. public Book create(Book book) {
  53. em.persist(book);
  54. return book;
  55. }
  56.  
  57. @Transactional(REQUIRED)
  58. public void delete(Long id) {
  59. em.remove(em.getReference(Book.class, id));
  60. }
  61. }

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).

  1. @Transactional(SUPPORTS)
  2. public class BookRepository {
  3.  
  4. // ======================================
  5. // = Injection Points =
  6. // ======================================
  7.  
  8. @PersistenceContext(unitName = "bookStorePU")
  9. private EntityManager em;
  10.  
  11. // ======================================
  12. // = Business methods =
  13. // ======================================
  14.  
  15. public Book find(Long id) {
  16. return em.find(Book.class, id);
  17. }
  18.  
  19. public List<Book> findAll() {
  20. // For complex SQL, we can also using Query language
  21. TypedQuery<Book> query = em.createQuery("SELECT b FROM Book b ORDER BY b.title DESC", Book.class);
  22. return query.getResultList();
  23. }
  24.  
  25. public Long countAll() {
  26. TypedQuery<Long> query = em.createQuery("SELECT COUNT(b) FROM Book b", Long.class);
  27. return query.getSingleResult();
  28. }
  29.  
  30. // For creating and deleting methods, we want to use REQUIRED
  31. @Transactional(REQUIRED)
  32. public Book create(Book book) {
  33. em.persist(book);
  34. return book;
  35. }
  36.  
  37. @Transactional(REQUIRED)
  38. public void delete(Long id) {
  39. em.remove(em.getReference(Book.class, id));
  40. }
  41. }

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

  1. public List<Book> findAll() {
  2. // For complex SQL, we can also using Query language
  3. TypedQuery<Book> query = em.createQuery("SELECT b FROM Book b ORDER BY b.title DESC", Book.class);
  4. return query.getResultList();
  5. }
  6.  
  7. public Long countAll() {
  8. TypedQuery<Long> query = em.createQuery("SELECT COUNT(b) FROM Book b", Long.class);
  9. return query.getSingleResult();
  10. }

[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. PostgreSQL与MySQL比较

    特性 MySQL PostgreSQL 实例 通过执行 MySQL 命令(mysqld)启动实例.一个实例可以管理一个或多个数据库.一台服务器可以运行多个 mysqld 实例.一个实例管理器可以监视 ...

  2. 利用freemarker导出页面格式复杂的excel

    刚开始大家可能会利用poi生成简单的excel,但是遇到需要生成复杂的excel,poi导出excel就比较困难,这时候可以利用freemarker来渲染实现实现生成复杂的excel, 首先,将exc ...

  3. Json-->Newton.Json.dll的使用方法

    Newton.Json.dll  for .NET2.0 实体1 public class Student    {        public string ID { get; set; }     ...

  4. Selenium基于Python web自动化测试框架 -- PO

    关于selenium测试框架首先想到的就是PO模型,简单说下PO模型 PO模型的概念和理解: PO就是一个设计思想,将代码以页面为单位进行组织,针对这个页面上的所有信息.相关操作都放到一个类中,从而使 ...

  5. js中获取class封装

    1.封装 //封装getClass function getClass(tagName,className) //获得标签名为tagName,类名className的元素 { if(document. ...

  6. 场景分割:MIT Scene Parsing 与DilatedNet 扩展卷积网络

    MIT Scene Parsing Benchmark简介 Scene parsing is to segment and parse an image into different image re ...

  7. MySql(四)Select条件查询

    select条件查询的格式如下: SELECT 查询列表FROM 表名WHERE 筛选条件:123456根据筛选条件可以分为以下几类: 按照条件按表达式进行筛选 常用条件运算符如下:> .< ...

  8. 【maven】Description Resource Path Location Type An error occurred while filtering resources TESTVIDEO line

    在maven中构建项目的时候发现了如下错误: Description Resource Path Location Type An error occurred while filtering res ...

  9. 巩固JavaSE基础--IDEA完成实战项目

    PS:学习完JavaSE基础后,需要有一个项目来测试自己的学习成果,并加以巩固.所以在这里,就让我们来学习下“一本糊涂账”项目吧.(此项目来源于Java自学网站) 项目完成效果图一览

  10. VNC 安装 (适用Redhat 9.0 和 CentOS 7.0+)

    Remote Service 本文转自https://www.cnblogs.com/yjscloud/p/6695388.html VNC 安装 (适用Redhat 9.0 和 CentOS 7.0 ...