| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1995 人关注过本帖
标题:[求助] 请教基于EJB的用Seam和JSF写的"persist"问题(如何写入数据库?) ...
只看楼主 加入收藏
BTMouseKing
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-10-14
收藏
 问题点数:0 回复次数:3 
[求助] 请教基于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
huwangvs
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:764
专家分:0
注 册:2007-2-4
收藏
得分:0 
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

我不了解Seam,但是从错误提示可以看出来原因。
你在保存一个对象的时候,这个对象引用了另外一个对象,但是被引用的对象并没有被存到数据库里面。

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);

就是这里。
你的question 也对应一张表吧?而且quiz和question是一对多的关系吧?你这么写是不行的。。在你保存quiz的时候,JPA会去找quesiont引用,但是你没有,所以报错了。
2008-10-14 09:00
huwangvs
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:764
专家分:0
注 册:2007-2-4
收藏
得分:0 
话说,哥们真早
5点还在好好学习。。。。
2008-10-14 09:05
BTMouseKing
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-10-14
收藏
得分:0 
太感谢了。

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

另外,question的确也是一张表,我再试试。
2008-10-14 13:02
快速回复:[求助] 请教基于EJB的用Seam和JSF写的"persist"问题(如何写入数据库? ...
数据加载中...
 
   



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

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