我不明白这个错误我已经变得非常令人沮丧......我试图使用DataTables将数据显示到具有oneToMany关系的表中,这是唯一的方法视图中显示的数据是通过将关系设置为null,这样上面的错误消失但我不知道如何根据需要显示数据。提前谢谢。
在用户中:
@JoinColumn(name = "id_rol", referencedColumnName = "id_rol", foreignKey = @ForeignKey(name = "fk_di"))
@ManyToOne(optional = false)
private Rol rol;
@GetMapping("/users/pag")
@ResponseBody
public String usersPaginate(HttpServletRequest request, HttpServletResponse response) {
DataTableRequest<User> dataTableInRQ = new DataTableRequest<User>(request);
PaginationCriteria pagination = dataTableInRQ.getPaginationRequest();
String baseQuery = "SELECT id_user, username, name, lastname, password, id_rol, (SELECT COUNT(1) FROM user) AS total_records FROM user ORDER BY lastname ASC";
String paginatedQuery = AppUtil.buildPaginatedQuery(baseQuery, pagination);
Query query = entityManager.createNativeQuery(paginatedQuery, User.class);
@SuppressWarnings("unchecked")
List<User> usersList = query.getResultList();
/*
for (int i = 0; i<usersList.size(); i++){
usersList.get(i).setRol(null);
}*/
DataTableResults<User> dataTableResult = new DataTableResults<User>();
dataTableResult.setDraw(dataTableInRQ.getDraw());
dataTableResult.setListOfDataObjects(usersList);
if (!AppUtil.isObjectEmpty(usersList)) {
dataTableResult.setRecordsTotal(usersList.get(0).getTotalRecords().toString());
if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) {
dataTableResult.setRecordsFiltered(usersList.get(0).getTotalRecords().toString());
} else {
dataTableResult.setRecordsFiltered(Integer.toString(usersList.size()));
}
}
return new Gson().toJson(dataTableResult);
}
$(document)
.ready(
function() {
var table = $('#paginatedTable')
.DataTable(
{
initComplete : function() {
var input = $(
'.dataTables_filter input')
.unbind(), self = this
.api(), $searchButton = $(
'#search')
.click(
function() {
self
.search(
input
.val())
.draw();
})
},
"processing" : true,
"serverSide" : true,
"pageLength" : 10,
"lengthChange" : false,
"columnDefs" : [
{
targets : '_all',
orderable : false
},
{
targets : [ 0, 3, 4, 5, 6 ],
"className" : "text-center"
},
{
targets : [ 3, 4, 5, 6, 7 ],
searchable : false
},
{
targets : [ 6 ],
render : function(data,
type, row) {
return data == true ? 'Habilitado'
: 'Deshabilitado'
}
},
{
targets : [ 4 ],
render : function(data,
type, row) {
return moment(data)
.format(
"DD/MM/YYYY");
}
},
{
targets : [ 7 ],
data : "idUser",
render : function(data,
type, row, meta) {
return '<div class="btn-group"><a href="/users/ed/'
+ data
+ '"><img class="btnEdit" src="/images/edit.png"></a></div>';
}
} ],
"ajax" : {
"url" : "/users/pag",
"data" : function(data) {
}
},
"columns" : [ {
"data" : "username"
}, {
"data" : "lastname"
}, {
"data" : "name"
}, {
"data" : "rol.tipoRol",
render : function(data, type, row) {
return data || '';
}
}]
});
});
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:982) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [tomcat-embed-core-8.5.16.jar:8.5.16]
2018-01-18 00:00:11.508 ERROR 9160 --- [nio-8091-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError: null
at java.util.Calendar.get(Calendar.java:1826) ~[na:1.8.0_144]
at java.text.SimpleDateFormat.subFormat(SimpleDateFormat.java:1119) ~[na:1.8.0_144]
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:966) ~[na:1.8.0_144]
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936) ~[na:1.8.0_144]
at java.text.DateFormat.format(DateFormat.java:345) ~[na:1.8.0_144]
答案 0 :(得分:0)
如果有人对解决方案感兴趣:
我将此课程添加到我的项目GraphAdapterBuilder,并更改了我的控制器的最后一行:
GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder().addType(User.class).registerOn(gsonBuilder);
Gson gson = gsonBuilder.create();
return gson;
这样我就得到了这个Json,并提供了所有需要的信息:
{"draw":"1","recordsFiltered":"3","recordsTotal":"3","data":[{"0x1":{"idUser":3,"username":"grtyg","name":"","lastname":"","totalRecords":3,"rol":{"idRol":2,"rol":"User ad"}}}}]}
我可以简单地在Rol实体中使用javax @Transient注释,如下所示:
@Transient
@OneToMany(cascade = CascadeType.ALL, mappedBy = "rol")
private Set<User> users;
但稍后我需要使用它。