0
我得到了ManyToMany實體帳戶與實體角色。NullPointerException得到ManyToMany關係
@Entity
public class Account {
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
private Long id;
@ManyToMany(cascade= {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH}, fetch=FetchType.EAGER)
@JoinTable(name="Account_Role")
private Set<Role> roles;
// getters, setters
}
當我嘗試訪問關係集合我得到NullPointerException異常的角色爲null:
Account account = new Account();
account.getRoles().size();
不宜空集被注入roles
?或者是默認的行爲,我應該通過控制roles
創作:
private Set<Role> roles = new HashSet()<>;