Commit 46bec59c authored by Abhishek Mondal's avatar Abhishek Mondal

Changes for Stored Proc Call

parent ec3fc6de
...@@ -34,11 +34,16 @@ ...@@ -34,11 +34,16 @@
<version>1.18.8</version> <version>1.18.8</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- MySQL -->
<dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--<dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
......
package com.spring.data.jpa.datajpa.controller; package com.spring.data.jpa.datajpa.controller;
import com.spring.data.jpa.datajpa.entity.Customer; import com.spring.data.jpa.datajpa.entity.Customer;
import com.spring.data.jpa.datajpa.exception.BusinessException;
import com.spring.data.jpa.datajpa.implementation.CutomCustomerRepoImlp;
import com.spring.data.jpa.datajpa.repository.CustomCustomerRepository;
import com.spring.data.jpa.datajpa.repository.CustomerRepository; import com.spring.data.jpa.datajpa.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
...@@ -10,10 +13,14 @@ import org.springframework.data.domain.Sort; ...@@ -10,10 +13,14 @@ import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.websocket.server.PathParam; import javax.websocket.server.PathParam;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
...@@ -43,9 +50,15 @@ public class CustomerController { ...@@ -43,9 +50,15 @@ public class CustomerController {
return new ResponseEntity(customer.get(), HttpStatus.OK); return new ResponseEntity(customer.get(), HttpStatus.OK);
} }
@PostMapping(value = "/addCustomers" , consumes = MediaType.APPLICATION_JSON_VALUE , produces = MediaType.APPLICATION_JSON_VALUE) @PostMapping(value = "/addCustomers", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> addCustomers(@RequestBody List<Customer> customer) { public ResponseEntity<String> addCustomers(@RequestBody List<Customer> customer) throws BusinessException {
try {
customerRepository.saveAll(customer); customerRepository.saveAll(customer);
} catch (UnexpectedRollbackException ex) {
if (ex.getMostSpecificCause() instanceof SQLIntegrityConstraintViolationException) {
throw new BusinessException("constraint violation", ex);
}
}
return new ResponseEntity("Customers Added Successfully", HttpStatus.CREATED); return new ResponseEntity("Customers Added Successfully", HttpStatus.CREATED);
} }
...@@ -103,15 +116,21 @@ public class CustomerController { ...@@ -103,15 +116,21 @@ public class CustomerController {
return new ResponseEntity(customerList, HttpStatus.OK); return new ResponseEntity(customerList, HttpStatus.OK);
} }
@GetMapping(value = "/updateLastNameById") @PutMapping(value = "/updateLastNameById")
public ResponseEntity<String> updateLastNameById(@PathParam("lastName") String lastName, @PathParam("id") Long id) { public ResponseEntity<String> updateLastNameById(@PathParam("lastName") String lastName, @PathParam("id") Long id) {
int updateCount = customerRepository.updateLastNameById(lastName, id); int updateCount = customerRepository.updateLastNameById(lastName, id);
return new ResponseEntity(updateCount + " Customer Updated.", HttpStatus.OK); return new ResponseEntity(updateCount + " Customer Updated.", HttpStatus.OK);
} }
@DeleteMapping(value = "/deleteById/{id}") @DeleteMapping(value = "/deleteById/{id}")
public ResponseEntity<String> deleteById(@PathVariable Long id) { public ResponseEntity<String> deleteById(@PathVariable Long id) throws BusinessException {
try {
customerRepository.deleteById(id); customerRepository.deleteById(id);
} catch (UnexpectedRollbackException ex) {
if (ex.getMostSpecificCause() instanceof SQLException) {
throw new BusinessException("Update failed due to :: ", ex);
}
}
return new ResponseEntity("Customer Deleted Successfully...", HttpStatus.OK); return new ResponseEntity("Customer Deleted Successfully...", HttpStatus.OK);
} }
...@@ -157,5 +176,18 @@ public class CustomerController { ...@@ -157,5 +176,18 @@ public class CustomerController {
return new ResponseEntity(customerList.toList(), HttpStatus.OK); return new ResponseEntity(customerList.toList(), HttpStatus.OK);
} }
@GetMapping(value = "fetchAllCustomerBySP")
public ResponseEntity<String> fetchAllCustomerBySP() {
List<Object[]> objectMap = customerRepository.fetchAllCustomers();
System.out.println("objectMap :: " +objectMap.size());
return new ResponseEntity("kjs" ,HttpStatus.OK);
}
@GetMapping(value = "getAllCustomersBySP")
public ResponseEntity<List<Customer>> getAllCustomersBySP() {
List<Customer> customerList = customerRepository.getAllCustomers();
return new ResponseEntity(customerList ,HttpStatus.OK);
}
} }
...@@ -3,6 +3,7 @@ package com.spring.data.jpa.datajpa.entity; ...@@ -3,6 +3,7 @@ package com.spring.data.jpa.datajpa.entity;
import lombok.*; import lombok.*;
import javax.persistence.*; import javax.persistence.*;
import java.io.Serializable;
@Entity @Entity
@Getter @Getter
...@@ -10,9 +11,11 @@ import javax.persistence.*; ...@@ -10,9 +11,11 @@ import javax.persistence.*;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@ToString @ToString
@Data
@Table(name = "Customer") @Table(name = "Customer")
@NamedQuery(name = "Customer.findByFirstName" , query = "SELECT c from Customer c where c.firstName = ?1") @NamedQuery(name = "Customer.findByFirstName" , query = "SELECT c from Customer c where c.firstName = ?1")
public class Customer { @NamedStoredProcedureQuery(name = "fetchAllCustomers" , procedureName = "FETCH_CUSTOMERS")
public class Customer implements Serializable {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
......
package com.spring.data.jpa.datajpa.exception;
public class BusinessException extends Exception {
public BusinessException(String message, Exception ex) {
super(message, ex);
}
}
package com.spring.data.jpa.datajpa.implementation;
import com.spring.data.jpa.datajpa.entity.Customer;
import com.spring.data.jpa.datajpa.repository.CustomCustomerRepository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;
import java.util.List;
public class CutomCustomerRepoImlp implements CustomCustomerRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<Customer> getAllCustomers() {
StoredProcedureQuery findProcedure = entityManager.createNamedStoredProcedureQuery("fetchAllCustomers");
return findProcedure.getResultList();
}
}
package com.spring.data.jpa.datajpa.repository;
import com.spring.data.jpa.datajpa.entity.Customer;
import java.util.List;
public interface CustomCustomerRepository {
List<Customer> getAllCustomers();
}
...@@ -7,20 +7,23 @@ import org.springframework.data.domain.Slice; ...@@ -7,20 +7,23 @@ import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@Repository @Repository
@Transactional @Transactional
public interface CustomerRepository extends CrudRepository<Customer, Long>, PagingAndSortingRepository<Customer, Long>, JpaRepository<Customer, Long> { public interface CustomerRepository extends CrudRepository<Customer, Long>, CustomCustomerRepository, PagingAndSortingRepository<Customer, Long>, JpaRepository<Customer, Long> {
//Custom Query Methods //Custom Query Methods
List<Customer> findByLastName(String lastName); List<Customer> findByLastName(String lastName);
...@@ -77,8 +80,12 @@ public interface CustomerRepository extends CrudRepository<Customer, Long>, Pagi ...@@ -77,8 +80,12 @@ public interface CustomerRepository extends CrudRepository<Customer, Long>, Pagi
//Pagination 2 //Pagination 2
List<Customer> findAllByLastName(String lastName, Pageable pageable); List<Customer> findAllByLastName(String lastName, Pageable pageable);
//Sorting with Pagination 1 //Stored Procedures Call
@Procedure(name = "fetchAllCustomers")
List<Object[]> fetchAllCustomers();
//Stored Procedures Call
@Query(nativeQuery = true, value = "call FETCH_CUSTOMERS")
List<Customer> getAllCustomers();
} }
...@@ -4,3 +4,27 @@ spring.jpa.properties.hibernate.format_sql=true ...@@ -4,3 +4,27 @@ spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
#H2 DB Config
#spring.datasource.url=jdbc:h2:mem:testdb
#spring.datasource.driverClassName=org.h2.Driver
#spring.datasource.username=sa
#spring.datasource.password=
#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# Enabling H2 Console
#spring.h2.console.enabled=true
# Custom H2 Console URL
#spring.h2.console.path=/h2/console
# Whether to enable trace output.
#spring.h2.console.settings.trace=false
# Whether to enable remote access.
#spring.h2.console.settings.web-allow-others=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/testDB
spring.datasource.username=root
spring.datasource.password=adminroot
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment