[JavaEE] Data Validation
When we create Entity and Respority, we also need to do validations to protect our data.
In Java, validations are built-in, using decorators. For Typescript, found a useful libaray to do the similar validation as well. Checkout class-validator
Entity:
@Entity
public class Book { // ======================================
// = Attributes =
// ====================================== @Id
@GeneratedValue
private Long id; @Column(length = 200)
@NotNull
@Size(min = 1, max = 200)
private String title; @Column(length = 10000)
@Size(min = 1, max = 10000)
private String description; @Column(name = "unit_cost")
@Min(1)
private Float unitCost; @Column(length = 50)
@NotNull
@Size(min = 1, max = 50)
private String isbn; @Column(name = "publication_date")
@Temporal(TemporalType.DATE)
@Past
private Date publicationDate; ....
}
Testing:
We want to test, if we give title as null, it should throw exception.
@RunWith(Arquillian.class)
public class BookRepositoryTest { @Inject
private BookRepository bookRepository; // We want the test throw exception
@Test(expected = Exception.class)
public void createInvalidBook() {
Book book = new Book("isbn", null, 12F, 123, Language.ENGLISH, new Date(), "imageURL", "description");
bookRepository.create(book);
} @Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(BookRepository.class)
.addClass(Book.class)
.addClass(Language.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml");
} @org.junit.Test
public void create() {
}
}
Respority:
@Transactional(SUPPORTS)
public class BookRepository { // ======================================
// = Injection Points =
// ====================================== @PersistenceContext(unitName = "bookStorePU")
private EntityManager em; // ======================================
// = Business methods =
// ====================================== public Book find(@NotNull Long id) {
return em.find(Book.class, id);
} // For creating and deleting methods, we want to use REQUIRED
@Transactional(REQUIRED)
public Book create(@NotNull Book book) {
em.persist(book);
return book;
} }
Testing:
@Test(expected = Exception.class)
public void findWithInvalidId() {
bookRepository.find(null);
}
[JavaEE] Data Validation的更多相关文章
- [WPF系列]-Data Validation
项目经常前台界面涉及到用户输入时,我们常常会用到数据有效性的验证.在网页中我们之前用js来校验Form中的数据有效性.在WPF中我们如何实现这种验证机制了?答案:INotifyDataErrorInf ...
- Chapter Data Modification & Chapter Data Validation
Chapter Data Modification XF的数据提交,支持单行.集合和多层次的master-details结构的数据. Create 当提交如下数据 <Job> <Id ...
- .NET MVC 学习笔记(五)— Data Validation
.NET MVC 学习笔记(五)—— Data Validation 在实际应用中,我们需要对数据进行增查改删业务,在添加和修改过程中,无论你编写什么样的网页程序,都需要对用户的数据进行验证,以确数据 ...
- Validating HTTP data with Play
Validations ensure that the data has certain values or meets specific requirements. You can use vali ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型
Creating a complex data model 创建复杂数据模型 8 of 9 people found this helpful The Contoso University sampl ...
- 使用Data Annotations进行手动数据验证
Data Annotations是在Asp.Net中用于表单验证的 它通过Attribute直接标记字段的有效性,简单且直观.在非Asp.Net程序中(如控制台程序),我们也可以使用Data Anno ...
- c# Use Properties Instead of Accessible Data Members
advantage of properties: 1 properties can be used in data binding, public data member can not. 2 dat ...
- WPF Input Validation Using MVVM
Data validation is a key part in WPF.Validation is used to alert the user that the data he entered i ...
- 运用模型绑定和web窗体显示和检索数据(Retrieving and displaying data with model binding and web forms)
原文 http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data ...
随机推荐
- nginx connect failed (110- Connection timed out) 问题排查
首先排查 ping 下 nginx 与代理服务是否ping 的通,带端口的,telnet 下端口号是否是通的,本次遇到问题为 telnet 发现有台服务器不通,原因是端口未开放
- Unity笔记(3)自学第三天
学习记录: 脚本使用:
- Burp Suite抓https数据包
本地环境JDK1.8Burp Suite 1.7.26 Firefox 59.0.2 一.burp介绍请自行谷歌,这里不过多介绍 二.配置HTTPS抓包方法[以Firefox为例]通常情况下burp默 ...
- 【译】x86程序员手册23-6.5组合页与段保护
6.5 Combining Page and Segment Protection 组合页与段保护 When paging is enabled, the 80386 first evaluates ...
- 【Linux】centos7 添加脚本到/etc/rc.local文件里,实现开机自启
Linux 设置开机自启动,添加命令到/etc/rc.d/rc.local,本文以设置tomcat自启动为例: 一:添加自启动命令 export JAVA_HOME=/usr/java/jdk1.8. ...
- vue项目 build之后发布到服务器index.html页面空白解决方法
第一部分 之前一直不太理解为什么要使用vue+webapck,还有在使用了vue-cli之后会用到后台,即vue-cli自动帮我们安装了express服务器,在本地服务器上运行,因为我们希望可以模拟在 ...
- php总结回顾
做人不能一直埋着头往前跑,还要偶尔停下来看下来时的路.所以今天就来回顾下之前的吧 下面依次介绍 [一]TP加载流程 ①应用入口文件index.php→②tp公共入口文件ThinkPHP.php→③核心 ...
- flipt 一个基于golang 的特性工具开发类库
以前介绍过一个Flagr 的基于golang 的特性功能开发类库(技术雷达推荐),今天看到一个类似也很不错的方案flipt 参考架构 包含的特性 快速,使用golang 编写,同时进行了性能优化 运行 ...
- .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类
引言 由公司需要使用dapper 同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions 并配套 生成实体类小工具的方 ...
- css3文字渐变无效果的解决方案
现在css3越来月流行了,为了实现一些高大上的效果,我们会用一些渐变的特效,请看文字渐变的特效代码: .title { font-size: 60px; line-height: 80px; text ...