How do I implement a unidirectional one to many self referencing relationship?

user3643556

I have a "Question" entity that I can persist to "Question" table. A question can have multiple translations. A translation is just another question in another language that is associated with its parent question with parent_id field. Thus the Question table has columns question_id (int), parent_id (int), language (varchar), prompt, etc. In my design, the parent_id is the same as the question_id for a translation. For example, let's say I have question with id 13 and 17 in English (default) and question 13 has 3 translations, in Chinese, Japanese, and Korean. Then the Question table would look like the following:

question_id parent_id language

13 13 English; 14 13 Chinese; 15 13 Japanese; 16 13 Korean; 17 17 English

In the Question object, I want map a Question's relation to its translations which is a @OneToMany self-referenced relationship. I have implemented it the following way after doing some research:

@Entity
@Table(name = "question")
public class Question {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;

    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="parent_id")
    private Question parent;

    @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
    private Set<Question> translations;

    Other properties...
    Getters and setters....
}

However, I do not understand the following due to it being a self-referencing relationship:

  • Is this a unidirectional or bidirectional relationship?
  • What would the effect be of the annotation cascade = CascadeType.PERSIT on "parent" field or what does it mean in this case?
  • Is it possible to replace the "parent" field with Integer "parent_id" and still maintain the relationship? In other words, can I just keep track of the parent with Integer "parent_id" instead of having the entire Question object like I have now: private Question parent
Guillermo
  • Is this a unidirectional or bidirectional relationship?

What you have is self-referential bidirectional relationship. It is justa a special case of standard bidirectional relationships.

  • What would the effect be of the annotation cascade = CascadeType.PERSIT on "parent" field or what does it mean in this case?

In this case and following your example, it means that if you set the parent 13 in your Question 14, just invoking persist for Question 14 the Querstion 13 will be persisted too.

I encourage to see the JPA specification to know what to expect (here is JPA 2.1 spect).

  • Is it possible to replace the "parent" field with Integer "parent_id" and still maintain the relationship? In other words, can I just keep track of the parent with Integer "parent_id" instead of having the entire Question object like I have now: private Question parent

Yes, you could do the following replacements,

//@ManyToOne(cascade=CascadeType.PERSIST)
//@JoinColumn(name="parent_id")
//private Question parent;
@Column(name="parent_id", insertable=false, updatable=false)
private Integer parentId;

//@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
@JoinColumn(name="parent_id")
private Set<Question> translations;

Some notes about,

  • Cascade persist option for translations collections isn't necessary. Due to the relationship isn't bidirectional anymore, you can add Questions 14, 15 and 16 to the Question 13's collection and persist it along with its collection.
  • In a common scenario the parentId attribute will be updated when a question is add or move it to some collection (this fire up an update statement to set value of field parent_id). It shouldn't be necessary to modify the attribute manually therefore I add and set the insertable and updateble options to false.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I enforce a unidirectional one-to-many relationship?

From Dev

How do I enforce a unidirectional one-to-many relationship?

From Dev

Many to Many Self referencing relationship

From Dev

How to implement one to many relationship

From Dev

Criteria query for unidirectional one-to-many relationship

From Dev

Criteria query for unidirectional one-to-many relationship

From Dev

Self referencing one to many Model

From Dev

How do I represent a one to many relationship with the Entity Framework?

From Dev

How do I create a one to many relationship in Parse?

From Dev

How do I define a one-to-one relationship over a one-to-many relationship in a relational database?

From Dev

How can I display an ER Diagram with a self referencing relationship in yEd?

From Dev

NHibernate unidirectional one-to-many relationship not saving foreign key

From Dev

NHibernate unidirectional one-to-many relationship not saving foreign key

From Dev

Self-referencing many-to-many relationship EF code first

From Dev

Laravel: Add a join to self referencing Many-to-Many relationship?

From Dev

Database design: many2many relationship for self referencing FK

From Dev

One-To-One unidirectional relationship

From Dev

How to identify the mapped/owned sides of a self-referencing one-to-one relationship in Hibernate?

From Dev

How do I create a table with self-referencing fields in MySQL?

From Dev

How do i create a graphql schema for a self referencing data hierarchy?

From Dev

How to implement Self-Referencing Relationships in Grails?

From Dev

Many-To-Many relationship with Attributes and unidirectional mapping

From Dev

How do I create a recursive one-to-many relationship using core data?

From Dev

How do I map one to many relationship on a JSP page and store them while submitting the page?

From Dev

How do I access the attribute of a one to many relationship and pass to another tableview?

From Dev

How do I make one to many relationship to same table in Laravel 5?

From Dev

How do I configure Entity Framework cascade delete to work properly with a one to many relationship?

From Dev

one to many unidirectional doctrine persistance

From Dev

TypeORM Unidirectional Many To One relation

Related Related

  1. 1

    How do I enforce a unidirectional one-to-many relationship?

  2. 2

    How do I enforce a unidirectional one-to-many relationship?

  3. 3

    Many to Many Self referencing relationship

  4. 4

    How to implement one to many relationship

  5. 5

    Criteria query for unidirectional one-to-many relationship

  6. 6

    Criteria query for unidirectional one-to-many relationship

  7. 7

    Self referencing one to many Model

  8. 8

    How do I represent a one to many relationship with the Entity Framework?

  9. 9

    How do I create a one to many relationship in Parse?

  10. 10

    How do I define a one-to-one relationship over a one-to-many relationship in a relational database?

  11. 11

    How can I display an ER Diagram with a self referencing relationship in yEd?

  12. 12

    NHibernate unidirectional one-to-many relationship not saving foreign key

  13. 13

    NHibernate unidirectional one-to-many relationship not saving foreign key

  14. 14

    Self-referencing many-to-many relationship EF code first

  15. 15

    Laravel: Add a join to self referencing Many-to-Many relationship?

  16. 16

    Database design: many2many relationship for self referencing FK

  17. 17

    One-To-One unidirectional relationship

  18. 18

    How to identify the mapped/owned sides of a self-referencing one-to-one relationship in Hibernate?

  19. 19

    How do I create a table with self-referencing fields in MySQL?

  20. 20

    How do i create a graphql schema for a self referencing data hierarchy?

  21. 21

    How to implement Self-Referencing Relationships in Grails?

  22. 22

    Many-To-Many relationship with Attributes and unidirectional mapping

  23. 23

    How do I create a recursive one-to-many relationship using core data?

  24. 24

    How do I map one to many relationship on a JSP page and store them while submitting the page?

  25. 25

    How do I access the attribute of a one to many relationship and pass to another tableview?

  26. 26

    How do I make one to many relationship to same table in Laravel 5?

  27. 27

    How do I configure Entity Framework cascade delete to work properly with a one to many relationship?

  28. 28

    one to many unidirectional doctrine persistance

  29. 29

    TypeORM Unidirectional Many To One relation

HotTag

Archive