在Spring 5 Data Jpa - OneToOne, new entity, a single insert operation被求解之后,
我有以下表格:
- UserDetails,带userId
- UserPreference,包含userId、propertyType、values和isFavorite
我试图Map它们,以便每个propertyType都是UserDetails上的单个变量。我尝试了各种注解和方法,但我总是遇到一个问题;无论是在启动时还是在尝试查询User时(这将查询UserDetails,与此问题相关)。
以下是我当前的设置,这使得Spring在启动时抛出此错误:
Caused by: org.hibernate.MappingException: A '@JoinColumn' references a column named 'propertyType' but the target entity 'com.msh.UserPreference' has no property which maps to this column
字符串
物业类型:
public enum PropertyType {
Buffering,
AntiAliasing,
ScreenSize,
}
public enum BufferMode {
None,
Double,
Triple,
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class UserDetails {
@Id
@Column(name = "user_id")
private String userId;
@OneToOne
@MapsId
@JoinColumn(name = "user_id")
@ToString.Exclude
private User user;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @JoinFormula(value = "'Buffering'", referencedColumnName = "propertyType")),
@JoinColumnOrFormula(column = @JoinColumn(referencedColumnName = "userId"))
})
private UserPreference<BufferMode> buffering;
/* After:
...
private UserPreference<Bool> antiAliasing;
*/
public UserDetails(User user) {
this.user = user;
this.buffering = new UserPreference<>(user, PropertyType.Buffering);
}
}
的数据
和用户首选项:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class UserPreference<T> {
@Id
@Column(name = "user_id")
private String userId;
@OneToOne
@MapsId
@JoinColumn(name = "user_id")
@ToString.Exclude
private User user;
@Id
private PropertyType propertyType;
@Convert(converter = JsonConverter.class)
@JavaType(value = StringJavaType.class)
private List<T> values;
private boolean favorite;
public UserPreference(User user, PropertyType property) {
this.user = user;
this.propertyType = property;
}
}
型
1条答案
按热度按时间lymgl2op1#
你的问题是与Map注解,我猜尝试进行这些更改用户详细信息:
字符串
用户偏好:
型
通过使用mappedby注解,这应该可以帮助您实现所需的Map,希望这对您有所帮助