0

我在我的应用程序中使用 Spring、hibernate、jsf 和 jquery。我通过休眠保存查询在数据库中插入一个 Question 对象。问题对象包含 id 、question、answertype 和使用 form_id 对表单对象的引用。现在我想通过更改存储在指定索引位置的 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);
    }

}

表单.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);
    }

}

问题DAO.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);
    }

}
4

2 回答 2

0

只是一个非常快速的评论,可能无法回答您的问题,但是:

我以前有这个,我没有提交我的会议!我在您的代码中没有看到您提交的任何地方......希望您找到答案。

于 2012-10-04T09:35:42.933 回答
0

提交事务或刷新会话。

如果那也行不通。使用刷新对象,

session.refresh(object, LockOptions.NONE)

它将从 DB 获取最新信息。

于 2013-08-09T11:56:16.220 回答