Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL JPA Hibernate refer user to parent user

I want to create an API with users where I can make referals to other user and store the users that refearal me.

@Entity
@Table(name="requirement")
@Data
@NoArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "name")
    private String name;
    
    
    private User parentUser;

}
CREATE TABLE user(
        id serial PRIMARY KEY,
        name VARCHAR,
        user_id INT,

what is the best way to handle this case

I want to create an API with users where I can make referals to other user and store the users that refearal me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use @OneToOne Relation between parent entity if you have a related column like ‘parent_user_id’.

@OneToOne
@JoinColumn(name = "parent_user_id")  
private User parentUser;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading