A survey application,
There is two entity: Survey and Question
Survey has numbers of questions which means One survey refers To Many question
Some questions belongs to one question which means Many questions refers To One survey.
Survey entity:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Table(name = "survey")
public class Survey {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "create_date")
private LocalDateTime createDate;
@Column(name = "username")
private String username;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "survey")
private List<Question> questions;
Question entity:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Table(name = "question")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "no")
private Integer no;
@Column(name = "type")
private Integer type;
@Column(name = "phrase")
private String phrase;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "survey_id", referencedColumnName = "id")
private Survey survey;
In DB, there will be no field for List<Question> questions;
But will be a field for Survey survey;This field will be named survey_id, refers to id from Survey entity. But you need to use it Survey class, not id
questionService.save(new Question(++count,
Integer.parseInt(map.get(key2)), map.get(key),
surveyRepository.findById(finalSurvey.getId()).get()));
Comments
Post a Comment