2017-04-13 68 views
0

我在spring項目中有一個JSP。我曾經使用Ajax的數據表獲取所有員工詳細信息。但是在加載頁面時我收到了異常。這是我的JSP,模型和控制器方法。糾正我的錯誤。數據表ajax請求中的異常

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@ page isELIgnored="false" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> 

<html> 

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>User and Employee Management</title> 
// Required script and Css link 
</head> 
<body> 
<div class="generic-container" style="width:97%; height:95%;"> 
    <%@include file="authheader.jsp" %> 
    <div class="panel panel-default"> 
      <!-- Default panel contents --> 
     <div class="panel-heading"><span class="lead">All Employees</span> 
    </div> 
    </div> 
    <div class="panel-heading"><span class="lead">Employee Details </span> 
    </div> 
    <div style="height:70%;overflow:auto;"> 
     <table class="table table-hover" style="overflow-x:auto" id="example"> 
      <thead> 
       <tr> 
        <th>Employee Reference Id</th> 
        <th>Employee Name</th> 
        <th>Employee Designation</th> 
        <th>Employee Salary</th> 
        <th>Address</th> 

       </tr> 
      </thead> 
     <tbody> 
</div> 
</body> 
<script type="text/javascript"> 
       $(document).ready(
         function() { 
          $("#example").DataTable({ 
               scrollY:  280, 
               scrollCollapse: true, 
               paging:   true, 
               "sAjaxSource" : "getEmployees", 
               "aoColumns" : [ 
                 { 
                  "mData" : "employeeReferenceId" 
                 }, 
                 { 
                  "mData" : "employeeName" 
                 }, 
                 { 
                  "mData" : "employeeDesg" 
                 }, 
                 { 
                  "mData" : "salary" 
                 },{ 
                  "mData" : "address.address" 
                 } 
               ] 
              }); 
         }); 
     </script> 

型號:僱員

@Entity 
@Table(name = "Employee") 
public class Employee implements Serializable{ 
private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue 
@Column(name = "EMPLOYEE_ID") 
private int id; 

@NotEmpty 
@Column(name = "EMPLOYEE_NAME") 
private String employeeName; 

@NotEmpty 
@Column(name = "EMPLOYEE_DESG") 
private String employeeDesg; 

@NotEmpty 
@Column(name = "SALARY") 
private String salary; 

@NotEmpty 
@Column(name = "EMPLOYEE_REFERENCE_ID") 
private String employeeReferenceId; 

@OneToOne(mappedBy="employee", cascade = CascadeType.ALL) 
private Address address; 
//Getters and Setters 
} 

型號:地址

@Entity 
@Table(name = "ADDRESS") 
public class Address { 

@Id 
@Column(name="ADDRESS_ID") 
@GeneratedValue(strategy=GenerationType.AUTO) 
private int id; 

@Column(name="emp_id",insertable = false, updatable = false) 
private int employeeId; 

@NotNull 
@Column(name = "ADDRESS") 
private String address; 
@OneToOne(fetch=FetchType.EAGER , cascade=CascadeType.ALL, orphanRemoval=true) 

@JoinColumn(name="emp_id",referencedColumnName="employee_id") 
private Employee employee; 
//Getters and Setters 
} 

和控制器的方法是

@RequestMapping(value = { "/getEmployees" }) 
public @ResponseBody Map<String, Object> getEmployees(ModelMap model) { 
    Map<String, Object> data = new HashMap<String, Object>(); 
    List<Employee> employees=employeeService.findAllEmployees(); 
    data.put("data", employees); 
    return data; 
} 

,誤差

Apr 13, 2017 5:58:25 PM 
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver 
handleHttpMessageNotWritable 
WARNING: Failed to write HTTP message: 
org.springframework.http.converter.HttpMessageNotWritableException: Could 
not write content: failed to lazily initialize a collection of role 
+0

也應該由stacktrace輸入引起。這將告訴究竟發生了什麼。看起來好像你在某個實體的某個地方有角色收集,而某些事情是不對的。 – user871199

回答

0

之所以得到這個例外是實體配置。因爲JSON正在將父實體映射到子實體。由於您已經使用關係父實體配置了子實體。因此,你正在獲得嵌套異常。

員工 - >地址(在Employee實體中再次配置員工)。可以通過Address對象中的以下注釋忽略此問題。

@JSONIgnore 
@JoinColumn(name="emp_id",referencedColumnName="employee_id") 
private Employee employee; 

@JSONIgnore註釋將在返回JSON對象時阻止嵌套異常。