Skip to content

Commit

Permalink
feat : delombok (#1287)
Browse files Browse the repository at this point in the history
* feat : rewrite with best practices

* fix : spotless format

* fix : tests and polish code

* fix : mapping issues

* feat : delombok

* fix format issue

* Update maven-wrapper.properties
  • Loading branch information
rajadilipkolli authored Jun 21, 2024
1 parent c4ef459 commit bd960e6
Show file tree
Hide file tree
Showing 19 changed files with 339 additions and 128 deletions.
26 changes: 4 additions & 22 deletions jpa/boot-hibernate2ndlevelcache-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
Expand All @@ -68,11 +64,6 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
Expand All @@ -86,6 +77,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down Expand Up @@ -166,26 +161,13 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- depending on your project -->
<!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- other annotation processors -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<!-- This is needed when using Lombok 1.18.16 and above -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
<release>${java.version}</release>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,72 @@
package com.example.hibernatecache.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

@Data
@ConfigurationProperties("application")
public class ApplicationProperties {

@NestedConfigurationProperty private Cors cors = new Cors();

@Data
public static class Cors {
private String pathPattern = "/api/**";
private String allowedMethods = "*";
private String allowedHeaders = "*";
private String allowedOriginPatterns = "*";
private boolean allowCredentials = true;

public String getPathPattern() {
return pathPattern;
}

public Cors setPathPattern(String pathPattern) {
this.pathPattern = pathPattern;
return this;
}

public String getAllowedMethods() {
return allowedMethods;
}

public Cors setAllowedMethods(String allowedMethods) {
this.allowedMethods = allowedMethods;
return this;
}

public String getAllowedHeaders() {
return allowedHeaders;
}

public Cors setAllowedHeaders(String allowedHeaders) {
this.allowedHeaders = allowedHeaders;
return this;
}

public String getAllowedOriginPatterns() {
return allowedOriginPatterns;
}

public Cors setAllowedOriginPatterns(String allowedOriginPatterns) {
this.allowedOriginPatterns = allowedOriginPatterns;
return this;
}

public boolean isAllowCredentials() {
return allowCredentials;
}

public Cors setAllowCredentials(boolean allowCredentials) {
this.allowCredentials = allowCredentials;
return this;
}
}

public Cors getCors() {
return cors;
}

public ApplicationProperties setCors(Cors cors) {
this.cors = cors;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.example.hibernatecache.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
class Initializer implements CommandLineRunner {

private static final Logger log = LoggerFactory.getLogger(Initializer.class);

private final ApplicationProperties properties;

Initializer(ApplicationProperties properties) {
this.properties = properties;
}

@Override
public void run(String... args) {
log.info("Running Initializer.....");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.example.hibernatecache.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
class WebMvcConfig implements WebMvcConfigurer {
private final ApplicationProperties properties;

WebMvcConfig(ApplicationProperties properties) {
this.properties = properties;
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping(properties.getCors().getPathPattern())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "customers")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(
name = "customers",
uniqueConstraints = {
@UniqueConstraint(
name = "uc_customer_email",
columnNames = {"email"})
})
@Cache(region = "customerCache", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Customer {

Expand All @@ -43,4 +42,70 @@ public class Customer {
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private List<Order> orders = new ArrayList<>();

public Long getId() {
return id;
}

public Customer setId(Long id) {
this.id = id;
return this;
}

public String getFirstName() {
return firstName;
}

public Customer setFirstName(String firstName) {
this.firstName = firstName;
return this;
}

public String getLastName() {
return lastName;
}

public Customer setLastName(String lastName) {
this.lastName = lastName;
return this;
}

public String getEmail() {
return email;
}

public Customer setEmail(String email) {
this.email = email;
return this;
}

public String getPhone() {
return phone;
}

public Customer setPhone(String phone) {
this.phone = phone;
return this;
}

public List<Order> getOrders() {
return orders;
}

public Customer setOrders(List<Order> orders) {
this.orders = orders;
return this;
}

public Customer addOrder(Order order) {
this.orders.add(order);
order.setCustomer(this);
return this;
}

public Customer removeOrder(Order order) {
this.orders.remove(order);
order.setCustomer(null);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.proxy.HibernateProxy;

@Entity
@Table(name = "orders")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Cacheable
@Cache(region = "orderCache", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Order {
Expand All @@ -51,6 +43,51 @@ public class Order {
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private List<OrderItem> orderItems = new ArrayList<>();

public Long getId() {
return id;
}

public Order setId(Long id) {
this.id = id;
return this;
}

public String getName() {
return name;
}

public Order setName(String name) {
this.name = name;
return this;
}

public BigDecimal getPrice() {
return price;
}

public Order setPrice(BigDecimal price) {
this.price = price;
return this;
}

public Customer getCustomer() {
return customer;
}

public Order setCustomer(Customer customer) {
this.customer = customer;
return this;
}

public List<OrderItem> getOrderItems() {
return orderItems;
}

public Order setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
return this;
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
Expand Down
Loading

0 comments on commit bd960e6

Please sign in to comment.