Hello ,
I try to make a web application but i have some problem with my databse and mapping.
I used Hibernate Annotation(used field),and made 3 tables (it will be a quiz game) one for the Category's(contains an id and a name) one for the Questions(contains an id aka pk and a foreign key from category; to define from which category is part of),and one for Answers(id aka pk and a foreign key from Questions).
Now my problem is when i test them togtether for exmpl. I make a category,question and answer;save and set them, its ok.But if later i try to set a new question with an answer for an already existing category its add a nother one with the same name....
I tried to make it unique from database but then just throws an exception and rolls back as i handle it.
Can any1 help a bit?
Here are my codes (a bit long) :
Category::
@Entity
@Table(name = "category")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
public int id;
@Column(name = "NAME")
public String name;
@OneToMany(mappedBy = "category", fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
public Set<Questions> questions = new HashSet<Questions>();
public Category() {
}
public Category(String name) {
this.name = name;
}
public Category(int id, String name) {
this.id = id;
this.name = name;
}
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 Set<Questions> getQuestions() {
return questions;
}
public void setQuestions(Set<Questions> questions) {
this.questions = questions;
}
}
Questions::
@Entity
@Table(name = "questions")
public class Questions implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;
@Column(name = "CAT_ID", insertable = false, updatable = false)
private int cat_id;
@Column(name = "QUESTION")
private String question;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "CAT_ID", nullable = false)
private Category category;
@OneToMany(fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
private Set<Answers> answers = new HashSet<Answers>();
public Questions() {
}
public Questions(int cat_id, String question) {
this.cat_id = cat_id;
this.question = question;
}
public Questions(String question) {
this.question = question;
}
public Questions(int id, int cat_id, String question) {
this.id = id;
this.cat_id = cat_id;
this.question = question;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCat_id() {
return cat_id;
}
public void setCat_id(int cat_id) {
this.cat_id = cat_id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Set<Answers> getAnswers() {
return answers;
}
public void setAnswers(Set<Answers> answers) {
this.answers = answers;
}
}
Answers::
package com.finalproject.pojo;
import javax.persistence.*;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
@Entity
@Table(name = "answers")
public class Answers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;
@Column(name = "QUESTION_ID", insertable = false, updatable = false)
private int question_id;
@Column(name = "CORRECT_ANSWER")
private String correct_answer;
@Column(name = "WA_1")
private String wa_1;
@Column(name = "WA_2")
private String wa_2;
@Column(name = "WA_3")
private String wa_3;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "QUESTION_ID", nullable = false)
private Questions questions;
public Answers() {
}
public Answers(String correct_answer, String wa_1, String wa_2, String wa_3) {
this.correct_answer = correct_answer;
this.wa_1 = wa_1;
this.wa_2 = wa_2;
this.wa_3 = wa_3;
}
public Answers(int question_id, String correct_answer, String wa_1,
String wa_2, String wa_3) {
this.question_id = question_id;
this.correct_answer = correct_answer;
this.wa_1 = wa_1;
this.wa_2 = wa_2;
this.wa_3 = wa_3;
}
public Answers(int id, int question_id, String correct_answer, String wa_1,
String wa_2, String wa_3) {
this.id = id;
this.question_id = question_id;
this.correct_answer = correct_answer;
this.wa_1 = wa_1;
this.wa_2 = wa_2;
this.wa_3 = wa_3;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getQuestion_id() {
return question_id;
}
public void setQuestion_id(int question_id) {
this.question_id = question_id;
}
public String getCorrect_answer() {
return correct_answer;
}
public void setCorrect_answer(String correct_answer) {
this.correct_answer = correct_answer;
}
public String getWa_1() {
return wa_1;
}
public void setWa_1(String wa_1) {
this.wa_1 = wa_1;
}
public String getWa_2() {
return wa_2;
}
public void setWa_2(String wa_2) {
this.wa_2 = wa_2;
}
public String getWa_3() {
return wa_3;
}
public void setWa_3(String wa_3) {
this.wa_3 = wa_3;
}
public Questions getQuestions() {
return questions;
}
public void setQuestions(Questions questions) {
this.questions = questions;
}
}