devxlogo

Java

Finding the Current Flush Mode for the JPA EntityManager

Knowing the current flush mode can be done as follows: // via EntityManagerentityManager.getFlushMode();// in Hibernate JPA, via Session(entityManager.unwrap(Session.class)).getFlushMode();// starting with Hibernate 5.2(entityManager.unwrap(Session.class)).getHibernateFlushMode();

Creating a Secure Socket

Java supports creating of ServerSocket(s) in more than one way. This is an important aspect of security. SSLServerSocketFactory is a secure way of achieving this. This ensures that the socket is

How to Disable Auto-commit Mode in Spring Boot

By default, Spring Boot uses HikariCP as the connection pool. Via the connection pool, we can disable the auto-commit mode. For example, the following setting disabled auto-commit mode from application.properties:

How to Cache Query Results

If em is the EntityManager, then the following query results will be cached: Query query = em .createQuery(“SELECT u FROM User u”); query.setHint(“eclipselink.cache-usage”, “CheckCacheThenDatabase”);

How to Join (Combine) Fix File Paths

In order to join (combine) fix file paths we can use Path#resolve() and Path#resolveSibling():. // using Path#resolve()Path base1 = Paths.get(“D:/learning/books”); Path path1 = base1.resolve(“Java.pdf”);// using Path#resolveSibling()Path base2 = Paths.get(“D:/learning/books/Java.pdf”);Path path3

How to Sort a Collection

An approach for sorting a Java collection can rely on Collections.sort() method as in the following example: List names = new ArrayList(); names.add(“John”); names.add(“Alicia”); names.add(“Tom”); Collections.sort(names); // sort ascending by

Connect to a Database in Spring Boot

Instructing Spring Boot to connect to a database can be done in application.properties via the JDBC URL, user and password as in the following example: spring.datasource.url=jdbc:mysql://localhost:3306/mydb?createDatabaseIfNotExist=truespring.datasource.username=rootspring.datasource.password=root

How to Convert a List to an Array

One way to convert a list to an array is shown below: List list = Arrays.asList(1, 2, 3);Integer[] array = new Integer[list.size()];array = list.toArray(array);