将表的组件映射为另一个表中的arraylist,如onetomany

问题描述 投票:0回答:1

我正在使用JPA和hibernate持久性管理器。

我有两个类,LibraryLoanSystem.java和User.java。

library loan system.Java

 @Entity
    public class LibraryLoanSystem {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer idOfLLS;

        @Transient
        private ArrayList<Loan> loans = new ArrayList<Loan>();

        @Transient
        private ArrayList<Loanable> loanables = new ArrayList<Loanable>();

        @OneToMany
        @JoinTable(name = "USER" , joinColumns = @JoinColumn(name = "studentId"))
        private ArrayList<User> users = new ArrayList<User>();

        @Transient
        private ObservableList<Material> materialData = FXCollections.observableArrayList();

        public LibraryLoanSystem() {

            populateUsers();
            populateMaterials();
        } //getters,setters and methods

user.Java

@Entity
@Table(name = "User")
public class User {

    /* Name of the User */
    @Column(name = "name", nullable = false)
    private String name;

    /* Date of Birth for the User */
    @Column(name = "dateOfBirth",nullable = false)
    private LocalDate dateOfBirth;

    /* Student ID of the User */
    @Id
    @Column(name = "studentId", updatable = false, nullable = false)
    private String studentId;
//constructor,getters & setters

基本上,libraryloansystem中的ArrayList用户是系统中所有用户的列表,也就是说。用户表行中的所有用户记录。这两个java类之间存在聚合关系,onetomany,libraryloansystem有多个用户,但用户只有一个系统。我不确定如何绘制它们,我得到了

非法尝试将非集合映射为@OneToMany,@ ManyToMany或@CollectionOfElements:controller.LibraryLoanSystem.users

在我的尝试。我怎样才能正确映射这个arraylist?谢谢

java hibernate jpa hibernate-mapping
1个回答
0
投票

而不是:private ArrayList<User> users = new ArrayList<User>();

使用:private List<User> users = new ArrayList<>();

正如文档所述(link):

Hibernate要求将持久的集合值字段声明为接口类型。实际的接口可能是java.util.Set,java.util.Collection,java.util.List,java.util.Map,java.util.SortedSet,java.util.SortedMap或任何你喜欢的东西(“你喜欢的任何东西”)意味着你必须编写org.hibernate.usertype.UserCollectionType的实现。)

© www.soinside.com 2019 - 2024. All rights reserved.