【问题标题】:HTTP Status 500 - Servlet.init() for servlet Dispatch threw exception in spring hibernateHTTP 状态 500 - servlet Dispatch 的 Servlet.init() 在 spring hibernate 中抛出异常
【发布时间】:2014-08-21 15:43:48
【问题描述】:

您好,我正在编写一个 spring mvc hibernate 注释应用程序,我有 2 个表 "team" 和另一个表 "Employee"。这里我使用的是 one -to-one 映射和 mysql 数据库。在 Employee 表中,我保存员工记录,例如姓名、团队等。“团队” 表包含两个条目 teamid(主键)和 teamname。这里我使用下拉列表来选择 Employee.jsp 中的团队名称> page.当我删除团队名称时,我想从员工表中删除其所有引用。当我运行程序时,我收到以下错误

 HTTP Status 500 - Servlet.init() for servlet Dispatch threw exception in spring hibernate

详情

    javax.servlet.ServletException: Servlet.init() for servlet Dispatch threw exception
    root cause 

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addTeamController': Injection of autowired dependencies failed; 
    nested exception is org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: private com.resource.ResourceService.AddTeamService com.resource.ResourceController.AddTeamController.teamServices; 
    nested exception is org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'teamService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: private com.resource.ResourceDao.AddTeamDao com.resource.ResourceService.AddTeamServiceImpl.teamdao; 
    nested exception is org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'teamDao': Injection of autowired dependencies failed; 
    nested exception is org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: private org.hibernate.SessionFactory com.resource.ResourceDao.AddTeamImpl.sessiofactory; 
    nested exception is org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'sessionFactory' defined in ServletContext resource 
    [/WEB-INF/dispatch/Dispatch-servlet.xml]: Invocation of init method failed; 
    nested exception is org.hibernate.MappingException: Could not determine type for: com.resource.ResourceBean.AddTeam, at table: Employee, for columns: [org.hibernate.mapping.Column(teams)]

我的服务类dao类重复同样的错误。

数据库架构

员工表

CREATE TABLE `Employee` (  `empId` int(11) NOT NULL,  
 `empName` varchar(255) DEFAULT NULL,  `empExp` int(11) NOT NULL, 
 `empTeam`      varchar(255) DEFAULT NULL, `teamId` int(11) DEFAULT NULL, 
  PRIMARY KEY (`empId`));

团队表

    CREATE TABLE `Team` (  `teamId` int(11) NOT NULL,  
    `teamName` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`teamId`)  , KEY `FK901` (`teamId`),
   CONSTRAINT `FK901` FOREIGN KEY (`teamId`) REFERENCES `Employee` (`empId`));

AddTeam.java

 @Entity
@Table(name="Team")

public class AddTeam {


    @Id
    @OneToOne
    @Column(name="teamId")



    private Integer teamId;

    @Column(name="teamName")
    private String teamName;

    public Integer getTeamId() {
          return teamId;
         }

         public void setTeamId(Integer teamId) {
          this.teamId = teamId;
         }

         public String getTeamName() {
          return teamName;
         }

         public void setTeamName(String teamName) {
          this.teamName = teamName;
         }
              }

资源.java

@Entity
@Table(name="Employee")
public class Resource implements Serializable {

    private static final long serialVersionUID = -723583058586873479L;

    @Id
    @Column(name="empid")
    private Integer empId;

    @Column(name="empname")
    private String empName;

    @Column(name="empexp")
    private Integer empExp;



    @Column(name="empteam")
    private String empTeam;


     @JoinColumn(name="teamId")
     private AddTeam teams;



    public Integer getEmpId(){
        return empId;
    }
    public void setEmpId(Integer empId){
        this.empId=empId;
    }


    public String getEmpName(){
        return empName;
    }
    public void setEmpName(String empName){
        this.empName=empName;
    }

    public Integer getEmpExp(){
        return empExp;
    }
    public void setEmpExp(Integer empExp){
        this.empExp=empExp;
    }



    public String getEmpTeam(){
        return empTeam;
    }
    public void setEmpTeam(String empTeam){
        this.empTeam=empTeam;
    }


    public AddTeam getTeams() {
          return teams;
         }

         public void setTeams(AddTeam teams) {
          this.teams = teams;
         }

}

这是 ResourceDaoImpl.java 中使用的查询

@Override
    public void deleteResource(int resourceid) {
        // TODO Auto-generated method stub
        sessionfactory.getCurrentSession().createQuery("DELETE FROM Resource WHERE empid=" +resourceid).executeUpdate();

        }

AddTeamDaoImpl.java 中用于删除的查询

@Override
    public void deleteTeams(int teamid) {
        // TODO Auto-generated method stub
        sessiofactory.getCurrentSession().createQuery("DELETE FROM AddTeam WHERE teamid="+teamid).executeUpdate();

    }

ResourceController.java 中的删除函数

@RequestMapping(value="/delete",method=RequestMethod.GET)
    public ModelAndView deleteResourceDetails(@ModelAttribute("command")  Resource resource,
            BindingResult result){
        resourceServices.deleteResource(resource.getEmpId());
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("resourcekey", resourceServices.listResources());
        model.put("teamKey", addteamServices.listTeams());
        return new  ModelAndView("EditTeam",model);
    }

团队.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
      <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Emoloyee details</title>

<center>
<h2>Add Team Details</h2>
<form:form method="POST" action="Team.html">
   <table>
                <tr>
                    <td><form:label path="teamId">Team ID:</form:label></td>
                    <td><form:input path="teamId" id="demo" value="${team.teamId}"/></td>
                </tr>
                <tr>
                    <td><form:label path="teamName">Team Name:</form:label></td>
                    <td><form:input path="teamName" value="${team.teamName}"/></td>
                </tr>

                <tr>
                <tr>
                    <td>&nbsp;</td>
                  <td><input type="submit" value="SAVE"/></td>
                </tr>
            </table> 
        </form:form>
        <br/>
  <c:if test="${!empty teamKey}">
    <table align="center" border="1">
        <tr>
            <th>Category ID</th>
            <th>Category Name</th>
            <th>Options</th>
        </tr>

        <c:forEach items="${teamKey}" var="team">
            <tr>
                <td><c:out value="${team.teamId}"/></td>
                <td><c:out value="${team.teamName}"/></td>
                <td align="center"><a href="editTeam.html?teamId=${team.teamId}">Edit</a> |
                 <a href="deleteTeam.html?teamId=${team.teamId}">Delete</a></td>
            </tr>
        </c:forEach>
    </table>
</c:if>
<h2><a href="updateresource.html">Adding Publication</a></h2>


</center>



</body>
</html>

EmployeeDetail.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Resource Manager</title>
</head>
<body>

<center>
<h2>Add Resources</h2>
<form:form method="POST" action="save.html">
<table>
  <tr>
    <td><form:label path="empId">Employee Id</form:label></td>
    <td><form:input path="empId" id="demo" value= "${resource.empId }"/></td>
  </tr>
  <tr>
    <td><form:label path="empName">Name</form:label></td>
    <td><form:input path="empName" value="${resource.empName }"/></td>
  </tr>

  <tr>
    <td><form:label path="empExp">Experience</form:label></td>
    <td><form:input path="empExp" value="${resource.empExp }"/></td>
  </tr>


   <tr>
    <td><form:label path="empTeam">Team</form:label></td>
    <td><form:input path="empTeam" value="${resource.empTeam}"/></td>
  </tr>


    <tr>
                    <td>
                        <form:label path="teams.teamId">Team Name</form:label>
                    </td>
                    <td>
                        <form:select path="teams.teamId" cssStyle="width: 150px;">    
                            <option value="-1">Select a type</option>
                            <c:forEach items="${teamKey}" var="teams">
                            <option value="${teams.teamId}">${teams.teamName}</option>
                            </c:forEach>
                        </form:select>
                    </td>
                </tr>
                <tr>


   <td colspan="2"><input type="submit"value="Submit"></td>

    </tr>

 </table>
 </form:form>
<br/>

 <c:if test="${!empty resourcekey}">
    <table align="center" border="1">
        <tr>
            <th> Emp ID</th>
            <th>Emp Name</th>
            <th>Emp Exp</th>
            <th>Emp Team</th>
            <th>Emp TeamNmae</th>
            <th>Emp skills</th>
            <th>Options</th>
        </tr>

        <c:forEach items="${resourcekey}" var="resource">
            <tr>
                <td><c:out value="${resource.empId}"/></td>
                <td><c:out value="${resource.empName}"/></td>
                <td><c:out value="${resource.empExp}"/></td>
                <td><c:out value="${resource.empTeam}"/></td>
                <td><c:out value="${resource.teams.teamName}"/></td>



                <td align="center"><a href="editPublication.html?empId=${resource.empId}">Edit</a> | <a href="deleteResource.html?empId=${resource.empId}">Delete</a></td>
            </tr>
        </c:forEach>
    </table>
</c:if>

    <h2><a href="addTeam.html">Adding Category</a></h2>
</center>
</body>
</html>

当我删除团队名称时,我想从员工表中删除其所有引用。 请帮助提前谢谢。

【问题讨论】:

    标签: mysql jsp jakarta-ee spring-mvc hibernate-annotations


    【解决方案1】:

    Stacktrace 告诉 hibernate 无法映射您的实体。

    在您的实体中:

    团队:为您的teamId 删除@OneToOne。仅:

    @Id    
    @Column(name="teamId")
    private Integer teamId;
    

    够了。

    在您的代码中,注释中带有@OneToOne,这意味着团队与自身链接(即使没有,它也用于关联到另一个持久类),这没有意义。

    员工:

    因为你需要一个团队有很多员工,所以它实际上是一个@ManyToOne 用于你表teamId 中的列Employee,如下所示:

    @ManyToOne
    @JoinColumn(name = "teamId")
    private AddTeam teams;
    

    如果您愿意,可以使用@GeneratedValue 作为您的 ID。

    要删除所有引用,您可以在@ManyToOne 中使用cascade = CascadeType.ALL

    @ManyToOne(cascade = CascadeType.ALL)
    

    更多关于@ManyToOne的详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2014-01-18
      • 2013-02-04
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      相关资源
      最近更新 更多