gpt4 book ai didi

mysql - spring mysql hibernate中的外键引用

转载 作者:行者123 更新时间:2023-11-29 12:53:44 26 4
gpt4 key购买 nike

嗨,我正在编写一个 spring mvc 休眠注释应用程序,我有 2 个表 "team" 和另一个表 "players"。这里我使用 一个-对一映射和mysql数据库。在玩家表中,我保存玩家记录,如姓名、活跃年份等。“团队” 表包含两个条目teamid(主键)和teamname“players” 表包含外键 teamid。在这里,当我尝试删除团队名称而不删除 players 表中的所有引用时,我是获取错误消息

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute update query

所以当我删除所有引用时,就没有错误了。那么我如何在删除之前找到所有引用。我有兴趣使用 javascript 显示一条警告消息,警告删除所有引用。我知道如何创建javascript中的简单警报消息,但在这里我必须仅在存在外键引用时才显示警报。我想知道是否有其他方法可以删除外键引用而不影响“玩家” 表。

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;
}

}

Player.java

@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;
}
}

这是在playerDaoImplementation.java类中使用的查询

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


}

AddteamDaoImplementation.java中用于删除的查询

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

}

PlayerController.java 中的删除函数

@RequestMapping(value="/deletePlayer",method=RequestMethod.GET)
public ModelAndView deletePlayerDetails(@ModelAttribute("command") Player player,
BindingResult result){
playerService.deletePlayer(player.getPlayerId());
Map<String, Object> model = new HashMap<String, Object>();
model.put("playerkey", playerService.listPlayer());
model.put("teamKey", addteamService.listTeams());
return new ModelAndView("EditPlayer",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>Players Manager</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>

EditPlayer.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 Players</h2>
<form:form method="POST" action="save.html">
<table>
<tr>
<td><form:label path="playerId">Player Id</form:label></td>
<td><form:input path="playerId" value= "${player.playerId }"/></td>
</tr>
<tr>
<td><form:label path="playerName">Name</form:label></td>
<td><form:input path="playerName" value="${player.playerName }"/></td>
</tr>

<tr>
<td><form:label path="YearsActive">Experience</form:label></td>
<td><form:input path="YearsActive" value="${player.YearsActive }"/></td>
</tr>
<tr>
<td><form:label path="Country">Date of Join</form:label></td>
<td><form:input path="Country" value="${player.Country }"/></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>



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

</tr>

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

<c:if test="${!empty playerkey}">
<table align="center" border="1">
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>YearsActive</th>
<th>Country</th>

</tr>

<c:forEach items="${playerkey}" var="player">
<tr>
<td><c:out value="${player.playerId}"/></td>
<td><c:out value="${player.playerName }"/></td>
<td><c:out value="${player.YearsActive}"/></td>
<td><c:out value="${player.Country}"/></td>



<td align="center"><a href="editPlayerDetails.html?playerId=${player.playerId}">Edit</a> | <a href="deletePlayer.html?playerId=${player.playerId}">Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>

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

请帮忙。提前致谢

最佳答案

对于您的用例,您的数据库建模(如果您已经定义了架构)或实体建模(如果您从实体模型生成架构)不正确。外键应位于指向 Player 主键的 AddTeam 表中。就实体映射而言,OneToOne 应该位于 AddTeam 上。

关于mysql - spring mysql hibernate中的外键引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24400126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com