简体   繁体   中英

Is it possible to use one-to-many and many-to-one in the same entity?

I would like to modify the spring's rest tutorial. Link here

The tutorial has two entity: User and bookmark ( many bookmark can belong to one user. )

I would like to modify it a bit. I would like to create a user, question, answer entity - a user can have many questions, and a question can have many answers.

Is this possible? How should the entity definition look like for the question entity?

The logic would be that a user could create quizzes. The quiz can contain questions, and those questions may have possible answers.

在此处输入图片说明

Any ideas how should the entities look like?

I would appreciate every idea.

It is definitely possible.

User

@Entity
public class User {

// id and other attributes ommited

// User and Quiz has OneToMany bidirectional relationship. OP hasn't specified that but I think it makes more sense because a quiz most likely will need to know the user who created it.
@OneToMany (mappedBy="user",  cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
private List<Quiz> quizes;

// ...
}

Quiz

@Entity
public class Quiz {
// id ommitted
@OneToMany
private List<Question> questions;

@ManyToOne
@JoinColumn(name = "user_id") //quiz table will have a column `user_id` foreign key referring to user table's `id` column
private User user;

// ...
}

Question

@Entity
public class Question {
// id ommitted
@OneToMany
@JoinColumn(name="question_id") // Question and Answer has one-to-many unidirectional relationship. `answer` table has a foreign key `question_id` referring to `question.id` column
private List<Answer> answers;

// ...
}

Answer

@Entity
public class Answer {

// ..more attributes
}  

Note that:

  • the entity relationships also depend on your business logic.
  • If the owner of the bidirectional relationship is different, then your client code needs to adjust. jpa-joincolumn-vs-mappedby
  • If you want to design your table to be "clean" such that one entity table does not have a foreign key referring to another associated entity. You can create a join table , make the OneToMany relationship "feel" like a ManyToMany and use unique index to enforce the OneToMany. It is up to you. This wikibook page explains pretty well

This is absolutely not the only solution.

Is it possible to use one-to-many and many-to-one in the same entity?

I assume your question is, can "questions entity" have one-to-many relationship with Answers entity and many-to-one relationship with User entity at the same time. Yes, it is possible. Just, be careful while using annotation to map your entities each other, otherwise performance of your application will be seriously degraded. Use eager/Lazy fetch wisely. Print out the sql queries that spring-data-jpa/hibernate fires under the hood and analyze.

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