Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
Spring-Data-JPA
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abhishek Mondal
Spring-Data-JPA
Commits
46bec59c
Commit
46bec59c
authored
Feb 20, 2020
by
Abhishek Mondal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changes for Stored Proc Call
parent
ec3fc6de
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
119 additions
and
11 deletions
+119
-11
pom.xml
pom.xml
+6
-1
CustomerController.java
...pring/data/jpa/datajpa/controller/CustomerController.java
+38
-6
Customer.java
...ain/java/com/spring/data/jpa/datajpa/entity/Customer.java
+4
-1
BusinessException.java
.../spring/data/jpa/datajpa/exception/BusinessException.java
+7
-0
CutomCustomerRepoImlp.java
...ata/jpa/datajpa/implementation/CutomCustomerRepoImlp.java
+21
-0
CustomCustomerRepository.java
...data/jpa/datajpa/repository/CustomCustomerRepository.java
+9
-0
CustomerRepository.java
...pring/data/jpa/datajpa/repository/CustomerRepository.java
+10
-3
application.properties
src/main/resources/application.properties
+24
-0
No files found.
pom.xml
View file @
46bec59c
...
...
@@ -34,11 +34,16 @@
<version>
1.18.8
</version>
<scope>
provided
</scope>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
</dependency>
<!--<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependency>
-->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
...
...
src/main/java/com/spring/data/jpa/datajpa/controller/CustomerController.java
View file @
46bec59c
package
com
.
spring
.
data
.
jpa
.
datajpa
.
controller
;
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
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Page
;
...
...
@@ -10,10 +13,14 @@ import org.springframework.data.domain.Sort;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.transaction.UnexpectedRollbackException
;
import
org.springframework.web.bind.annotation.*
;
import
javax.websocket.server.PathParam
;
import
java.sql.SQLException
;
import
java.sql.SQLIntegrityConstraintViolationException
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.Future
;
...
...
@@ -43,9 +50,15 @@ public class CustomerController {
return
new
ResponseEntity
(
customer
.
get
(),
HttpStatus
.
OK
);
}
@PostMapping
(
value
=
"/addCustomers"
,
consumes
=
MediaType
.
APPLICATION_JSON_VALUE
,
produces
=
MediaType
.
APPLICATION_JSON_VALUE
)
public
ResponseEntity
<
String
>
addCustomers
(
@RequestBody
List
<
Customer
>
customer
)
{
@PostMapping
(
value
=
"/addCustomers"
,
consumes
=
MediaType
.
APPLICATION_JSON_VALUE
,
produces
=
MediaType
.
APPLICATION_JSON_VALUE
)
public
ResponseEntity
<
String
>
addCustomers
(
@RequestBody
List
<
Customer
>
customer
)
throws
BusinessException
{
try
{
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
);
}
...
...
@@ -103,15 +116,21 @@ public class CustomerController {
return
new
ResponseEntity
(
customerList
,
HttpStatus
.
OK
);
}
@
Ge
tMapping
(
value
=
"/updateLastNameById"
)
@
Pu
tMapping
(
value
=
"/updateLastNameById"
)
public
ResponseEntity
<
String
>
updateLastNameById
(
@PathParam
(
"lastName"
)
String
lastName
,
@PathParam
(
"id"
)
Long
id
)
{
int
updateCount
=
customerRepository
.
updateLastNameById
(
lastName
,
id
);
return
new
ResponseEntity
(
updateCount
+
" Customer Updated."
,
HttpStatus
.
OK
);
}
@DeleteMapping
(
value
=
"/deleteById/{id}"
)
public
ResponseEntity
<
String
>
deleteById
(
@PathVariable
Long
id
)
{
public
ResponseEntity
<
String
>
deleteById
(
@PathVariable
Long
id
)
throws
BusinessException
{
try
{
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
);
}
...
...
@@ -157,5 +176,18 @@ public class CustomerController {
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
);
}
}
src/main/java/com/spring/data/jpa/datajpa/entity/Customer.java
View file @
46bec59c
...
...
@@ -3,6 +3,7 @@ package com.spring.data.jpa.datajpa.entity;
import
lombok.*
;
import
javax.persistence.*
;
import
java.io.Serializable
;
@Entity
@Getter
...
...
@@ -10,9 +11,11 @@ import javax.persistence.*;
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
@Table
(
name
=
"Customer"
)
@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
@GeneratedValue
(
strategy
=
GenerationType
.
AUTO
)
private
Long
id
;
...
...
src/main/java/com/spring/data/jpa/datajpa/exception/BusinessException.java
0 → 100644
View file @
46bec59c
package
com
.
spring
.
data
.
jpa
.
datajpa
.
exception
;
public
class
BusinessException
extends
Exception
{
public
BusinessException
(
String
message
,
Exception
ex
)
{
super
(
message
,
ex
);
}
}
src/main/java/com/spring/data/jpa/datajpa/implementation/CutomCustomerRepoImlp.java
0 → 100644
View file @
46bec59c
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
();
}
}
src/main/java/com/spring/data/jpa/datajpa/repository/CustomCustomerRepository.java
0 → 100644
View file @
46bec59c
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
();
}
src/main/java/com/spring/data/jpa/datajpa/repository/CustomerRepository.java
View file @
46bec59c
...
...
@@ -7,20 +7,23 @@ import org.springframework.data.domain.Slice;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.data.jpa.repository.Modifying
;
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.PagingAndSortingRepository
;
import
org.springframework.data.repository.query.Param
;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.stereotype.Repository
;
import
org.springframework.transaction.annotation.Propagation
;
import
javax.transaction.Transactional
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.concurrent.Future
;
@Repository
@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
List
<
Customer
>
findByLastName
(
String
lastName
);
...
...
@@ -77,8 +80,12 @@ public interface CustomerRepository extends CrudRepository<Customer, Long>, Pagi
//Pagination 2
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
();
}
src/main/resources/application.properties
View file @
46bec59c
...
...
@@ -4,3 +4,27 @@ spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL
=
DEBUG
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment