JPA 实体 Class 使用 class 对象作为参数

JPA Entity Class using class object as parameter

我有一个 class User,它有另一个 class 类型的参数 ShoppingList。 像这样...

@Entity
public class Employee implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, updatable = false)
    private Long id;
    private String Name;
    @?????
    private ShoppingList[] shoppingList;
}

当变量是一个数组时,如何建立这种 ManyToOne 关系? 这个想法是有一个 User table 和另一个 ShoppingList table,因此用户可以同时拥有多个列表。

正确的方法是:

一个Employee有很多ShoppingList

一个ShoppingList只有一个Employee.

@Entity
public class Employee implements Serializable {
    ....

    @OneToMany(mappedBy = "employee", fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    private List<ShoppingList> shoppingList;

    ....
}
@Entity
public class ShoppingList implements Serializable {
    ....
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "employee_id", nullable = false)
    private Employee employee;

   ....
}

您可以根据需要微调您的实体。 更多信息,我会参考this tutorial,它对我帮助很大。