[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();
}
[JavaEE] JTA, Java Transaction API, Repository for DB opreations的更多相关文章
- 分布式事务(二)Java事务API(JTA)规范
一.引子 既然出现了分布式场景(DTP模型), 大java也及时制定出一套规范来给各大应用服务器.数据库/mq等厂商使用,以方便管理互通--->JTA闪亮登场.JTA(Java Transact ...
- .NET C#到Java没那么难,DB篇
前言 .NET C#到Java没那么难,都是面向对象的语言,而且语法还是相似的,先对比一下开发环境,再到Servlet,再到MVC,都是一样一样的,只是JAVA的配制项比较多而已,只要配好一个,后面都 ...
- JAVA PERSISTENCE API (JPA)
13.2.1. About JPA The Java Persistence API (JPA) is the standard for using persistence in Java proje ...
- 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 ...
- Java mongodb api疑问之MongoCollection与DBCollection
在学习Java mongodb api时发现,可以调用不同的java mongodb api来连接数据库并进行相关操作. 方式一: 该方式使用mongoClient.getDB("xxx&q ...
- 没想到吧,Java开发 API接口可以不用写 Controller了
本文案例收录在 https://github.com/chengxy-nds/Springboot-Notebook 大家好,我是小富~ 今天介绍我正在用的一款高效敏捷开发工具magic-api,顺便 ...
- 关于c#调用java中间件api的几个问题
由于项目需要,做的c#客户端数据库连接串首先肯定不能写死的程序里(数据库很容易被攻击,我们的项目半年改了几次密码...) 放置在配置文件内,都可以看得到,最开始想法将配置文件加密,老师说加密过的文件还 ...
- Kylin Java RESTful API
最近在做大数据方面的开发, 学习研究了一段时间的kylin系统, 对于前端开发需要使用 RESTful API ,但是官网并没有提供详细的Java API. 经过几天的看文档,最终写出了 Java ...
- Java 2D API - 2. Graphics 入门
Java 2D API强大而复杂,不过大多时候我们只需使用java.awt.Graphcis类的部分功能.下面的内容将覆盖大多数的常见应用. Graphics 类中的方法大致可以分为两类: Draw ...
随机推荐
- 常用的几个Dos命令-持续更新中
1.服务相关 (1).查看服务 C:\Windows\system32>net start 已经启动以下 Windows 服务: (2).启动服务 C:\Windows\system32> ...
- MySql数据库存储emoji表情报错解决办法
异常:java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1 解决: ...
- dubbo与springmvc的简单使用
什么是Dubbo? dubbo是阿里巴巴公司开源的高性能优秀服务框架,通过高性能的RPC(远程服务调用)实现服务的输入输出功能,可以与spring框架无缝整合: 传统的架构所有的模块都在一台服务器上, ...
- win7如何设置自动关机
如果想设置Win7按照自己意愿自动关机,而又不希望下载安装第三方软件,则可以通过以下两个方法来简单实现. 工具/原料 Windows7操作系统环境 方法1:利用cmd命令 1 打开cmd窗口. 方法一 ...
- Framework7首页隐藏navbar
f7首页隐藏navbar其他页面显示navbar 帮别人解决问题,自己也记录一下, 首页.navbar加.navbar-hidden, 首页.page加.no-navbar, 如果首页有.navbar ...
- 多路开关模式的switch语句
在实例10中,将break语句去掉之后,会将符合检验条件后的所有语句都输出.利用这个特点,可以设计多路开关模式的switch语句,例如:在平年一年12个月,1.3.5.7.8.10.12月是31天,4 ...
- java对于07excel的读、改、写、并触发计算
InputStream is = null; try { is = new FileInputStream(filePath); } catch (FileNotFoundException e1) ...
- 【alert(1) to win】不完全攻略
alert(1) to win 一个练习XSS的平台,地址:https://alf.nu/alert1 Warmup 给出了一段JavaScript代码 function escape(s) { re ...
- BZOJ3124: [Sdoi2013]直径 (树形DP)
题意:给一颗树 第一问求直径 第二问求有多少条边是所有直径都含有的 题解:求直径就不说了 解第二问需要自己摸索出一些性质 任意记录一条直径后 跑这条直径的每一个点 如果以这个点不经过直径能到达最远的 ...
- MySQL Docker方式安装
以5.7版本为例 1 配置mysql配置文件编辑/etc/my.cnf,添加以下内容: [mysqld] skip-host-cache skip-name-resolve datadir=/var/ ...