0 Tip: How to handle both insert & update timestamps using Hibernate

Tuesday, December 24, 2019 Labels: , ,
We prefer to maintain the time whenever we update or insert a row in the table. If you are using JPA/Hibernate, then you can let hibernate do it for you. This way, you will be sure, that with each update, only the update time is being updated and the same is true for the insert timestamp.

Let me show you how it's easy peasy.

Marks a property as the creation timestamp of the containing entity. The property value will be set to the current VM date exactly once when saving the owning entity for the first time.

Usage Example:

private Timestamp insertTs;

Marks a property as the update timestamp of the containing entity. The property value will be set to the current VM date whenever the owning entity is updated.

Usage Example:

private Timestamp updateTs;

Note: You will have to import these two as well in your entity:

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
Read more

0 TIP: Generate a pairs of public and private keys

Tuesday, October 29, 2019 Labels: , , , , ,
In Java, The KeyPairGenerator class is used to create a pair of private/public keys. Java supports below three basic types of algorithms with keysize in parenthesis:

  1. DiffieHellman (1024)
  2. DSA (1024)
  3. RSA (1024, 2048)

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;

public class GenerateKeysEncoded {

 private KeyPairGenerator keyGen;
 private KeyPair pair;
 private PrivateKey privateKey;
 private PublicKey publicKey;

 public GenerateKeysEncoded(int keylength, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException {
  this.keyGen = KeyPairGenerator.getInstance(algorithm);

 public void createKeys() {
  this.pair = this.keyGen.generateKeyPair();
  this.privateKey = pair.getPrivate();
  this.publicKey = pair.getPublic();

 public PrivateKey getPrivateKey() {
  return this.privateKey;

 public PublicKey getPublicKey() {
  return this.publicKey;

 public void writeToFile(String path, byte[] key) throws IOException {
  File f = new File(path);

  FileOutputStream fos = new FileOutputStream(f);

 public static void main(String[] args) {
  GenerateKeysEncoded gk;
  try {
   gk = new GenerateKeysEncoded(1024, "RSA");
   gk.writeToFile("publicKey", gk.getPublicKey().getEncoded());
   gk.writeToFile("privateKey", gk.getPrivateKey().getEncoded());
  } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
  } catch (IOException e) {



Read more

0 TIP: how to create private docker registry for your company

Thursday, October 24, 2019 Labels: , , ,
For beginners, they look towards docker hub for the registry. But, what if the company has sensitive applications, which can't be hosted even privately on docker hub. The novice engineer runs out of options. So here is the tip for the day.

You can have your very own private registry. The good thing is, it would be another running container.

You can do this on your development machine, as well as on your server. But keep in mind these few points.

1. Default port would be 5000 for the registry

2. Locally, you are not bothered about the security, but for actual production-grade Docker registry, you should be running it on 443 or over SSL.

3. Further, you can restrict its access using basic-authentication as well.

4. Last but not least, point the storage to Amazon S3 or IBM Cloud Object Storage

Best of luck with containerization!
Read more

0 TIP: Batch Updates with Spring JdcbTemplate

Wednesday, October 23, 2019 Labels: , , , , ,
Spring JdbcTemplate offers the following method to do multiple updates.

int[] org.springframework.jdbc.core.JdbcTemplate.batchUpdate(String sql, BatchPreparedStatementSetter pss) throws DataAccessException

1. Issue multiple update statements on a single PreparedStatement, using batch updates and a BatchPreparedStatementSetter to set values.
2. Will fall back to separate updates on a single PreparedStatement if the JDBC driver does not support batch updates.

Specified by: batchUpdate(...) in JdbcOperations
sql defining PreparedStatement that will be reused. All statements in the batch will use the same SQL.
pss object to set parameters on the PreparedStatement created by this method
an array of the number of rows affected by each statement
DataAccessException - if there is any problem issuing the update

Here is a reference code, where jdbcTemplate is already @Autowired.

public int[] insertPersons(List persons) {
    return jdbcTemplate.batchUpdate(
        "insert into staging.storage_insight_call_detail(first_name, middle_name, last_name) values(?, ?, ?)",
        new BatchPreparedStatementSetter() {

          public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setString(1, persons.get(i).firstName());
            ps.setString(2, persons.get(i).middleName());
            ps.setString(3, persons.get(i).lastName());
          public int getBatchSize() {
            return persons.size();

Read more

0 Getting the Generated Key using Spring's JdbcTemplate

Monday, October 21, 2019 Labels: , , , , , ,
It is pretty straight forward to have the generated primary key using simple JDBC, but what to do, when you are using Spring's JDBCTemplate.

This is how it's done:

KeyHolder keyHolder = new GeneratedKeyHolder();
        new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps =
                    connection.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, callString);
                ps.setString(2, storageInsightDTO.getMetadata().getRc());
                return ps;

 System.out.println("generated key: " + keyHolder.getKey());

1.  INSERT_SQL is your native SQL Insert Query

2. Statement.RETURN_GENERATED_KEYS is required to get the generated key.

I hope, you know basic Spring, if you come here and looking for the generated key, then above code is going to be very helpful for you.

Read more
Waqas Sadiq © 2014