我正在使用 jsf 2.0 + primefaces 3.2 + EJB 2.0 + JPA 2.0 。大家好,当我试图影响一个新学生上课时,系统会发出一条错误消息,该消息是trasaction abored。我的实体代码:ClasseHasEtudiant
public class ClasseHasEtudiant implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected ClasseHasEtudiantPK classeHasEtudiantPK;
@JoinColumn(name = "Etudiant_id", referencedColumnName = "id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Etudiant etudiant;
@JoinColumn(name = "Classe_id", referencedColumnName = "id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Classe classe;
public ClasseHasEtudiant() {
}
public ClasseHasEtudiant(ClasseHasEtudiantPK classeHasEtudiantPK) {
this.classeHasEtudiantPK = classeHasEtudiantPK;
}
public ClasseHasEtudiant(int classeid, int etudiantid) {
this.classeHasEtudiantPK = new ClasseHasEtudiantPK(classeid, etudiantid);
}
public ClasseHasEtudiantPK getClasseHasEtudiantPK() {
return classeHasEtudiantPK;
}
public void setClasseHasEtudiantPK(ClasseHasEtudiantPK classeHasEtudiantPK) {
this.classeHasEtudiantPK = classeHasEtudiantPK;
}
public Etudiant getEtudiant() {
return etudiant;
}
public void setEtudiant(Etudiant etudiant) {
this.etudiant = etudiant;
}
public Classe getClasse() {
return classe;
}
public void setClasse(Classe classe) {
this.classe = classe;
}
@Override
public int hashCode() {
int hash = 0;
hash += (classeHasEtudiantPK != null ? classeHasEtudiantPK.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof ClasseHasEtudiant)) {
return false;
}
ClasseHasEtudiant other = (ClasseHasEtudiant) object;
if ((this.classeHasEtudiantPK == null && other.classeHasEtudiantPK != null) || (this.classeHasEtudiantPK != null && !this.classeHasEtudiantPK.equals(other.classeHasEtudiantPK))) {
return false;
}
return true;
}
@Override
public String toString() {
return "dossierEtudiants.entities.ClasseHasEtudiant[ classeHasEtudiantPK=" + classeHasEtudiantPK + " ]";
}
}
实体 ClasseHasEtudiantPK 的代码
@Embeddable
public class ClasseHasEtudiantPK implements Serializable {
@Basic(optional = false)
@NotNull
@Column(name = "Classe_id")
private int classeid;
@Basic(optional = false)
@NotNull
@Column(name = "Etudiant_id")
private int etudiantid;
public ClasseHasEtudiantPK() {
}
public ClasseHasEtudiantPK(int classeid, int etudiantid) {
this.classeid = classeid;
this.etudiantid = etudiantid;
}
public int getClasseid() {
return classeid;
}
public void setClasseid(int classeid) {
this.classeid = classeid;
}
public int getEtudiantid() {
return etudiantid;
}
public void setEtudiantid(int etudiantid) {
this.etudiantid = etudiantid;
}
@Override
public int hashCode() {
int hash = 0;
hash += (int) classeid;
hash += (int) etudiantid;
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof ClasseHasEtudiantPK)) {
return false;
}
ClasseHasEtudiantPK other = (ClasseHasEtudiantPK) object;
if (this.classeid != other.classeid) {
return false;
}
if (this.etudiantid != other.etudiantid) {
return false;
}
return true;
}
@Override
public String toString() {
return "dossierEtudiants.entities.ClasseHasEtudiantPK[ classeid=" + classeid + ", etudiantid=" + etudiantid + " ]";
}
}
这是我的 .xhtml 代码:
<ui:define name="content">
<h:panelGroup id="messagePanel" layout="block">
<h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
</h:panelGroup>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="#{bundle.CreateClasseHasEtudiantLabel_etudiant}" for="etudiant" />
<h:selectOneMenu id="etudiant" value="#{classeHasEtudiantController.selected.etudiant}" title="#{bundle.CreateClasseHasEtudiantTitle_etudiant}" required="true" requiredMessage="#{bundle.CreateClasseHasEtudiantRequiredMessage_etudiant}">
<f:selectItems value="#{etudiantController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="#{bundle.CreateClasseHasEtudiantLabel_classe}" for="classe" />
<h:selectOneMenu id="classe" value="#{classeHasEtudiantController.selected.classe}" title="#{bundle.CreateClasseHasEtudiantTitle_classe}" required="true" requiredMessage="#{bundle.CreateClasseHasEtudiantRequiredMessage_classe}">
<f:selectItems value="#{classeController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
</h:panelGrid>
<br />
<h:commandLink action="#{classeHasEtudiantController.create}" value="#{bundle.CreateClasseHasEtudiantSaveLink}" />
<br />
<br />
<h:commandLink action="#{classeHasEtudiantController.prepareList}" value="#{bundle.CreateClasseHasEtudiantShowAllLink}" immediate="true"/>
<br />
<br />
<h:commandLink value="#{bundle.CreateClasseHasEtudiantIndexLink}" action="/index" immediate="true" />
</h:form>
</ui:define>
这是我的 managedBean 代码:
public String prepareCreate() {
current = new ClasseHasEtudiant();
current.setClasseHasEtudiantPK(new dossierEtudiants.entities.ClasseHasEtudiantPK());
selectedItemIndex = -1;
return "Create";
}
public String create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle1").getString("ClasseHasEtudiantCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle1").getString("PersistenceErrorOccured"));
return null;
}
}
最后是我的日志:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`isetdb`.`classe_has_etudiant`, CONSTRAINT `classe_has_etudiant_ibfk_1` FOREIGN KEY (`Classe_id`) REFERENCES `classe` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Error Code: 1452
Call: INSERT INTO classe_has_etudiant (Classe_id, Etudiant_id) VALUES (?, ?)
bind => [2 parameters bound]
Query: InsertObjectQuery(entities.ClasseHasEtudiant[ classeHasEtudiantPK=entities.ClasseHasEtudiantPK[ classeid=0, etudiantid=0 ] ])
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:840)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:906)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:592)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:535)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1702)
at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:253)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:207)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:342)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:162)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:177)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:472)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:287)
at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:829)
at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:728)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2863)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1501)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1483)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1443)
at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:224)
at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:123)
at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:3784)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1407)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:634)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1497)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.issueSQLbeforeCompletion(UnitOfWorkImpl.java:3135)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.issueSQLbeforeCompletion(RepeatableWriteUnitOfWork.java:344)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:157)
at org.eclipse.persistence.transaction.JTASynchronizationListener.beforeCompletion(JTASynchronizationListener.java:68)
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:435)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:852)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5114)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4879)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2039)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1990)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at $Proxy302.create(Unknown Source)
at sessionBeans.__EJB31_Generated__ClasseHasEtudiantFacade__Intf____Bean__.create(Unknown Source)
at managedBeans.ClasseHasEtudiantController.create(ClasseHasEtudiantController.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`isetdb`.`classe_has_etudiant`, CONSTRAINT `classe_has_etudiant_ibfk_1` FOREIGN KEY (`Classe_id`) REFERENCES `classe` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2409)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2327)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2312)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:831)
... 87 more