0

我遇到了@ManyToMany 集合没有填充数据加载的问题。我试过 FetchType.LAZY 和 FetchType.EAGER,结果没有变化。

当我打印用户对象时,角色的集合对象为空。

用户 [userId=2, firstName=Ajay, lastName=C, email=admin.demo@gmail.com, password=12345, roles=[]]

还尝试通过添加引用的列。但没有奏效。

请协助。

用户和角色实体如下。

@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
}
@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
}
4

1 回答 1

0

在课堂角色中尝试一下。

  @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "USER_ROLE_MAPPING",joinColumns = @JoinColumn(name = "ROLE_ID"), inverseJoinColumns = @JoinColumn(name = "USER_ID""))
    private Set<Role> roles = new HashSet<>();

并在 User 类中将 fetchType 更改为 EAGER。如果它不起作用,请尝试将表更改为不同的名称。示例:用户、角色。它不起作用,因为它在 SQL DATABASE 中具有相同的名称。

于 2019-01-23T09:23:34.833 回答