Posts

Showing posts from July, 2012

Creating Spring Boot MVC application with AWS DynamoDB in 10 mins

Image
AWS DynamoDB DB is a serverless NOSQL database. You can understand how to build a spring boot Java web MVC application (Game Leaderboard) reading a AWS DynamoDB in 10 mins. Source of the demo code: https://github.com/babysteps-topro/spring-mvc-dynamodb Command to run the project: mvn spring-boot:run Video explain the table design: https://youtu.be/V0GtrBfY7XM Prerequisite: Install the AWS CLI: https://youtu.be/pE-Q_4YXlR0 Video explain the how to create the table: https://youtu.be/sBZIVLlmpxY

Adding Hibernate native SQL features into your Spring Data Repository

JPA provides @NamedNativeQuery for you to use native SQL. However, the usage is not so convenient, especially when you need to map multiple entities in your native SQL. You have to define a set of SqlResultSetMapping mapping which is quite error prone. For those who have used Hibernate native SQL features before, you will find that it is much more easier to use than JPA's @NamedNativeQuery. In recent projects, I am using Spring Data JPA. I have added hibernate native query features to my Spring Data base repostory. You can now perform native query in JPA without SqlResultSetMapping. 1. Add you cusomt annotation @NativeQueries and @NativeQuery @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface NativeQueries { NativeQuery[] queries() default {}; } @Retention(RetentionPolicy.RUNTIME) public @interface NativeQuery { String name() default ""; String sql() default ""; } 2. Add the method "queryNatively" in base Spri

Inject SLF4J logger by annotation

Actually, the following class is not written by me. It is written my colleage. I know he is referencing from another post from Java Geeks . I found this is quite handy so I put it here as a reference. The main idea is make use of the BeanPostProcessors interface. We first create an "LoggableInjector" class which implement the BeanPostProcessors interface. This injector class gets the SLF4J logger and assign to the beans property after the bean creation by the Spring IOC continaer. 1. Create Loggable annotation @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @Documented public @interface Loggable { //for slf4j } 2. Create LoggableInjector class to add logger to the bean @Component public class LoggableInjector implements BeanPostProcessor { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } public Object postProcessBeforeInitialization(final Object bean, Str

Adding Hibernate Entity Level Filtering feature to Spring Data JPA Repository

Those who have used data filtering features of hibernate should know that it is very powerful. You could define a set of filtering criteria to an entity class or a collection. Spring data JPA is a very handy library but it does not have fitering features. In this post, I will demonstrate how to add the hibernate filter features at entity level. We can just use annotation in your repositoy interface to enable this features. Step 1. Define filter at entity level as usual. Just use hibernate @FilterDef annotation. @Entity @Table(name = "STUDENT") @FilterDef(name="filterBySchoolAndClass", parameters={@ParamDef(name="school", type="string"),@ParamDef(name="class", type="integer")}) public class Student extends GenericEntity implements Serializable { ... } Step2. Define two custom annotations. These two annotations are to be used in your repository interfaces. You could apply the hibernate filter defined in step 1 to spe

Customizing Spring Data JPA Repository

Spring Data is a very convenient library. However, as the project as quite new, it is not well featured. By default, Spring Data JPA will provide implementation of the DAO based on SimpleJpaRepository. In recent project, I have developed a customize repository base class so that I could add more features on it. You could add vendor specific features to this repository base class as you like. Configuration You have to add the following configuration to you spring beans configuration file. You have to specified a new repository factory class. We will develop the class later. <jpa:repositories base-package="example.borislam.dao" factory-class="example.borislam.data.springData.DefaultRepositoryFactoryBean/> Just develop an interface extending JpaRepository. You should remember to annotate it with @NoRepositoryBean. @NoRepositoryBean public interface GenericRepository <T, ID extends Serializable> extends JpaRepository<T, ID> { } Define

Welcome to Programming Peacefully!

I am an information technology professional from Hong Kong. In Hong Kong, it is so ridiculous that many IT professionals do not like programming. I had even heard that some analyst programmers said that they "hate" programming. Many of them will go to the area of project management and their technical skills may become outdated.  I keep this blog just want to share some of my experience in software development and to exchange knowledge from technology lovers all over the world. If you think that my programme is written badly, please feel free to comment on it. I always want to get improvement on my technical skills :). Apart from programming, I also love music. I like to play a musical instrument called "Ukulele". If you are interest in it, we can also share something about ukulele in this blog. :)