0

I'm relearning Java, Springboot in a personal project. I have an entity named User, which is managed via the autogenerated scaffolding from jHipster. I also have a UserProfile which is an entity that I created to hold extra data (since I didn't want to muck around with the User object. Now, when I expose REST endpoints for UserProfile, I want the GET calls to include the user_id as part of the JSON, and the PUT/POST calls to accept user_id for the UserProfile, do the association and only then persist. The ORM being used is Hibernate/JPA. What Jackson annotations should I use to make this happen?

My User object is:

public class User {
    @ToString.Exclude
    @OneToOne(mappedBy = "user", orphanRemoval = true, fetch = FetchType.LAZY)
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @JsonIgnore
    private UserProfile userProfile;
}

and my UserProfile class is:

public class UserProfile {
    @ToString.Exclude
    @ApiModelProperty(value = "Linking the UserProfile to the base User object that is used for authentication")
    @OneToOne(optional = false)
    // Note: Not marking with 'NotNull' annotation since we will not want to update the User object when upserting the UerProfile
    @JoinColumn(unique = true, nullable = false, insertable = true, updatable = false)
    @JsonIgnoreProperties("userProfile")
    private User user;
}

My versions are: spring_boot_version=2.0.8.RELEASE hibernate_version=5.2.17.Final

1 Answers1

0

Finally, I got this working myself. This SO post helped me solve the issue. And here are my domain model classes with the annotations that worked:

public class User {
    @ToString.Exclude
    @OneToOne(mappedBy = "user", orphanRemoval = true, fetch = FetchType.LAZY)
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @JsonIgnore
    private UserProfile userProfile;
}
public class UserProfile {
    @ToString.Exclude
    @ApiModelProperty(value = "Linking the UserProfile to the base User object that is used for authentication")
    @OneToOne(optional = false)
    // Note: Not marking with 'NotNull' annotation since we will not want to update the User object when upserting the UerProfile
    @JoinColumn(unique = true, nullable = false, insertable = true, updatable = false)
    @JsonIgnoreProperties(value = {"login", "password", "firstName", "lastName", "email", "activated", "langKey", "imageUrl", "resetDate"}, ignoreUnknown = true)
    private User user;
}