[求助] 请教基于EJB的用Seam和JSF写的"persist"问题(如何写入数据库?)
我也不知道如何阐述题目比较清楚,希望达人能够明白。代码如下:
========================================================================================
package edu.groupProjectPackage;
import static javax.persistence.PersistenceContextType.EXTENDED;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Conversational;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;
import edu.groupProjectPackage.CourseCreateQuiz;
@Stateful
@Scope(ScopeType.CONVERSATION)
@RolesAllowed(value={"quiz"})
@Name("courseCreateQuiz")
@Conversational
public class CourseCreateQuizBean implements CourseCreateQuiz
{
@PersistenceContext(type=EXTENDED)
private EntityManager em;
@In(value="quiz",required=false)
@Out(required=false)
private Quiz quiz;
@DataModel
private List<Question> questions;
@DataModelSelection
@Out(required = false)
private Question question;
@In
private FacesMessages facesMessages;
@Logger
private Log log;
@In Identity identity;
@Factory("questions")
@Begin
public void createQuiz()
{
question = new Question();
quiz = new Quiz();
questions = new Vector<Question>();
question.setText("question1");
questions.add(question);
quiz.setTitle("firstquiz");
quiz.setStart(new Date(0));
quiz.setEnd(new Date(15));
quiz.setQuestions(questions);
System.out.println(quiz.getDecimalScore()+ " "+ quiz.getFlagDatePass()+ " "+
quiz.getId()+" " + quiz.getNumberOfCorrectAnswer()+" "+ quiz.getTitle()+
" "+quiz.getEnd()+ " "+ quiz.getStart()+ " "+ quiz.getQuestions().size());
System.out.println("hello");
}
@End
public void submitQuiz()
{
System.out.println("how are you");
em.persist(quiz);
//log.info("Create a new Quiz #{quiz.title}");
//facesMessages.add("Done.#{quiz.title}");
}
@Remove
public void destroy(){}
}
========================================================================================
问题出在点击xhtml里的submit按钮时,调用submit()函数,然后碰到persist()就出问题了:
17:22:22,256 INFO [STDOUT] Hibernate:
insert
into
quizzes
(
end, start, title)
values
(?, ?, ?)
17:22:22,264 INFO [STDOUT] Hibernate:
insert
into
quizzes_questions
(quizzes_id, questions_id)
values
(?, ?)
17:22:22,265 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: edu.columbia.w4156.sasuke.Question
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:755)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1143)
....................