0

有我的两个班级组和用户:

@Entity
@Table(name = "GROUPS")
@NamedNativeQuery(name = "getGroupbyUserEmail", query = "SELECT g.group_id, g.name, g.description FROM  `GROUPS` AS g LEFT JOIN user_group AS c ON c.group_id = g.group_id LEFT JOIN USERS AS u ON u.user_id = c.user_id WHERE u.email =? ")
@NamedQueries({
        @NamedQuery(name = "Group.getAllGroups", query = "SELECT e FROM Group e"),
        @NamedQuery(name = "Group.getGroupByName", query = "SELECT e FROM Group e WHERE e.name=:name") })
public class Group implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private String description;
    private Set<User> users;

    public Group() {

        users = new HashSet<User>(0);
    }

    public Group(String name, String description, Set<User> users) {
        super();
        this.name = name;
        this.description = description;
        this.users = users;
    }

    public Group(String name, String description) {
        super();
        this.name = name;
        this.description = description;

    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "GROUP_ID", unique = true, nullable = false)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "NAME", nullable = false, length = 20)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "DESCRIPTION", nullable = false, length = 20)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

和用户:

    @Entity
@Table(name = "USERS")
@NamedQuery(name = "User.getUserByEmail", query = "SELECT e FROM User e WHERE e.email = :email ")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    private Integer id;
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private Set<Group> groups = new HashSet<Group>(0);

    public User() {

    }

    public User(String firstName, String lastName, String email,
            String password, Set<Group> groups) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.groups = groups;
    }

    public User(String firstName, String lastName, String email, String password) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "USER_ID", unique = true, nullable = false)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "FIRST_NAME", nullable = false, length = 30)
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "LAST_NAME", nullable = false, length = 30)
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name = "EMAIL", nullable = false, length = 30)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "PASSWORD", nullable = false, length = 30)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "user_group", joinColumns = { @JoinColumn(name = "USER_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "GROUP_ID", nullable = false, updatable = false) })
    public Set<Group> getGroups() {
        return groups;
    }

    public void setGroups(Set<Group> groups) {
        this.groups = groups;
    }

我的问题是,当我从数据库中获取用户对象时,正确检索所有字段,控制台我看到两个查询选择,一个是用户,另一个与组相关,但不幸的是 ArrayList 应该包含的组是空的。

我尝试更改 fetchType Lazy-> Eager 但没有预期的结果

4

0 回答 0