Home > OS >  SQL JPA Hibernate refer user to parent user
SQL JPA Hibernate refer user to parent user

Time:12-15

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.

CodePudding user response:

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;
  • Related