2012-10-04 78 views
0

我在我的應用程序中使用Spring,hibernate,jsf和jquery。我通過hibernate保存查詢在數據庫中插入一個Question對象。問題對象包含使用form_id的表單對象的id,question,answertype和引用。現在我想通過改變存儲在指定索引位置的Question對象列表中的值來更改存儲在數據庫中的Question對象的值。如果我更改列表中的值,則數據庫中的值不會被更新查詢更改。您能否提一些建議。對象值沒有得到更新數據庫使用休眠

Question.java 包com.otv.model;

import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 

import org.apache.commons.lang.builder.ToStringBuilder; 

@Entity 
@Table(name = "questions") 
public class Question implements Serializable { 

    @Id 
    @GeneratedValue 
    @Column(name = "id", unique = true, nullable = false) 
    private int id; 

    @Column(name = "question", nullable = false) 
    private String text; 

    @Column(name = "answertype", nullable = false) 
    private String answertype; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "form_id") 
    private Form form; 

    // @JoinColumn(name = "form_id") 
    // private int formId; 

    public Question() { 
    } 

    public Question(String text, String answertype) { 
     this.text = text; 
     this.answertype = answertype; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getQuestion() { 
     return text; 
    } 

    public void setQuestion(String question) { 

     this.text = question; 
    } 

    public String getAnswertype() { 
     return answertype; 
    } 

    public void setAnswertype(String answertype) { 
     this.answertype = answertype; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((answertype == null) ? 0 : answertype.hashCode()); 
     result = prime * result + id; 
     result = prime * result + ((text == null) ? 0 : text.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Question other = (Question) obj; 
     if (answertype == null) { 
      if (other.answertype != null) 
       return false; 
     } else if (!answertype.equals(other.answertype)) 
      return false; 
     if (id != other.id) 
      return false; 
     if (text == null) { 
      if (other.text != null) 
       return false; 
     } else if (!text.equals(other.text)) 
      return false; 
     return true; 
    } 

    public void setForm(Form form) { 
     this.form = form; 
    } 

    @Override 
    public String toString() { 
     return ToStringBuilder.reflectionToString(this); 
    } 

} 

Form.java

package com.otv.model; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

import org.apache.commons.lang.builder.ToStringBuilder; 

@Entity 
@Table(name = "FORM") 
public class Form implements Serializable { 

    @Id 
    @GeneratedValue 
    @Column(name = "id", unique = true, nullable = false) 
    private int id; 

    @Column(name = "name", nullable = false) 
    private String name; 

    @Column(name = "description", nullable = false) 
    private String description; 

    @OneToMany(mappedBy = "form", fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
    List<Question> questions = new ArrayList<Question>(); 

    public Form(String name) { 
     super(); 
     this.name = name; 
    } 

    public Form() { 
     super(); 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public List<Question> getQuestions() { 
     return questions; 
    } 

    public void setQuestions(List<Question> formQuestions) { 
     this.questions = formQuestions; 
    } 

    public void addQuestion(Question question) { 
     questions.add(question); 
     question.setForm(this); 
    } 

    public void removeQuestion(Question question) { 
     questions.remove(question); 
     question.setForm(this); 
    } 

    @Override 
    public String toString() { 
     return ToStringBuilder.reflectionToString(this); 
    } 

    public void replaceQuestion(int index, Question question) { 
     Question prevQuestion = questions.get(index); 
     // prevQuestion.setQuestion(question.getQuestion()); 
     // prevQuestion.setAnswertype(question.getAnswertype()); 
     question.setId(prevQuestion.getId()); 
     question.setForm(this); 
     questions.set(index, question); 
    } 

} 

QuestionDAO.java

package com.otv.user.dao; 

import java.util.List; 

import org.hibernate.SessionFactory; 

import com.otv.model.Question; 

public class QuestionDAO implements IQuestionDAO { 

    private SessionFactory sessionFactory; 

    public SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

    public void setSessionFactory(SessionFactory sessionFactory) { 
     this.sessionFactory = sessionFactory; 
    } 

    public void addQuestion(Question question) { 
     getSessionFactory().getCurrentSession().save(question); 
    } 

    public void deleteQuestion(Question question) { 
     getSessionFactory().getCurrentSession().delete(question); 
    } 

    public void updateQuestion(Question question) { 
     getSessionFactory().getCurrentSession().update(question); 
    } 

    public Question getQuestionById(int id) { 
     List list = getSessionFactory().getCurrentSession().createQuery("from Questions where id=?") 
       .setParameter(0, id).list(); 
     return (Question) list.get(0); 
    } 

} 

回答

0

只是一個非常快速的評論,不得然而回答你的問題,:

我收到了這一點,我沒有參加我的會議!我在你的代碼中沒有看到你正在提交的任何地方......希望你找到你的答案。

+0

嗨馬修,謝謝你的答覆。我使用的服務類標記爲Transactional,據說Spring將提交事務。 – user1662917

0

提交事務或刷新會話。

如果這是行不通的,太。刷新對象使用,

session.refresh(object, LockOptions.NONE) 

它會從數據庫得到最新。