我遇到了@ManyToMany 集合没有填充数据加载的问题。我试过 FetchType.LAZY 和 FetchType.EAGER,结果没有变化。
当我打印用户对象时,角色的集合对象为空。
用户 [userId=2, firstName=Ajay, lastName=C, email=admin.demo@gmail.com, password=12345, roles=[]]
还尝试通过添加引用的列。但没有奏效。
请协助。
用户和角色实体如下。
@Entity
@Table(name = "\"USER\"", schema = "\"PLATFORM_PROD_IOT\"", uniqueConstraints = {
@UniqueConstraint(columnNames = { "\"EMAIL_ID\"" }) })
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Size(min = 1, max = 50)
@Column(name = "\"USER_ID\"")
private Long userId;
@NotBlank
@Size(min = 3, max = 50)
@Column(name = "\"FIRST_NAME\"")
private String firstName;
@NotBlank
@Size(min = 3, max = 50)
@Column(name = "\"LAST_NAME\"")
private String lastName;
@NaturalId
@NotBlank
@Size(max = 50)
@Email
@Column(name = "\"EMAIL_ID\"")
private String email;
@NotBlank
@Size(min = 3, max = 100)
@Column(name = "\"PASSWORD\"")
private String password;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "\"USER_ROLE_MAPPING\"", schema = "\"\PLATFORM_PROD_IOT\"", joinColumns = @JoinColumn(name = "\"USER_ID\""), inverseJoinColumns = @JoinColumn(name = "\"ROLE_ID\""))
private Set<Role> roles = new HashSet<>();
//Getters and Setters
}
@Entity
@Table(name = "\"ROLE\"",schema="\"PLATFORM_PROD_IOT\"")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="\"ROLE_ID\"")
private Long roleId;
@Column(name="\"ROLE_NAME\"")
private RoleName name;
//Getters and Setters
}