如何使用@OneToMany 和@ManyToOne 嵌套?

How to use @OneToMany and @ManyToOne nest?

我有如下三个实体:

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = {"id", "bricksets", "themes"})
@Entity
public class Brand {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;                // Unique database primary key

    @OneToMany(mappedBy = "brand", cascade = CascadeType.ALL)
    private List<Brickset> bricksets;

    @OneToMany(mappedBy = "brand", cascade = CascadeType.ALL)
    private List<Theme> themes;

    @NotNull
    private String name;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = {"id"})
@ToString(exclude={"brand", "theme"})
@Entity
public class Brickset {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;            // Unique database primary key

    @NotNull
    private String number;      // set number

    @ManyToOne
    @JoinColumn(name = "brand_id")
    private Brand brand;

    @ManyToOne
    @JoinColumn(name = "theme_id")
    private Theme theme;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = {"id"})
@ToString(exclude={"brand", "brickset"})
@Entity
public class Theme {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;                // Unique database primary key

    @NotNull
    private String name;

    @ManyToOne
    @JoinColumn(name = "brand_id")
    private Brand brand;

    @OneToMany(mappedBy = "Theme", cascade = CascadeType.ALL)
    private List<Brickset> brickset;
}

但是我遇到如下编译错误,我试图自己解决它,但我是新手JPA/Hibernate,你能帮我吗,我下一步该怎么做?我认为问题是我在 Entity Theme 中使用 ManyToOne 和 OneToMany,这可能会被 JPA/Hibernate 混淆,我能理解吗?

Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.zangland.entity.Theme.brickset

您需要为 @OneToMany 注释使用集合。

Hibernate 要求将持久集合值字段声明为接口类型。

实际界面可能是 java.util.Setjava.util.Collectionjava.util.Listjava.util.Mapjava.util.SortedSetjava.util.SortedMap 或您喜欢的任何内容 ( "anything you like" 意味着您必须编写 org.hibernate.usertype.UserCollectionType 的实现。)

@OneToMany(mappedBy = "theme", cascade = CascadeType.ALL)
    private Set<Brickset> brickset;