title: Foreign key reference in spring mysql hibernate
body: <p>Hi i am writing a spring mvc hibernate annotation application there i have 2 tables <strong>"team"</strong> and another table <strong>"players"</strong>.Here i am using <strong>one-to-one</strong> mapping and <strong>mysql</strong> database.In <strong>players</strong> table i am keeping player records like name,years active etc.The <strong>"team"</strong> table contains two entries <strong>teamid</strong>(primary key)and <strong>teamname</strong>.
  The <strong>"players"</strong> table contains the foreign key <strong>teamid</strong>.Here  when i try to delete a teamname without deleting all its references in <strong>players</strong> table i am getting error message</p>
  
  <pre><code>HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute update query
  </code></pre>
  
  <p>so  when i delete all the references ,then there is no error.So how can i find all the refrences before deleting.I am interested in showing an alert message using javascript which warns to delete all references.I know how to create simple alert message in javascript,but here i have to show the alert only if there is a reference to foreign key.And i want to know is there  any alternate way to delete the foriegn key reference without affecting the <strong>"players"</strong> table.</p>
  
  <pre><code>AddTeam.java
  
  
  @Entity
  @Table(name="Team")
  
  public class AddTeam {
  
  
      @Id
      @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;
           }
  
  }
  </code></pre>
  
  <p>Player.java</p>
  
  <pre><code>@Entity
  @Table(name="playerdata")
  public class Player implements Serializable {
  
      private static final long serialVersionUID = -723583058586873479L;
  
      /**
       * 
       */
  
      @Id
      @Column(name="playerid")
      private Integer playerId;
  
      @Column(name="playername")
      private String playerName;
  
      @Column(name="yearsactive")
      private Integer yearsActive;
  
          @Column(name="country")
      private Integer Country;
  
  
  
  
       @OneToOne
       @JoinColumn(name="teamId")
       private AddTeam teams;
  
  
  
      public Integer getPlayerId(){
          return playerId;
      }
      public void setPlayerId(Integer playerId){
          this.playerId=playerId;
      }
  
  
      public String getPlayerName(){
          return playerName;
      }
      public void setPlayerName(String playerName){
          this.playerName=playerName;
      }
  
  
  
      public String getCountry(){
          return Country;
      }
      public void setCountry(String Country){
          this.Country=Country;
      }
  
  
  
      public Integer getyearsActive(){
          return yearsActive;
      }
      public void setyearsActive(Integer yearsActive){
          this.yearsActive=yearsActive;
      }
  
      public AddTeam getTeams() {
            return teams;
           }
  
           public void setTeams(AddTeam teams) {
            this.teams = teams;
           }
    }
  </code></pre>
  
  <p>and this is the query used in playerDaoImplementation.java class</p>
  
  <pre><code>  @Override
      public void deleteResource(int playerid) {
          // TODO Auto-generated method stub
          sessionfactory.getCurrentSession().createQuery("DELETE FROM Resource WHERE playerid=" +playerid).executeUpdate();
  
  
      }
  </code></pre>
  
  <p>Query used to delete in AddteamDaoImplementation.java</p>
  
  <pre><code>@Override
      public void deleteTeams(int teamid) {
          // TODO Auto-generated method stub
          sessiofactory.getCurrentSession().createQuery("DELETE FROM AddTeam WHERE teamid="+teamid).executeUpdate();
  
      }
  </code></pre>
  
  <p>deletefunction in PlayerController.java </p>
  
  <pre><code>@RequestMapping(value="/deletePlayer",method=RequestMethod.GET)
      public ModelAndView deletePlayerDetails(@ModelAttribute("command")  Player player,
              BindingResult result){
          playerService.deletePlayer(player.getPlayerId());
          Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;();
          model.put("playerkey", playerService.listPlayer());
          model.put("teamKey", addteamService.listTeams());
          return new  ModelAndView("EditPlayer",model);
      }
  </code></pre>
  
  <p>Team.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
        &lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  
      &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Players Manager&lt;/title&gt;
  
  &lt;center&gt;
  &lt;h2&gt;Add Team Details&lt;/h2&gt;
  &lt;form:form method="POST" action="Team.html"&gt;
     &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="teamId"&gt;Team ID:&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="teamId" id="demo" value="${team.teamId}"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="teamName"&gt;Team Name:&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="teamName" value="${team.teamName}"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                    &lt;td&gt;&lt;input type="submit" value="SAVE"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt; 
          &lt;/form:form&gt;
          &lt;br/&gt;
    &lt;c:if test="${!empty teamKey}"&gt;
      &lt;table align="center" border="1"&gt;
          &lt;tr&gt;
              &lt;th&gt;Category ID&lt;/th&gt;
              &lt;th&gt;Category Name&lt;/th&gt;
              &lt;th&gt;Options&lt;/th&gt;
          &lt;/tr&gt;
  
          &lt;c:forEach items="${teamKey}" var="team"&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;c:out value="${team.teamId}"/&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;c:out value="${team.teamName}"/&gt;&lt;/td&gt;
                  &lt;td align="center"&gt;&lt;a href="editTeam.html?teamId=${team.teamId}"&gt;Edit&lt;/a&gt; |
                   &lt;a href="deleteTeam.html?teamId=${team.teamId}"&gt;Delete&lt;/a&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/c:forEach&gt;
      &lt;/table&gt;
  &lt;/c:if&gt;
  &lt;h2&gt;&lt;a href="updateresource.html"&gt;Adding Publication&lt;/a&gt;&lt;/h2&gt;
  
  
  &lt;/center&gt;
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>EditPlayer.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  
      &lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
       &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Resource Manager&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;center&gt;
  &lt;h2&gt;Add Players&lt;/h2&gt;
  &lt;form:form method="POST" action="save.html"&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;form:label path="playerId"&gt;Player Id&lt;/form:label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;form:input path="playerId"  value= "${player.playerId }"/&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;form:label path="playerName"&gt;Name&lt;/form:label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;form:input path="playerName" value="${player.playerName }"/&gt;&lt;/td&gt;
    &lt;/tr&gt;
  
    &lt;tr&gt;
      &lt;td&gt;&lt;form:label path="YearsActive"&gt;Experience&lt;/form:label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;form:input path="YearsActive" value="${player.YearsActive }"/&gt;&lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;&lt;form:label path="Country"&gt;Date of Join&lt;/form:label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;form:input path="Country" value="${player.Country }"/&gt;&lt;/td&gt;
    &lt;/tr&gt;
  
  
  
  
      &lt;tr&gt;
                      &lt;td&gt;
                          &lt;form:label path="teams.teamId"&gt;Team Name&lt;/form:label&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;form:select path="teams.teamId" cssStyle="width: 150px;"&gt;    
                              &lt;option value="-1"&gt;Select a type&lt;/option&gt;
                              &lt;c:forEach items="${teamKey}" var="teams"&gt;
                              &lt;option value="${teams.teamId}"&gt;${teams.teamName}&lt;/option&gt;
                              &lt;/c:forEach&gt;
                          &lt;/form:select&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
  
  
  
    &lt;tr&gt;
     &lt;td colspan="2"&gt;&lt;input type="submit"value="Submit"&gt;&lt;/td&gt;
  
      &lt;/tr&gt;
  
   &lt;/table&gt;
   &lt;/form:form&gt;
  &lt;br/&gt;
  
   &lt;c:if test="${!empty playerkey}"&gt;
      &lt;table align="center" border="1"&gt;
          &lt;tr&gt;
              &lt;th&gt;Player ID&lt;/th&gt;
              &lt;th&gt;Player Name&lt;/th&gt;
              &lt;th&gt;YearsActive&lt;/th&gt;
              &lt;th&gt;Country&lt;/th&gt;
  
          &lt;/tr&gt;
  
          &lt;c:forEach items="${playerkey}" var="player"&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;c:out value="${player.playerId}"/&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;c:out value="${player.playerName }"/&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;c:out value="${player.YearsActive}"/&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;c:out value="${player.Country}"/&gt;&lt;/td&gt;
  
  
  
                  &lt;td align="center"&gt;&lt;a href="editPlayerDetails.html?playerId=${player.playerId}"&gt;Edit&lt;/a&gt; | &lt;a href="deletePlayer.html?playerId=${player.playerId}"&gt;Delete&lt;/a&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/c:forEach&gt;
      &lt;/table&gt;
  &lt;/c:if&gt;
  
      &lt;h2&gt;&lt;a href="addTeam.html"&gt;Adding Team&lt;/a&gt;&lt;/h2&gt;
  &lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>please help.
  thanks in advance</p>