Skip to content

Instantly share code, notes, and snippets.

@sohangp
sohangp / CustomerRepository.java
Last active May 16, 2019 00:58
JPA Repository
@Repository
public interface CustomerRepository extends JpaRepository<CustomerEntity, Integer> {
@Query(name = "customerEntity.getCustomerDetails", nativeQuery = true)
List<CustomerDetailsDTO> getCustomerDetails();
}
@sohangp
sohangp / CustomerEntity.java
Last active May 16, 2019 14:20
JPA entity for Customer
@Data
@Entity
@Table(name = "CUSTOMER")
public class CustomerEntity {
@Id
@Column(name = "ID")
private Integer id;
@Column(name = "FIRST_NAME")
@sohangp
sohangp / OrderEntity.java
Last active May 16, 2019 14:20
JPA Entities for Customer Order
@Data
@Entity
@Table(name = "CUSTOMER_ORDER")
public class OrderEntity {
@Id
@Column(name = "ID")
private Integer id;
@Column(name = "ORDER_NUMBER")
@sohangp
sohangp / CustomerDetailsDTO.java
Last active December 8, 2019 12:40
Projection DTO for Customer Details
public interface CustomerDetailsDTO {
Integer getCustomerId();
@Value("#{target.firstName + ' ' + target.lastName}")
String getCustomerName();
String getCity();
String getCountry();
@sohangp
sohangp / OrderDTO.java
Last active May 16, 2019 01:22
Order DTO that will be built with SPEL
@Component
public class MapperUtility {
public OrderDTO buildOrderDTO(Long orderNumber, Double totalAmount) {
OrderDTO order = new OrderDTO();
order.setOrderNumber(orderNumber);
order.setTotalAmount("$" + totalAmount);
return order;
}
}
@sohangp
sohangp / MapperUtility.java
Created May 15, 2019 18:59
Another component called for Object creation
@Component
public class MapperUtility {
public CustomerOrderDTO buildOrderDTO(Long orderNumber, Double totalAmount) {
CustomerOrderDTO customerOrderDTO = null;
if (orderNumber != null) {
customerOrderDTO = new CustomerOrderDTO();
customerOrderDTO.setOrderNumber(orderNumber);
customerOrderDTO.setTotalAmount("$" + totalAmount);
@sohangp
sohangp / orm.xml
Last active May 16, 2019 14:40
The Native Query
<named-native-query name="customerEntity.getCustomerDetails">
<query><![CDATA[
SELECT
cust.id as customerId,
cust.FIRST_NAME as firstName,
cust.LAST_NAME as lastName,
cust.CITY as city,
cust.COUNTRY as country,
corder.ORDER_NUMBER as orderNumber,
corder.TOTAL_AMOUNT as totalAmount
public List<CustomerDetailsDTO> getCustomersAndOrderData() throws Exception {
return customerRepository.getCustomerDetails();
}
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerRepository customerRepository;
public List<CustomerDetailsDTO> getCustomersAndOrderData() throws Exception {
return customerRepository.getCustomerDetails();
}
}
@RestController
public class CustomerController {
@RequestMapping(value = "/customers", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<CustomerDetailsDTO> getCustomers() throws Exception {
return customerService.getCustomersAndOrderData();
}
}