Я пытаюсь сохранить новый demande в базе данных, у меня есть выпадающий список в jsp, который дает мне существующий тип demande из DB. У demande есть идентификатор (первичный ключ), пользователь demande (пользователь внешнего ключа), тип demande (тип внешнего ключа demande). на странице jsp существующие списки работают нормально, хотя ..
это ошибка в спящем режиме дает мне:
WARN UnresolvedEntityInsertActions - HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
Unsaved transient entity: ([jpa.project.model.DemTypeDemande#0])
Dependent entities: ([[jpa.project.model.DemDemande#150]])
Non-nullable association(s): ([jpa.project.model.DemDemande.demTypeDemande])
tomcat:
HTTP Status 500 - Request processing failed; nested exception is org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: jpa.project.model.DemDemande.demTypeDemande -> jpa.project.model.DemTypeDemande
РЕДАКТИРОВАТЬ: Контроллер:
@Controller
public class DemandeController {
@Autowired
ProjectService service ;
@RequestMapping(value = "/demandes/manage",method = RequestMethod.GET)
public ModelAndView manageUsers(HttpServletRequest request) throws IOException {
//set view
ModelAndView model = new ModelAndView();
model.setViewName("managedemandes");
//set users list
List<DemDemande> listDemandes = service.getDemandes();
model.addObject("listDemandes", listDemandes);
//set types demande list
List<DemTypeDemande> listTypeDemandes = service.getTypeDemandes();
model.addObject("listTypeDemandes", listTypeDemandes);
//retrieve user
String ConnectedUsername = service.getConnectedUsername(request);
DemUser currentUser = service.getUserByUsername(ConnectedUsername);
model.addObject("demUser", currentUser);
//add demande object
model.addObject("DemDemande", new DemDemande());
return model;
}
@RequestMapping(value = "/demandes/actionadddemande", method = RequestMethod.POST)
public String actionadduser(@ModelAttribute("DemTypeDemande") DemTypeDemande TypeDemande,
@ModelAttribute("demUser") DemUser DemUser
,@ModelAttribute("DemDemande") DemDemande demande,BindingResult result) {
demande.setDemUser(DemUser);
demande.setDemTypeDemande(TypeDemande);
service.addDemande(demande);
return "redirect:/demandes/manage";
}
}
объект demande:
@Entity
@Table(name="DEM_DEMANDE")
@NamedQuery(name="DemDemande.findAll", query="SELECT d FROM DemDemande d")
public class DemDemande implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="DEM_DEMANDE_IDDEMANDE_GENERATOR", sequenceName="SEQ_DEM_DEMANDE")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DEM_DEMANDE_IDDEMANDE_GENERATOR")
@Column(name="ID_DEMANDE", unique=true, nullable=false)
private long idDemande;
//uni-directional many-to-one association to DemTypeDemande
@ManyToOne
@JoinColumn(name="ID_TYPE_DEMANDE", nullable=false)
private DemTypeDemande demTypeDemande;
//uni-directional many-to-one association to DemUser
@ManyToOne
@JoinColumn(name="ID_DEM_USER", nullable=false)
private DemUser demUser;
public DemDemande() {
}
public long getIdDemande() {
return this.idDemande;
}
public void setIdDemande(long idDemande) {
this.idDemande = idDemande;
}
public DemTypeDemande getDemTypeDemande() {
return this.demTypeDemande;
}
public void setDemTypeDemande(DemTypeDemande demTypeDemande) {
this.demTypeDemande = demTypeDemande;
}
public DemUser getDemUser() {
return this.demUser;
}
public void setDemUser(DemUser demUser) {
this.demUser = demUser;
}
}
тип объекта demande:
@Entity
@Table(name="DEM_TYPE_DEMANDE")
@NamedQuery(name="DemTypeDemande.findAll", query="SELECT d FROM DemTypeDemande d")
public class DemTypeDemande implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="DEM_TYPE_DEMANDE_IDTYPEDEMANDE_GENERATOR", sequenceName="SEQ_DEM_TYPE_DEMANDE")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DEM_TYPE_DEMANDE_IDTYPEDEMANDE_GENERATOR")
@Column(name="ID_TYPE_DEMANDE", unique=true, nullable=false)
private long idTypeDemande;
@Column(name="LIB_TYPE_DEMANDE", nullable=false, length=100)
private String libTypeDemande;
public DemTypeDemande() {
}
public long getIdTypeDemande() {
return this.idTypeDemande;
}
public void setIdTypeDemande(long idTypeDemande) {
this.idTypeDemande = idTypeDemande;
}
public String getLibTypeDemande() {
return this.libTypeDemande;
}
public void setLibTypeDemande(String libTypeDemande) {
this.libTypeDemande = libTypeDemande;
}
}
объект пользователя:
@Entity
@Table(name="DEM_USER")
@NamedQuery(name="DemUser.findAll", query="SELECT d FROM DemUser d")
public class DemUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="DEM_USER_IDUSER_GENERATOR", sequenceName="SEQ_DEM_USER")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DEM_USER_IDUSER_GENERATOR")
@Column(name="ID_USER", unique=true, nullable=false)
private long idUser;
@Column(name="NAME_USER", nullable=false, length=50)
private String nameUser;
@Column(nullable=false, length=20)
private String password;
//uni-directional many-to-one association to DemRole
@ManyToOne
@JoinColumn(name="ID_ROLE", nullable=false)
private DemRole demRole;
public DemUser() {
}
public long getIdUser() {
return this.idUser;
}
public void setIdUser(long idUser) {
this.idUser = idUser;
}
public String getNameUser() {
return this.nameUser;
}
public void setNameUser(String nameUser) {
this.nameUser = nameUser;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public DemRole getDemRole() {
return this.demRole;
}
public void setDemRole(DemRole demRole) {
this.demRole = demRole;
}
}
JSP:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ 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>Insert title here</title>
</head>
<body>
<div align="left">
This is manage Demandes
<table border="1">
<h1>Demandes</h1>
<tr><td> id demande </td> <td> Type demande :</td> <td> Nom demandeur :</td></tr>
<c:forEach var="demande" items="${listDemandes}">
<tr>
<td>${demande.idDemande}</td>
<td>${demande.demTypeDemande.libTypeDemande}</td>
<td>${demande.demUser.nameUser}</td>
</tr>
</c:forEach>
</table>
</div>
<div align="center">
<h1>New user</h1>
<form:form action="actionadddemande" method="POST" modelAttribute="DemDemande">
<table>
<form:hidden path="idDemande" />
<form:hidden path="demUser" />
<tr>
<td>Types :</td>
<td>
<form:select path="demTypeDemande">
<form:options items="${listTypeDemandes}" itemValue="idTypeDemande" itemLabel="libTypeDemande"></form:options>
</form:select></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save"></td>
</tr>
</table>
</form:form>
</div>
</body>
</html>