Type-safe queries approach helps to verify the correctness of the query statement at compile time.

Have you ever face a problem that you changing the entity field and then the application throw exception at runtime due to the change? If you have been facing that problem I think using type-safe queries might be a good choice.

Problem with JPQL or Native query

When writing query statements we mostly use JPQL or native query and write literally in string.

I purposely wrote the wrong query but we got no warning from the compiler because it has no way to detect such errors. …

Let’s learn how to update the document with MongoTemplate with an explanation of how it works.

Follow part one of the CRUD document using MongoTemplate series which shares about inserting documents, in this article, we will learn how to update documents by using MongoTemplate.

Update behavior

MongoTemplate provides a save method that allows updating documents. The diagram below shows how the save method work in basic.

How the mongotemplate save method works
How the mongotemplate save method works
  • At first, if the _id value was not provided the MongoDB will process the saving document as inserting a new document.
  • If the _id provided, MongoDB server will scan the collection and tries to a matching document with provided _id value. If a document matching the provided _id found, it will be…

Centralizing cache or localizing cache.

Many of us have known about the distributed system, right? It is the kind of software architecture that we distribute the application on multiple servers. It allows us to build high scalability and high reliability. The drawback is managing data consistency becomes more complex. And invalidate cache is one of the complex tasks too. Therefore in this article, I would like to share two caching approaches for a distributed system. Centralized Cache or Localized Cache.

Centralizing cache for distributed system

The centralizing cache is mostly done by using cache servers such as Memcached or Redis. …

As a java developer, I guest that you heard about the connection pool and might apply it to your system. Therefore in this article, we will learn how to configure C3P0 which is the most popular connection-pool library for java developers.

What is the C3P0 connection pool?

If you are a fan of Start War you might think C3P0 is this guy.

At my first time hearing about the connection pool and saw C3P0, I have made a mistake about the ZERO and O letter. I did not know and googling c3po and google show me this guy and I was confused. …

In this article, we learn how to use Transactional annotation in a spring application to guarantee the consistency of data.

Why we need transactions?

Let start with an example of saving order information. In many cases to store order information, we will have two tables let call Order and OrderLine. In consequently the system must save data for the order first then sort info of order line items. Now have to think about the case we save Order data successfully but the OrderLine failed. For this case, we have to rollback the order info to be not saved in the database to guarantee the consistency of the order information. In such problems, we have to use transactions.

working with transaction in applications
working with transaction in applications

The benefits of using Transactional annotation

Reduce boilplate code

In java or other languages, the developer…

Spring Data MongoDB provides a class named MongoTemplate which allows developers to perform common operations with the MongoDB server such as adding documents, updating documents, deleting documents, or collections.

This article is the first part of CRUD document using MongoTemplate that shares a detailed explanation of inserting document process while insert document with MongoTemplate.

Insert a new document process

Insert new document process in mongodb
Insert new document process in mongodb

First, we have a new document to be inserted to Mongodb, If the collection of the document does not exist(created) the MongoDB will create the collection first then continue to perform the insert process.

If the inserted document omitted the _id field, the MongoDB driver automatically…

The @Document annotation

This annotation represents a MongoDB collection. It similars to Entity annotation of Java persistence API. We must be put this annotation at the class level. If the collection’s name different from the class name we can specify the name by using the collection property or value property.

Document annotation — Spring Data Mongodb
Document annotation — Spring Data Mongodb

The Field annotation

The Field annotation used to represent a field in a collection. It is similar to the Column annotation in java persistence API but it does not have much property like java.persistence.Column.

Field properties:

  • name → to specify the name of the field stored in MongoDB. …

Learn how to configure and connect a spring boot application to MongoDB.

In recent decade Mongodb rises to become of the most database used in the software industry. This database stands on top of document databases it provides the scalability and flexibility that you want with the querying and indexing that you need. Therefore, I would like to share how we can connect a spring application to MongoDB and execute basic accessing data operations.

How does spring boot support integrating MongoDB?

Before jumping to code, I would like to introduce how does spring support MongoDB in basic.

Spring boot supports MongoDB integration through the spring-boot-starter-data-mongodb module which already included spring-boot-starter, mongodb-driver-sync and spring-data-mongodb modules. Each module has a different…

Learn how to integrate HikariCP in the Spring Boot application.

In order to increase the performance while accessing data from a database, we must consider applying the connection-pool. You can search to know how does it help to increase the performance. I already wrote an article about this here:

In this article, I would like to share how we use the HikariCP library in a Spring Boot application.

Why HikariCP?

HikariCP is a lightweight connection-pool library. This library is also known as the fastest connection pool library. “Fast, simple, reliable, lightweight” are four words used to describe it. At the time I’m writing this article it has more than 63.3k …

Many systems require auditing and tracking the change in the persistence layer. It is really helpful to investigate problems related to the data. Data auditing typically answers these questions: when was it created, who was create the record, when was it created, who modified the record last and when was it modified last. In this article, we will learn how to implement auditing features.

How Spring Data JPA support auditing

By now, Spring provides four annotations in the spring-data-commons module for auditing features. They are:

  • @CreatedByrepresents the principal that created the entity containing the field.
  • @CreatedDate represents the date the entity containing the field was created.

Programming Sharing

We write about programming. We share about programming.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store