devxlogo

Java

How to Trigger an Async Query

Query methods defined in repositories can be executed asynchronously. This means that the query method will return immediately upon invocation. The sample code below relies on @Async annotation and Future,

How to Selectively Expose CRUD Operations

In order to selectively expose CRUD operations, we need to define an intermediate interface, annotated as below: @NoRepositoryBeaninterface IntermediateRepository extends Repository { // add here the selected CRUD, for example

How to Quickly Sort an Array

The quickest solution for sorting an array in Java relies on Arrays.sort() method as below: int[] arr = {5, 12, 3, 44, 55}; Arrays.sort(arr);

Allow Only One Null Property

Consider a Review object with three properties: article, book and magazine. Let’s assume that only one of these three properties should be set as non-null. For checking this constraint we

How to Declare a Pattern in Java

The best way to declare a Pattern in Java is as a constant, since Pattern is immutable. Use the following code: private static final Pattern PATTERN = Pattern.compile(” +”); Further,

Handling BindException in Java

Port BindExceptions can occur if a port is already occupied by some other process and you try to use it again. Below is a simple example that demonstrates this and