Sunday, December 8, 2019

Using Project Lomobok in Spring and JPA

   What is Project Lomobok
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

How to Configure
  • Go to https://projectlombok.org/mavenrepo/
  • Copy latest dependency for this project and paste into your project’s pom.xml
  • Let intelij (Or your preferd IDE) download the dependency
  • Enable lombok plugin in intelij

How To Use
  •    Open a bean
  •     Refactor à Lombok as @Data and notice the code getting refactored.
  • What does @Data do while using Lombo
o   It adds
§  Getters and setters
§  HashCode
§  Equals
Some Tips For Using LOMBOK and JPA
  •   /**

 * Having bydirectional relationshps in a n entity and using lambok with it creates
        problem for hashcoding so we will have to exclude the bidirectional
 memebers of this class

 * while using lombokl

 */

@EqualsAndHashCode(exclude = "recipes")
 
  •  Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.spring5.springrecepie.domain.Category.recipes, could not initialize proxy - no Session

  • The above error was caused when there was no “fetch = FetchType.EAGER” defined 
  • Hence defining mapping as below resolved this problem
·       @ManyToMany(mappedBy = "categories",fetch = FetchType.EAGER)
private Set<Recipe> recipes;

Lombok Features And Annotations

  •   /*Lombok Annotations*/
    @Setter
    @Getter
    @EqualsAndHashCode
    (exclude = {"petType","owner","visitSet"})
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    /*Lombok Annotations*/
  • Visit below page to see all the available features

Video Tutorial
  •  Also you can watch the video given on below URL

No comments:

Post a Comment