标题:[求助] 请教基于EJB的用Seam和JSF写的"persist"问题(如何写入数据库?) ...
取消只看楼主
BTMouseKing
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-10-14
 问题点数:0 回复次数:1 
[求助] 请教基于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)
....................
搜索更多相关主题的帖子: EJB JSF Seam persist 
2008-10-14 05:23
BTMouseKing
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-10-14
得分:0 
太感谢了。

没办法。人生啊,5点还在工作。。。

另外,question的确也是一张表,我再试试。
2008-10-14 13:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-238115-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.475639 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved