[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 ...
随机推荐
- log4j2异步日志解读(二)AsyncLogger
前文已经讲了log4j2的AsyncAppender的实现[log4j2异步日志解读(一)AsyncAppender],今天我们看看AsyncLogger的实现. 看了这个图,应该很清楚AsyncLo ...
- LN : leetcode 207 Course Schedule
lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...
- LN : leetcode 191 Number of 1 Bits
lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...
- const学习(续)
续接上一篇<C++ const学习> const与成员函数 之前说到了const修饰成员函数本身. const成员函数不能修改对象成员值 对于const或者费const对象都可以调用con ...
- 【转】jvm内存结构
JVM的基本结构 包括四部分:类加载器.执行引擎.内存区(运行时数据区).本地方法接口 类加载器:jvm启动时或类运行时将需要的class文件加载到JVM中. JVM内存申请过程如下: JVM 会试图 ...
- dede手机端首页点击文章内容、列表,却跳到pc端
手机访问到手机端首页,点击列表.内容.图片等都跳到pc端,是什么原因? 查看m模板里面的index.html文件生成的代码是绝对路径(数字随机)13.html 而不是view.php?aid=13 解 ...
- js中取整数的方法
1.取整的方法 Math.floor( ) Math 对象的方法--取比当前数值小的最大整数(下取整). Math.ceil( ) Math对象的方法--取比当前数值大的最小整数(上取整). Math ...
- (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
http://blog.csdn.net/yerenyuan_pku/article/details/72863323 我们知道Jedis在处理Redis的单机版和集群版时是完全不同的,有可能在开发的 ...
- vs 2017 清空 打开项目的历史记录
- mac os 10.10解决pod问题
转一下 http://leancodingnow.com/how-to-get-cocoapods-work-on-yosemite/