Skip to main content

Posts

Showing posts from February, 2016

SpringBoot : How to use & configure SessionFactory Bean

Points To Remember Add the SessionFactory  bean in the Application class. Add the Current Session Context class in application.properties. Use the SessionFactory using  @Autowired  annotation. How to Configure & Use SessionFactory Bean Add the following to the Main Application class or Configuration class. view plain copy to clipboard print ? @Bean    public  SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){        return  hemf.getSessionFactory();   }   @Bean public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){ return hemf.getSessionFactory(); } Add the following line in application.properties spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext Now you can use the Session Factory in your code as follows. view plain copy to clipboard print ? class  Sample {           @Autowired       SessionFactory sessionFactory;          // use the session factory as         // sessionFact

Java : How to sort Array List based on Custom Order

How to sort Array List using Custom Order On many occasions we need to sort an array list or linked list based on custom logic. Suppose you have a Quiz application and you want to return questions based on a custom question id. e.g Request : ids = [ 88, 21, 43, 15, 64, 35 ] When you will fetch the Questions from Database you will be using a query something like below select * from question where id in (?) This will give you questions ordered by id by default. So to covert it in same order as requested order you can use view plain copy to clipboard print ? // ids = [ 88, 21, 43, 15, 64, 35 ]    // questions = new ArrayList<Question>();     Collections.sort(questions,  new  Comparator<Question>() {         @Override          public   int  compare(QuestionDto q1, QuestionDto q2) {             // compare based on the index of id in list **ids              return  ids.indexOf(q1.getId()) - ids.indexOf(q2.getId());        }    });   // ids = [ 88, 21, 43, 15, 64, 35 ]

SpringBoot : How to do Exception Handling in Rest Application

Points To Remember There are three ways to do exception handling in spring boot application. Global Level using -   @ControllerAdvice Controller Level using -  @ExceptionHandler   Method Level using -  try/catch    Difference between @ExceptionHandler and @ControllerAdvice How to do Exception Handling in Rest Application To demonstrate Exception Handling we will make three Exceptions. BaseException that will be parent of all the Custom Exceptions. CustomException1 that extends Base Exception. CustomException2 that extends Base Exception. We will create the following methods ex1() throws BaseException ex2() throws CustomException1 ex3() throws CustomException2 ex4() throws NullPointerException  ex5() throws NumberFormatException and now we will see how we can handle these exceptions using above mentioned ways. Way 1 : Global Exception Handling using @ControllerAdvice Add a class with annotation @ControllerAdvice and  @RestController.  This will make the class return a Rest Response. @Co

SpringBoot : How to disable the Application Banner

How to disable the Application Banner  You can disable the application banner using the following ways You can diable by adding following in application.properties file spring.main.web_environment=false spring.main.banner_mode=off You can disable the same by adding the following code to your main Application class. public static void main(String[] args) { new SpringApplicationBuilder() .showBanner(false) .sources(Application.class) .run(args); } By following any of the above steps you can disable the banner in the spring boot application. Note :: How to add Custom Banner in Spring Boot Application.

SpringBoot : Adding Custom Banner in Application

Adding Custom Banner in Spring Boot Application You can replace the default spring boot banner for your application to give a more meaningful name to it. Steps to add Custom Banner in application. Go to the link ASCII art generator  and generate an ascii art. Create a file named banner.txt  under resources folder. That's it, rerun your application and you will be able to see your new banner. Note :: name of the file should be banner.txt and placed in folder resources Note :: How to disable the banner in Spring boot Application.

Spring Boot : How to enable Hibernate SQL Logging using application.properties

How to enable Hibernate SQL Logging in Spring Boot using application.properties You can enable hibernate sql logging level to Debug. This will print the sql queries fired by hibernate. logging.level.org.hibernate.SQL=DEBUG Your complete SQL configuration may look like following spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mysql://localhost/ekiras spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver logging.level.org.hibernate.SQL=DEBUG