Skip to content

Commit fe59fbd

Browse files
committed
Fixed Totol Records Issue
1 parent 6b6dd13 commit fe59fbd

4 files changed

Lines changed: 40 additions & 24 deletions

File tree

spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class BaseController {
5252
@RequestMapping(value="/", method = RequestMethod.GET)
5353
public ModelAndView home(@RequestParam(value = "name", defaultValue = "World") String name) {
5454
ModelAndView mv = new ModelAndView("index");
55-
mv.addObject("userModel", new User());
55+
mv.addObject("userModel", new UserModel());
5656
List<UserModel> userList = genericRepo.getUserModel();
5757
mv.addObject("userlist", userList);
5858
return mv;
@@ -70,17 +70,17 @@ public String listUsersPaginated(HttpServletRequest request, HttpServletResponse
7070
DataTableRequest<User> dataTableInRQ = new DataTableRequest<User>(request);
7171
PaginationCriteria pagination = dataTableInRQ.getPaginationRequest();
7272

73-
String baseQuery = "SELECT id as id, name as name, salary as salary, (SELECT COUNT(1) FROM USER) AS totalrecords FROM USER";
73+
String baseQuery = "SELECT id as id, name as name, salary as salary, (SELECT COUNT(1) FROM USER) AS total_records FROM USER";
7474
String paginatedQuery = AppUtil.buildPaginatedQuery(baseQuery, pagination);
7575

7676
System.out.println(paginatedQuery);
7777

78-
Query query = entityManager.createNativeQuery(paginatedQuery, User.class);
78+
Query query = entityManager.createNativeQuery(paginatedQuery, UserModel.class);
7979

8080
@SuppressWarnings("unchecked")
81-
List<User> userList = query.getResultList();
81+
List<UserModel> userList = query.getResultList();
8282

83-
DataTableResults<User> dataTableResult = new DataTableResults<User>();
83+
DataTableResults<UserModel> dataTableResult = new DataTableResults<UserModel>();
8484
dataTableResult.setDraw(dataTableInRQ.getDraw());
8585
dataTableResult.setListOfDataObjects(userList);
8686
if (!AppUtil.isObjectEmpty(userList)) {
@@ -97,13 +97,18 @@ public String listUsersPaginated(HttpServletRequest request, HttpServletResponse
9797
}
9898

9999
@RequestMapping(value="/adduser", method=RequestMethod.POST)
100-
public String addUser(@ModelAttribute User userModel, Model model) {
100+
public String addUser(@ModelAttribute UserModel userModel, Model model) {
101101
if(null != userModel) {
102102

103103
if(!AppUtil.isObjectEmpty(userModel.getId()) &&
104104
!AppUtil.isObjectEmpty(userModel.getName()) &&
105105
!AppUtil.isObjectEmpty(userModel.getSalary())) {
106-
userRepo.save(userModel);
106+
107+
User u = new User();
108+
u.setId(userModel.getId());
109+
u.setName(userModel.getName());
110+
u.setSalary(userModel.getSalary());
111+
userRepo.save(u);
107112
}
108113
}
109114
return "redirect:/";

spring-boot-datatable/src/main/java/com/opencodez/domain/User.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44
package com.opencodez.domain;
55

6-
import javax.persistence.Column;
76
import javax.persistence.Entity;
87
import javax.persistence.Id;
98

@@ -18,9 +17,7 @@ public class User {
1817
private Long id;
1918
private String name;
2019
private String salary;
21-
22-
@Column(name="totalrecords", updatable=false, insertable=false)
23-
private Integer totalRecords;
20+
2421

2522
/**
2623
* @return the id
@@ -58,16 +55,16 @@ public String getSalary() {
5855
public void setSalary(String salary) {
5956
this.salary = salary;
6057
}
61-
/**
62-
* @return the totalRecords
63-
*/
64-
public Integer getTotalRecords() {
65-
return totalRecords;
66-
}
67-
/**
68-
* @param totalRecords the totalRecords to set
69-
*/
70-
public void setTotalRecords(Integer totalRecords) {
71-
this.totalRecords = totalRecords;
72-
}
58+
// /**
59+
// * @return the totalRecords
60+
// */
61+
// public Integer getTotalRecords() {
62+
// return totalRecords;
63+
// }
64+
// /**
65+
// * @param totalRecords the totalRecords to set
66+
// */
67+
// public void setTotalRecords(Integer totalRecords) {
68+
// this.totalRecords = totalRecords;
69+
// }
7370
}

spring-boot-datatable/src/main/java/com/opencodez/domain/UserModel.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class UserModel {
1818
private String name;
1919
private String salary;
2020

21+
private Integer totalRecords;
2122

2223
/**
2324
* @return the id
@@ -55,4 +56,17 @@ public String getSalary() {
5556
public void setSalary(String salary) {
5657
this.salary = salary;
5758
}
59+
/**
60+
* @return the totalRecords
61+
*/
62+
public Integer getTotalRecords() {
63+
return totalRecords;
64+
}
65+
/**
66+
* @param totalRecords the totalRecords to set
67+
*/
68+
public void setTotalRecords(Integer totalRecords) {
69+
this.totalRecords = totalRecords;
70+
}
71+
5872
}

spring-boot-datatable/src/main/java/com/opencodez/repo/GenericRepoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class GenericRepoImpl implements GenericRepo {
2929
@Override
3030
public List<UserModel> getUserModel() {
3131

32-
String qry = "SELECT id as id, name as name, salary as salary FROM USER";
32+
String qry = "SELECT id as id, name as name, salary as salary, 1 as total_records FROM USER";
3333
Query query = entityManager.createNativeQuery(qry,
3434
UserModel.class);
3535

0 commit comments

Comments
 (0)