ManyToMany始终为null

时间:2015-04-30 13:44:39

标签: hibernate

VedUser中的

getUserGroups()始终为null。为什么呢?

@Entity
@Table(name="USERS", schema="VED")
public class VedUser implements Serializable{   
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_USER")
private long id;

private String login;

private int status;

@ManyToMany
@JoinTable(name="GROUPS_USERS",
    joinColumns=@JoinColumn(name="ID_USER", referencedColumnName="ID_USER"),
    inverseJoinColumns=@JoinColumn(name="ID_GROUP", referencedColumnName="ID_GROUP")
    )
private List<VedUserGroup> userGroups;

public String toString() {
    return new StringBuffer()
        .append("id=").append(getId()).append("; ")         
        .append("login=").append(getLogin()).append("; ")
        .append("status=").append(getStatus())
        .toString();
}

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

public long getId() {
    return id;
}

public void setLogin(String login) {
    this.login = login;
}

public String getLogin() {
    return login;
}

public void setStatus(int status) {
    this.status = status;
}

public int getStatus() {
    return status;
}

public void setUserGroups(List<VedUserGroup> userGroups) {
    this.userGroups = userGroups;
}

public List<VedUserGroup> getUserGroups() {
    return userGroups;
}   




////////////////////////////////////////////
`@Entity`
`@Table(name="GROUPS", schema="VED")` 
`public class VedUserGroup implements Serializable {`

    private static final long serialVersionUID = 1L;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_GROUP")
    private long id;

    private String name;

    private String description;

    private int status;

    //@ManyToMany(fetch = FetchType.EAGER, mappedBy = "userGroups")
    private Collection<VedUser> users;

    public String toString() {
        return new StringBuffer()
            .append("id=").append(getId()).append("; ")         
            .append("name=").append(getName()).append("; ")
            .append("description=").append(getDescription()).append("; ")
            .append("status=").append(getStatus())
            .toString();
    }

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

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getStatus() {
        return status;
    }

    public void setUsers(Collection<VedUser> users) {
        this.users = users;
    }

    public Collection<VedUser> getUsers() {
        return users;
    }   
}

CREATE TABLE "VED     "."GROUPS"  (
          "ID_GROUP" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (  
            START WITH +1  
            INCREMENT BY +1  
            MINVALUE +1  
            MAXVALUE +9223372036854775807  
            NO CYCLE  
            CACHE 20  
            NO ORDER ) , 
          "NAME" VARCHAR(50) NOT NULL , 
          "DESCRIPTION" LONG VARCHAR , 
          "STATUS" SMALLINT NOT NULL WITH DEFAULT 1 )   
         IN "USERSPACE1" ; 

ALTER TABLE "VED     "."GROUPS" 
    ADD CONSTRAINT "GROUPS_PK" PRIMARY KEY
        ("ID_GROUP");



CREATE TABLE "VED     "."USERS"  (
          "ID_USER" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (  
            START WITH +1  
            INCREMENT BY +1  
            MINVALUE +1  
            MAXVALUE +9223372036854775807  
            NO CYCLE  
            CACHE 20  
            NO ORDER ) , 
          "LOGIN" VARCHAR(20) NOT NULL , 
          "STATUS" SMALLINT NOT NULL WITH DEFAULT 1 )   
         IN "USERSPACE1" ; 


ALTER TABLE "VED     "."USERS" 
    ADD CONSTRAINT "USERS_PK" PRIMARY KEY
        ("ID_USER");



CREATE TABLE "VED     "."GROUPS_USERS"  (
          "ID_GROUP" BIGINT NOT NULL , 
          "ID_USER" BIGINT NOT NULL )   
         IN "USERSPACE1" ; 


ALTER TABLE "VED     "."GROUPS_USERS" 
    ADD CONSTRAINT "GROUPS_USERS_PK" PRIMARY KEY
        ("ID_GROUP",
         "ID_USER");


ALTER TABLE "VED     "."GROUPS_USERS" 
    ADD CONSTRAINT "GROUPS_USERS_GROUPS_FK" FOREIGN KEY
        ("ID_GROUP")
    REFERENCES "VED     "."GROUPS"
        ("ID_GROUP")
    ON DELETE CASCADE
    ON UPDATE NO ACTION
    ENFORCED
    ENABLE QUERY OPTIMIZATION;

ALTER TABLE "VED     "."GROUPS_USERS" 
    ADD CONSTRAINT "GROUPS_USERS_USERS_FK" FOREIGN KEY
        ("ID_USER")
    REFERENCES "VED     "."USERS"
        ("ID_USER")
    ON DELETE CASCADE
    ON UPDATE NO ACTION
    ENFORCED
    ENABLE QUERY OPTIMIZATION;

0 个答案:

没有答案