Переучиваю Java, Springboot в личном проекте. У меня есть сущность с именем User
, которая управляется с помощью автоматически сгенерированных каркасов из jHipster
. У меня также есть объект UserProfile
, который я создал для хранения дополнительных данных (поскольку я не хотел возиться с объектом User
. Теперь, когда я открываю конечные точки REST для UserProfile
, я хочу, чтобы вызовы GET включали user_id
как часть JSON, а вызовы PUT/POST принять user_id
вместо UserProfile
, выполнить ассоциацию и только затем сохраниться. Используемый ORM — Hibernate/JPA. Какие аннотации Джексона мне следует использовать, чтобы это произошло?
Мой User
объект:
public class User {
@ToString.Exclude
@OneToOne(mappedBy = "user", orphanRemoval = true, fetch = FetchType.LAZY)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonIgnore
private UserProfile 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("userProfile")
private User user;
}
Мои версии: spring_boot_version=2.0.8.РЕЛИЗ hibernate_version=5.2.17.Окончательный
Наконец-то я сам заработал. Сообщение Этот SO помогло мне решить проблему. И вот мои классы модели предметной области с аннотациями, которые сработали:
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;
}