我正在用我的表单创建一个角色,这是我第一次遇到这个错误,我已经在这里20天了。
显然,问题出在我的保存和/或我的角色上。实体。我该如何解决这个问题?以下是相关文件:我的错误:
20-Oct-2021 05:07:51.658 AVERTISSEMENT [http-nio-8080-exec-1] com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute #{rolesBean.newRoles}: javax.persistence.EntityNotFoundException: Entity no longer exists in the database: entities.Roles@780b0a91.
javax.faces.FacesException: #{rolesBean.newRoles}: javax.persistence.EntityNotFoundException: Entity no longer exists in the database: entities.Roles@780b0a91.
at com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:96)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:71)
at javax.faces.component.UICommand.broadcast(UICommand.java:222)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:847)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:1012)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:55)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
at javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:450)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.faces.el.EvaluationException: javax.persistence.EntityNotFoundException: Entity no longer exists in the database: entities.Roles@780b0a91.
at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:76)
at com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:82)
... 49 more
Caused by: javax.persistence.EntityNotFoundException: Entity no longer exists in the database: entities.Roles@780b0a91.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:1160)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:1042)
at services.Service.refreshEntity(Service.java:55)
at managedBean.RolesBean.save(RolesBean.java:257)
at managedBean.RolesBean.newRoles(RolesBean.java:166)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:65)
at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:66)
... 50 more
20-Oct-2021 05:07:51.661 GRAVE [http-nio-8080-exec-1] com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError javax.faces.el.EvaluationException: javax.persistence.EntityNotFoundException: Entity no longer exists in the database: entities.Roles@780b0a91.
at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:76)
at com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:82)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:71)
at javax.faces.component.UICommand.broadcast(UICommand.java:222)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:847)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:1012)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:55)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
at javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:450)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.persistence.EntityNotFoundException: Entity no longer exists in the database: entities.Roles@780b0a91.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:1160)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:1042)
at services.Service.refreshEntity(Service.java:55)
at managedBean.RolesBean.save(RolesBean.java:257)
at managedBean.RolesBean.newRoles(RolesBean.java:166)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:65)
at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:66)
... 50 more
实体角色:
public class Roles implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idRoles;
private String denomination;
private boolean actif = true;
@OneToMany(mappedBy = "role")
private Collection<PermissionsRoles> permissionsRoles;
@OneToMany(mappedBy = "roles")
private Collection<Utilisateurs> utilisateurs;
@Id
@Column(name = "IdRoles", nullable = false)
public int getIdRoles() {
return idRoles;
}
public void setIdRoles(int idRoles) {
this.idRoles = idRoles;
}
@Basic
@Column(name = "Denomination", nullable = false, length = 150)
public String getDenomination() {
return denomination;
}
public void setDenomination(String denomination) {
this.denomination = denomination;
}
@Basic
@Column(name = "Actif", nullable = false)
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Roles roles = (Roles) o;
return idRoles == roles.idRoles &&
actif == roles.actif &&
Objects.equals(denomination, roles.denomination);
}
@Override
public int hashCode() {
return Objects.hash(idRoles, denomination, actif);
}
public Collection<PermissionsRoles> getPermissionsRoles() {
return permissionsRoles;
}
public void setPermissionsRoles(Collection<PermissionsRoles> pr) {
this.permissionsRoles = pr;
}
public Collection<Utilisateurs> getUtilisateurs() {
return utilisateurs;
}
public void setUtilisateurs(Collection<Utilisateurs> user) {
this.utilisateurs = user;
}
@Override
public Roles clone(){
Roles role = null;
try{
role = (Roles) super.clone();
}catch (CloneNotSupportedException e) {
e.printStackTrace(System.err);
}
return role;
}
public void setFields(Roles role) {
this.denomination = role.denomination;
this.actif = role.actif;
this.permissionsRoles = role.permissionsRoles;
this.utilisateurs = role.utilisateurs;
}
我的服务:
public class SvcRoles extends Service<Roles> implements Serializable {
// Déclaration des variables
private static final Logger log = Logger.getLogger(SvcRoles.class);
private static final long serialVersionUID = 1L;
Map<String, Object> params = new HashMap<String, Object>();
public SvcRoles() {
super();
}
// Méthode qui permet de sauver un role et de le mettre en DB
@Override
public Roles save(Roles roles) {
if (roles.getIdRoles() == 0) {
em.persist(roles);
} else {
roles = em.merge(roles);
}
return roles;
}
和my save()
public void save()
{
SvcRoles serviceR = new SvcRoles();
SvcPermissions serviceP = new SvcPermissions();
SvcPermissionRoles servicePR = new SvcPermissionRoles();
servicePR.setEm(serviceR.getEm());
EntityTransaction transaction = serviceR.getTransaction();
transaction.begin();
try {
serviceR.save(role);
serviceR.refreshEntity(role);
if(role.getPermissionsRoles().size()>0){
for(PermissionsRoles pr : role.getPermissionsRoles())
{
servicePR.delete(pr.getIdPermissionsRoles());
}
}
for(Permissions p : listPerm)
{
if(serviceP.findOnePermission(p).size()>0){
servicePR.save(servicePR.createPermissionRoles(serviceP.findOnePermission(p).get(0),role));
}
else{
transaction.rollback();
FacesContext fc = FacesContext.getCurrentInstance();
fc.getExternalContext().getFlash().setKeepMessages(true);
fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"L'operation n'a pas reussie",null));
break;
}
}
transaction.commit();
FacesContext fc = FacesContext.getCurrentInstance();
fc.getExternalContext().getFlash().setKeepMessages(true);
fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"L'operation a reussie",null));
} finally {
if (transaction.isActive()) {
transaction.rollback();
FacesContext fc = FacesContext.getCurrentInstance();
fc.getExternalContext().getFlash().setKeepMessages(true);
fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"L'operation n'a pas reussie",null));
}
serviceR.close();
}
}
和我的网页:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f = "http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:shiro="http://shiro.apache.org/tags"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="Template/template.xhtml">
<ui:define name="title">#{I18N['role.form.nouveau.titre']}</ui:define>
<ui:define name="content">
<p:growl id="growl" showDetail="true" sticky="true" />
<shiro:hasPermission name="Roles:Creer">
<h:form id="form" prependId="false">
<h:head>
<title>#{I18N['role.form.nouveau.titre']}</title>
</h:head>
<h2 style="text-align:center">#{I18N['role.form.nouveau.titre']}</h2>
<p:panelGrid style="margin: 0 auto;" styleClass="ui-noborder" id="newrecform" columns="2" columnClasses="col1Newrec,col2Newrec,col3Newrec">
<p:outputLabel value = "#{I18N['role.label.Denomination']}" indicateRequired="true"/>
<p:inputText id="roleDenomination"
validatorMessage="La valeur doit être entre 2 et 100 caracteres"
value="#{rolesBean.role.denomination}"
required="true"
requiredMessage="#{I18N['formulaire.message.required.denomination']}"
maxlength="100">
<f:validateLength minimum="2" maximum="100"/>
<p:tooltip for="roleDenomination" value="#{I18N['tooltip.role.deno']}" position="right"/>
</p:inputText>
<p:outputLabel value = "#{I18N['Permissions.label.Type']}" indicateRequired="true"/>
<p:selectOneMenu value = "#{permissionsBean.type}" id="type" >
<f:selectItem itemValue="#{null}"/>
<f:selectItems value = "#{permissionsBean.permissionsType}" var="ty"
itemLabel="#{ty}"
itemValue="#{ty}"/>
<p:ajax event="change" listener="#{permissionsBean.getPermissionsAction}" update="form:action" />
</p:selectOneMenu>
<p:outputLabel value = "#{I18N['Permissions.label.Action']}" indicateRequired="true"/>
<p:selectOneMenu value = "#{permissionsBean.action}" id="action">
<f:selectItems value = "#{permissionsBean.listAction}" var="ac"
itemLabel="#{ac}"
itemValue="#{ac}"/>
</p:selectOneMenu>
<p:commandButton rendered="#{permissionsBean.action ne null}" icon="fa fa-fw fa-check-circle" value="#{I18N['permissions.label.add']}" action="#{rolesBean.addPermission}" ajax="false">
<f:setPropertyActionListener target="#{rolesBean.pe.type}" value="#{permissionsBean.type}" />
<f:setPropertyActionListener target="#{rolesBean.pe.action}" value="#{permissionsBean.action}" />
</p:commandButton>
</p:panelGrid>
<p:dataTable value="#{rolesBean.listPerm}" emptyMessage="Aucun enregistrement trouvé" var="pe" id="tableListPerm"
Class="table" headerClass="table-header" rowClasses="table-odd-row,table-even-row">
<p:column>
<!-- column header -->
<f:facet name="header">Type de permission</f:facet>
<!-- row record -->
<p:outputLabel value ="#{pe.type}"/>
</p:column>
<p:column>
<!-- column header -->
<f:facet name="header">Action</f:facet>
<!-- row record -->
<p:outputLabel value ="#{pe.action}"/>
</p:column>
<p:column>
<!-- column header -->
<f:facet name="header">Supprimer la permission</f:facet>
<!-- row record -->
<p:commandButton icon="fa fa-fw fa-check-circle" action="#{rolesBean.supPermission}" ajax="false">
<f:setPropertyActionListener target="#{rolesBean.pe}" value="#{pe}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:commandButton style="float: right" icon="fa fa-fw fa-check-circle" value="#{I18N['application.button.validate']}" action="#{rolesBean.newRoles}" immediate="true"/>
<p:commandButton style="float: left" icon="fa fa-fw fa-home" value="#{I18N['application.button.return']}" action="#{rolesBean.flushRol}" immediate="true"/>
</h:form>
</shiro:hasPermission>
<shiro:lacksPermission name="Roles:Creer">
<p:outputLabel value="#{I18N['application.page.accessrefuse']}"/>
</shiro:lacksPermission>
</ui:define>
</ui:composition>
</html>
谢谢大家
如果你搜索Stackoverflow关于javax.persistence.EntityNotFoundException
,您会发现问题出在您的IdRoles
配置中。您可以这样定义它:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idRoles;
@Id // should be removed
@Column(name = "IdRoles", nullable = false) // should be moved to field, int can't be null
public int getIdRoles() {
return idRoles;
}
您可以将javax.persistence
公告放在字段或getter上,但不能同时放在两者上。
参见:Hibernate通告——字段访问和属性访问哪个更好?
在您的情况下,有时持久化层正在从getter读取属性,并且它缺少@GeneratedValue()
注释,因此它等待您为实体设置id
。