1

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.

rvicente
  • 15
  • 4
  • Does this answer your question? [How to create Hibernate Mapping for a self referencing table](https://stackoverflow.com/questions/29975751/how-to-create-hibernate-mapping-for-a-self-referencing-table) – racraman Dec 15 '22 at 10:24

1 Answers1

0

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;