Passing data to a repository rest api in a spring boot application

31 Views Asked by At

I have 2 entities -

public class Instance {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    private String name;
    private LocalDate startDate;

    @Enumerated(EnumType.STRING)
    private InstanceStatus instanceStatus;
}


public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    @ManyToOne(targetEntity = Instance.class)
    private Instance instance;

    private String name;
    private String email;

    @Enumerated(EnumType.STRING)
    private UserStatus userStatus;
}

and respective repositories -

public interface InstanceRepository extends JpaRepository<Instance, UUID> {
}

public interface UserRepository extends JpaRepository<User, UUID> {
}

I am trying to persist user information along with an instance which is already existing. I have an instance with id = 5512a059-fe11-4b6f-9ce0-a485839186b1 and below is the user data i am trying to save in json -

{
    "name": "Manasi B Gopinath",
    "email": "[email protected]",
    "userStatus": null,
    "instance": {
          "href": "http://localhost:8080/api/instances/5512a059-fe11-4b6f-9ce0-a485839186b1"
    }
}  

The user data is getting saved to the database, but the instance is not getting resolved and hence saved as null. Can someone let me know how to pass the instance data here.

Thanks.

0

There are 0 best solutions below