Spring 5 Data Jpa - OneToOne on a specific literal property

zphenhs4  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(114)

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;
    }
}

lymgl2op

lymgl2op1#

你的问题是与Map注解,我猜尝试进行这些更改用户详细信息:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class UserDetails {
    @Id
    @Column(name = "user_id")
    private String userId;

    @OneToOne(mappedBy = "userDetails", cascade = CascadeType.ALL, orphanRemoval = true)
    private UserPreference<BufferMode> buffering;

    public UserDetails(User user) {
        this.userId = user.getId(); // Assuming you have a getId() method in User entity
        this.buffering = new UserPreference<>(this, PropertyType.Buffering);
    }
}

字符串
用户偏好:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class UserPreference<T> {
    @Id
    @ManyToOne
    @JoinColumn(name = "user_id")
    @ToString.Exclude
    private UserDetails userDetails;

    @Enumerated(EnumType.STRING)
    private PropertyType propertyType;

    @Convert(converter = JsonConverter.class)
    @JavaType(value = StringJavaType.class)
    private List<T> values;

    private boolean favorite;

    public UserPreference(UserDetails userDetails, PropertyType property) {
        this.userDetails = userDetails;
        this.propertyType = property;
    }
}


通过使用mappedby注解,这应该可以帮助您实现所需的Map,希望这对您有所帮助

相关问题