Month: May 2020

  • How to automatically update creation and update timestamp through hibernate?

    Let’s say you have two columns in all of your tables , one to track when a record was created and another to track when it was last updated. You have chosen hibernate as your ORM framework and created corresponding entity classes as well. Hibernate allows you to update these columns automatically ! You just…

  • How to represent a composite key in hibernate?

    Consider the below class representing a Student. It has a primary key id , a class id and a name field. Say you want to combine the id and class id into a composite key. You want the same student id to be used against a different class id. We can achieve this in hibernate…

  • How to map JSON data in PostGreSQL database to a Hibernate Entity column?

    PostGreSQL database is one of the most popular open source databases . It has features which traditional relational databases lack , like storing a JSON as itself in the database. JSON is a widely used communication data format and hence this feature comes quite handy. The problem arises when you use an ORM tool like…

  • How to auto generate and auto increment identifier values for PostGreSQL tables through Hibernate?

    I was trying to connect to PostGreSQL database through Spring Boot. I used spring-boot-starter-data-jpa dependency which automatically included hibernate dependencies to the project. And since I was connecting to postgresql database I included the following postgresql dependency as well: I created a simple table named my_table with an integer ID and a JSON data type…

  • How to preserve newline character in YAML property value?

    I dealt with a particular use case in my project. I had to fetch a string with newline characters from a yaml property file. The string value was something like this: quote: “I am Bond \n James Bond” The problem was when I fetched this quote property in my code , it got modified into…

  • How to measure time in a single unit of time in Java?

    Let’s say you want to find out the number of weeks since your birthday , or the number of decades , or the number of hours . And you want to do this in a single line of code. Java 8 allows you to do that. It provides an API named ChronoUnit under java.time.temporal package…

  • How to calculate your next salary date using Java Temporal Adjuster?

    Java 8 came up with a fresh set of packages for dealing with Time and Date. Prior to this dealing with java dates was quite clumsy. One such package is the java.time.temporal package which allows you to adjust a given date to another date based on certain rules like: Given this date , what is…

  • How to populate application properties dynamically in Spring Boot?

    Assumption: Maven is used to build the spring boot project Let’s say you want to populate application.properties dynamically while building the project in Spring Boot. You can fetch those properties from various sources like: pom.xml an external file System properties settings xml of Maven To accomplish this , define the properties in application.properties like this…

  • What are the different ways to group a collection in Java?

    Let’s say you have a collection of phones. The phones have the attributes : model name , phone type (Android or Iphone) , cost , color and rating. And you want to group the phones based on whether they are Iphones or Android Phones. You want to find the costliest IPhone and the costliest Android…

  • How does OAuth 2.0 work?

    Let’s say you have created an API. You want to protect it. There are so many ways to do so. And one of the most efficient way to do is to protect it using OAuth 2.0. As oauth0.com explains: OAuth 2.0 is a protocol that allows a user to grant limited access to their resources on…