我获得了具有 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,因为角色为空:
Account account = new Account();
account.getRoles().size();
不应该注入一个空集roles
吗?或者它是默认行为,我应该roles
通过以下方式控制创建:
private Set<Role> roles = new HashSet()<>;