Object value not getting updated in the database using hibernate
- by user1662917
I am using Spring,hibernate,jsf with jquery in my application. I am inserting a Question object in the database through the hibernate save query . The question object contains id ,question,answertype and reference to a form object using form_id. Now I want to alter the values of Question object stored in the database by altering the value stored in the list of Question objects at the specified index position. If I alter the value in the list the value in the database is not getting altered by update query . Could you please advise.
Question.java
package 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);
}
}