Java blog for smart concepts and coding practices

  1. Spring transactions on non-public methods with load-time weaving

    In this tutorial, we will learn to apply transactions in any spring application on non-public methods (by deault spring AOP can only advise public methods of beans declared in the IoC container). Using this technique, you can manage transactions for nonpublic methods, or on any methods into objects which are created outside the Spring IoC […]

  2. How to Create Password Protected Zip Files in Java

    This tutorial covers creating password protected zip files using a very useful library zip4j. Java, by default, does not provide any support for password protection for file; though it has very good API support for creating/extracting zip files. There are some other useful libraries out there, which are equally good and sometimes better than zip4j, […]

  3. Windows 7 – Set Environment Variables without Admin Access

    In this tutorial, we will learn to set environment variables e.g. JAVA_HOME when you don’t have admin access to your development machine. This is very common scenario in companies with high security requirements like banks ( like mine :-) ). Open “User Accounts” Option You can locate this option by searching ‘accounts‘ or ‘user accounts‘ […]

  4. How to Checkout Sourcecode from Google Code Projects

    If you go to Github projects, they provide you a link to download the sourcecode as a zip file (if you do not want to configure any git client). But in google code projects, you are not given this option is some of the projects and rather you are presented with SVN URL to checkout. […]

  5. Implement Phonetic search using Soundex algorithm

    Have you ever wondered how spell checkers, in any word editor, suggest you a list of probable other words whenever you have any spelling mistake?? This is done using phonetic search. Soundex is a phonetic algorithm for indexing names by sound, as pronounced in English. The goal is for homophones (pronounced the same as another […]

  6. How to left, right, or center align string in Java

    In this example, we will learn to align a string to left ,right or center. I have created a utility class StringAlignUtils.java, which wraps all the logic inside it, and provide you convenient methods which you can call directly. Sourcecode of StringAlignUtils StringAlignUtils class extends java.text.Format class. This help you in some cases where references […]

  7. [Solved] org.hibernate.QueryException: Cannot mix named and positional parameters

    You might face this exception if you are trying execute stored procedure using JPA 2.1 and hibernate’s entity manager, and you are not using the entity manager’s registerStoredProcedureParameter() correctly. Incorrect usage may look like below OR the one you have written. Exception will look like this: Solution To fix this error, use correctly the registerStoredProcedureParameter() […]

  8. [Solved] HsqlException: data exception: invalid character value for cast

    You might face this exception if you are trying execute any query OR stored procedure, and parameter are not set in correct order. It may happen that accidentally you are setting a String type column to a Integer type column. Exception will look like this: Solution To fix this error, make sure you are mapping […]

  9. Hibernate 4 + JPA 2.1 @NamedStoredProcedureQuery Example

    In this tutorial, I am giving an example of executing Stored Procedures in with the help of @NamedStoredProcedureQuery annotation. I am using HQLDB (in-memory database) for backend DB in this example. I will first create a stored procedure during start of application, then will run it using EntityManager.createStoredProcedureQuery() method. Table of Contents Maven Configuration Declaring […]

  10. Using SQL Scripts with Spring JDBC + JPA + HSQLDB

    In this tutorial, I am giving an example of using custom SQL scripts in spring application to initialize the database at startup of application with appropriate tables and data populated inside it. I am using HQLDB (in-memory database) for backend DB in this example. Table of Contents SQL Scripts Include Spring JDBC dependency Adding jdbc:initialize-database […]

Java blog for smart concepts and coding practices