[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 ...
随机推荐
- Java开发笔记(九十二)文件通道的基本用法
前面介绍的各色流式IO在功能方面着实强大,处理文件的时候该具备的操作应有尽有,可流式IO在性能方面不尽如人意,它的设计原理使得实际运行效率偏低,为此从Java4开始增加了NIO技术,通过全新的架构体系 ...
- python学习笔记(6)——for...in &while
for x in ...循环就是把每个元素代入变量x,然后执行缩进块的语句. 注意:python中的格式也起到语法规则的作用,缩进块就是一个例 求和问题,比较异同 1/sum= ,,,,,,,,,]: ...
- EF 迁移操作
一. 模型设计 1. 遵循EF标准,注意表关系配对 2. 数据模型里尽量把必须的属性和说明都写全 3. EF默认id字段为主键,如果没有,需指定主键 二. 数据迁移 1. 命令运行环境:vis ...
- JavaScipt30(第十八个案例)(主要知识点:Array.prototype.map)
承接上文,这是第十八个案例,中间的十到十八我直接看了答案,因为有些例子从他打开的页面看不出他要做什么. 附上项目链接: https://github.com/wesbos/JavaScript30 这 ...
- impdp and docker install oracleXE
docker oracle https://hub.docker.com/r/sath89/oracle-xe-11g/ docker run -d -p 8080:8080 -p 1521:1521 ...
- 王垠代表作《完全用Linux工作》
完全用Linux工作-王垠 <完全用Linux工作>作者:王垠 完全用 GNU/Linux 工作 理解 GNU/Linux 更多精彩请直接访问SkySeraph个人站点:www.sky ...
- 打造完美的ImageLoader——LruCache+DiskLruCache
做android应用少不了要和网络打交道,在我刚开始学android的时候总是处理不好网络图片的加载,尤其是图片乱跳的问题,后来发现了各种缓存图片的方法:本地缓存.软引用.LruCache.... 我 ...
- Configure a proxy for your API calls with Angular CLI
Table of contents Local development setup with Angular Issue: Dev server plus backend API Configurin ...
- python爬虫24 | 搞事情了,用 Appium 爬取你的微信朋友圈。
昨天小帅b看到一些事情不顺眼 有人偷换概念 忍不住就写了一篇反讽 996 的 看不下去了,我支持996,年轻人就该996! 没想到有些人看不懂 这就算了 还来骂我 早些时候关注我的小伙伴应该知道我第一 ...
- 洛谷 1823 [COI2007] Patrik 音乐会的等待
[题解] 维护一个单调栈即可. 但是因为有相同身高的存在,所以要稍微考虑下相同身高的处理.因为这个卡了一下下QAQ... #include<cstdio> #include<algo ...