Friday, March 31, 2023

Spring Data JPA Query Methods

 Spring Data JPA Queries are a great way to create queries derived from the method name without needing to write tons of boiler-plate code to create connection & query, execute it and then process the result set. This generally works fine for simple queries and method names are very explicit in pointing out what is being returned by that method so the code is more readable and maintainable. To make this work successfully, the method name has to follow certain conventions:

  1. Method names usually start with the word "find"
  2. Filter clauses are added to the function name as "findBy<ColumnName>" which creates queries of the form "where <ColumnName> = ?"
  3. Multiple Filter clauses are supported using And / Or clauses 
    1. And can be used as "findBy<Col1>And<Col2>"  which creates the query "where <Col1> = ? and <Col2> = ?"
    2. Or can be used as "findBy<Col1>Or<Col2>"  which creates the query "where <Col1> = ? or <Col2> = ?"
  4. Similarly other operators are supported on the lines of Between, After, Before, LessThan, GreaterThan, Like, etc
  5. Ordering of the results can be delegated to the database by adding "OrderBy" to the function name as "findBy<Col1>OrderBy<Col2><Asc|Desc><Col3><Asc|Desc>"
Here is a handy table of supported keywords inside method names taken from the Spring Data documentation:

KeywordSampleJPQL snippet

Distinct

findDistinctByLastnameAndFirstname

select distinct …​ where x.lastname = ?1 and x.firstname = ?2

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

IsEquals

findByFirstname,findByFirstnameIs,findByFirstnameEquals

… where x.firstname = ?1

Between

findByStartDateBetween

… where x.startDate between ?1 and ?2

LessThan

findByAgeLessThan

… where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

… where x.age <= ?1

GreaterThan

findByAgeGreaterThan

… where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

… where x.age >= ?1

After

findByStartDateAfter

… where x.startDate > ?1

Before

findByStartDateBefore

… where x.startDate < ?1

IsNullNull

findByAge(Is)Null

… where x.age is null

IsNotNullNotNull

findByAge(Is)NotNull

… where x.age not null

Like

findByFirstnameLike

… where x.firstname like ?1

NotLike

findByFirstnameNotLike

… where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

… where x.firstname like ?1 (parameter bound with appended %)

EndingWith

findByFirstnameEndingWith

… where x.firstname like ?1 (parameter bound with prepended %)

Containing

findByFirstnameContaining

… where x.firstname like ?1 (parameter bound wrapped in %)

OrderBy

findByAgeOrderByLastnameDesc

… where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

… where x.lastname <> ?1

In

findByAgeIn(Collection<Age> ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection<Age> ages)

… where x.age not in ?1

True

findByActiveTrue()

… where x.active = true

False

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstname) = UPPER(?1)


Saturday, January 21, 2023

Piping output of top through grep does not give any output

Recently I was trying to grep for "load average" in the output of top and then redirect it to a file to save for later analysis - however the output file would be totally empty for quite some time. That is the time I realized that grep was buffering its output causing the output file to be empty. So the grep command was not writing to the file immediately but was collecting large amounts of data in its buffer before flushing that out to the file.

My original command was

top -b -u myusername -d 5 | grep -i 'load average' > /tmp/load_averages.log

Because this did not write anything to the output file for quite a while, I had to change it buffer only till it saw a newline in the output and then to flush out whatever it has gathered so far in its buffer as below:

top -b -u myusername -d 5 | grep -i 'load average' --line-buffered > /tmp/load_averages.log

And lo behold, tailing the output file shows the load averages being printed every 5 seconds (because of the -d 5 flag given to the top command)

Saturday, December 31, 2022

Extract a range of lines from a file on Linux

Say you have a huge file, but want to extract only a specific range of lines from that file into another one. There are many ways to do this, but a very simple way would be to use sed as follows:

sed -n '$start_line,$end_linep;$end_lineq' input_file.txt > output_file.txt

This will print all lines from the start_line to end_line and stop processing once it reaches end_line.
Note that the p in the above command makes sed print the line and the q makes sed end once the specific line is processed. Since we have the print command inside the quotes, we suppress automatic printing of pattern space by specifying the -n switch. For example, to extract lines 1500 - 1700, we can use this command:

sed -n '1500,1700p;1700q' input_file.txt > output_file.txt


Another simple method is to use a combination of head and tail as follows:

head -n +$end_line input_file.txt | tail -n +$start_line > output_file.txt

Here the + before the line numbers tells the head to output up to that line number and for tail to output starting at that line number. So our command above uses head to get all lines till the last required line and then uses tail to get all lines including and following the required first line so we do not need to do any calculations in our head. For example, to extract lines 1500 - 1700, we can use this command:

head -n +1700 input_file.txt | tail -n +1500 > output_file.txt

Friday, December 30, 2022

Disable automatic backslash insertion before $ when using auto-complete in bash

When using bash that is not configured properly, on using tab to auto-complete paths, it automatically escapes dollar symbols by inserting a backslash symbol before them, causing the commands to fail if run after auto-completion. 

For example: 

ls -l $HOME_DIR/code/wo (TAB) becomes 
ls -l \$HOME_DIR/code/workspace/ 

To stop this from occurring, you need to add this command to your  ~/.bashrc

shopt -u progcomp 

After this, source the .bashrc again and every time after you login, bash will not automatically add the backslash to escape $ symbol - viola - problem solved!

Thursday, May 6, 2021

Extract files from RPM package without installing

An RPM package is a file consisting of a cpio archive that contains the files to be installed and a header that contains metadata information about the package.
You can use the simple utility tool rpm2cpio to convert the contents of an RPM package into a cpio archive and then use the cpio command to extract the contents of that archive without needing to install the RPM package.

rpm2cpio name_of_rpm_package.rpm | cpio -idmv

Note that this will extract the files to the current folder.
Here is an explanation of the options used for the cpio command:

  • i: extract files
  • d: create leading directories where needed
  • m: preserve previous file modification times for the extracted files
  • v: verbose

Wednesday, May 5, 2021

Handling Exceptions in EJB using Interceptors

In my previous posts Using EJB Interceptors to add functionality to existing Beans, and Using EJB Interceptors to execute code AFTER service method completes, we saw how to use EJB3 Interceptors to add common concern features like logging, etc. to J2EE service methods.
In case you want to have common exception handling logic for all service method calls, you can wrap the return context.proceed(); in a try-catch block and handle the exception in the catch block as shown below:


package com.my.ejb.interceptors;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

public class MyInterceptor {

    @AroundInvoke
    public Object interceptorMethod(InvocationContext context) throws Exception {
    	try {
          System.out.println("Before invoking method: " + context.getMethod());
          return context.proceed();
        } catch (Exception e) { // You can catch ALL or ONLY Specific Exceptions as per your requirement
          System.out.println("Exception in method " + context.getMethod() + ": " + e.getMessage());
        } finally {
          System.out.println("After invoking method: " + context.getMethod());
        }
    }
}

Once this interceptor is created, we just need to add an "@Interceptors" annotation to the target EJB as:

import javax.interceptor.Interceptors;

@Interceptors({com.my.ejb.interceptors.MyInterceptor.class})

Now you should see log lines "Before ..." before the the method executes and "After..." after the method executes and in case of any exceptions in the metod call, you will see "Exception ..."


Tuesday, May 4, 2021

Using EJB Interceptors to execute code AFTER service method completes

In my previous post Using EJB Interceptors to add functionality to existing Beans, we saw how to use EJB3 Interceptors to add common concern features like logging, etc. to J2EE service methods.
In case you want to run any piece of code after each service method call, you can wrap the return context.proceed(); in a try-finally block and add the "after" code in the finally block as shown below:


package com.my.ejb.interceptors;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

public class MyInterceptor {

    @AroundInvoke
    public Object interceptorMethod(InvocationContext context) throws Exception {
    	try {
          System.out.println("Before invoking method: " + context.getMethod());
          return context.proceed();
        } finally {
          System.out.println("After invoking method: " + context.getMethod());
        }
    }
}

Once this interceptor is created, we just need to add an "@Interceptors" annotation to the target EJB as:

import javax.interceptor.Interceptors;

@Interceptors({com.my.ejb.interceptors.MyInterceptor.class})

Now you should see log lines "Before ..." before the the method executes and "After..." after the method executes!


Tuesday, November 10, 2020

Using EJB Interceptors to add functionality to existing Beans

To add any decorations or cross-cutting features like logging, profiling or performance measurement for service method calls of existing enterprise beans, you can use EJB3 Interceptors that were provided starting with JavaEE5.

Interceptor is a method that wraps around the invocation of the actual business method call allowing you to apply the required feature and can either exist in the target class if it is very speficic to that class; or in an external class if it applies to multiple service methods. If it is a part of an external class, then it has to be the only method in that class having the annotation "@AroundInvoke"

This interceptor method takes the following form:

package com.my.ejb.interceptors;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

public class MyInterceptor {

    @AroundInvoke
    public Object interceptorMethod(InvocationContext context) throws Exception {
        System.out.println("Going to invoke method: " + context.getMethod());
        return context.proceed();
    }
}

Once this interceptor is created, we just need to add an "@Interceptors" annotation to the target EJB as:

@Interceptors({com.my.ejb.interceptors.MyInterceptor.class})
And viola - it simply works, no need for any additionaly libraries or frameworks!

Note: Do not forget the return context.proceed(); call at the end of the interceptor method - this will allow the chain of interceptors to proceed and the target metod to be invoked!

Monday, November 9, 2020

Undo git stash clear

You can clear git stashes using the command:
git stash drop stash@{stash_index}
### OR
git stash clear
If you need to recover very recently deleted stashes, you can try the following two commands to try to recover them:

1. List all the available stashes that can be receovered using the below command:
git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merger --no-walk
2. Choose commit id of the stash that you want to restore and run the below command:
git stash apply [commit_id]

Wednesday, June 17, 2020

Syntax Highlighting Code in Blogger

I was using Syntax Highlighter created by Alex Gorbatchev to highlight code snippets on this blog, but of late, it was not working or taking too long to load up. 
So it was time again to figure out a new way to do this and there were a few options that work in a similar fashion:
The other option was to pre-format code and then paste it into your post:

I found Code Prettifier to be the simplest option that can get me closest to Syntax Highlighter and the setup is very easy - just need to add these lines to the theme HTML code:

<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<style>
li.L0, li.L1, li.L2, li.L3,
li.L5, li.L6, li.L7, li.L8 {
  list-style-type: decimal !important;
}
</style>

Once this is done, all you need to do is to put encoded code snippet in a pre block with the class set to prettyprint and viola!
Note that the above code has a style override which makes line numbers show up on all lines of code. Without that block of style code, only every fifth line of could would have a line number.

Here is an example of the pre block that you need to use to syntax highlight your code:
 <pre class="prettyprint linenums lang-sh"> ... your code ... </pre>

You don't need to specify the language since prettyPrint will guess it, however you can specify a language by specifying the language extension along with the prettyprint class. Languages supported out of the box are:
"bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html", "java",
"js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh", "xhtml", "xml","xsl"
You may also use the HTML 5 convention of embedding a <code> element inside the <pre> and using language-java style classes:
  <pre class="prettyprint"><code class="language-java">...</code></pre>
Note: Remember to escape your code using HTML entities :)

LinkWithin

Related Posts Plugin for WordPress, Blogger...