简体   繁体   中英

Java hibernate how can I persist List of entities without relationship?

How can I persist a list of entities in one column of a table?

I'm trying to store result after a user has answered a random set of multiple choice questions with one answer being correct. The two tables being "quetions" and "result". So I want to store all the questions that was picked to a column in "result" preferable without relationship/mapping.

When I try this it creates multiple tables like this:

"questions"

"result_questions" This one containing result_id and question_id like it would be a manyToMany relationship..

"result"

@Entity
public class Questions{
private String question;
private String[] options;
private String answer;
}

@Entity
public class Result{

private int score;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;


@ElementCollection
private List<MultipleChoice> questions;

}

Check the Tobias Liefke answer to this question: Convert list in entity to single string column in database . This way you will have a text column with questions splitted by the characters you defined on the converter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM