我正在使用JPA和hibernate持久性管理器。
我有两个类, LibraryLoanSystem.java 和 User.java 。
LibraryLoanSystem.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?谢谢
答案 0 :(得分: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的实现。)