使用hibernate在数据库中没有更新对象值

时间:2012-10-04 09:30:08

标签: hibernate object

我在我的应用程序中使用Spring,hibernate,jsf和jquery。我通过hibernate保存查询在数据库中插入一个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);
    }

}

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

}

2 个答案:

答案 0 :(得分:0)

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

我之前有这个,我没有参加我的会议!我在你的代码中看不到你提交的任何地方...希望你能找到答案。

答案 1 :(得分:0)

提交事务或刷新会话。

如果那也行不通。使用

刷新对象
session.refresh(object, LockOptions.NONE)

它将从DB获得最新信息。