Skip to content

Instantly share code, notes, and snippets.

@ekirastogi
Last active January 19, 2016 08:01
Show Gist options
  • Save ekirastogi/73027f95efb4e7899e7f to your computer and use it in GitHub Desktop.
Save ekirastogi/73027f95efb4e7899e7f to your computer and use it in GitHub Desktop.
Spring Hibernate - One to Many Mapping
package com.ekiras.domian;
import javax.persistence.*;
/**
* @author ekansh
* @since 19/1/16
*/
@Entity
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "question")
private String question;
@Column(name = "answer")
private String answer;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "topic_id")
private Topic topic;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic = topic;
}
public String getQuestion() {
return question;
}
public String getAnswer() {
return answer;
}
}
package com.ekiras.domian;
import javax.persistence.*;
import java.util.List;
/**
* @author ekansh
* @since 19/1/16
*/
@Entity
public class Topic {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "name")
private String name;
@OneToMany(cascade = CascadeType.ALL,mappedBy = "topic",fetch = FetchType.EAGER)
private List<Question> questions;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment