hibernate生成的多对多实体类,在进行数据保存时如何操作

如下三个实体类,是hibernate自动生成的,我在保存员工数据的时候需要关联相应的项目数据一起保存,按照我原来的写法是在员工实体类中配置有项目的集合属性,直接把包关联保存的项目集合放入即可,但是hibernate自动生成的是一个中间关系维护类的方式(里面有项目类、员工类,还有一个id属性),这种应该如何关联保存项目数据呢?

  • 员工实体类
@Entity
@Table(name = "t_staff", catalog = "tq_monitor")
public class TStaff implements java.io.Serializable {
    private String id;
    private String openId;
    private String unionId;
    private String userId;
    private String staffName;
    private String email;
    private String phone;
    private String position;
    private Boolean staffState;
    private Set<TStaffProject> TStaffProjects = new HashSet<TStaffProject>(0);

    @GenericGenerator(name = "generator", strategy = "uuid")
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "id", unique = true, nullable = false, length = 36)
    public String getId() {
        return this.id;
    }

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

    @Column(name = "openId", length = 36)
    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    @Column(name = "unionId", length = 36)
    public String getUnionId() {
        return this.unionId;
    }

    public void setUnionId(String unionId) {
        this.unionId = unionId;
    }

    @Column(name = "userId", length = 50)
    public String getUserId() {
        return this.userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Column(name = "staffName", length = 40)
    public String getStaffName() {
        return this.staffName;
    }

    public void setStaffName(String staffName) {
        this.staffName = staffName;
    }

    @Column(name = "email", length = 50)
    public String getEmail() {
        return this.email;
    }

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

    @Column(name = "phone", length = 20)
    public String getPhone() {
        return this.phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Column(name = "position", length = 50)
    public String getPosition() {
        return this.position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    @Column(name = "staffState", precision = 1, scale = 0)
    public Boolean getStaffState() {
        return this.staffState;
    }

    public void setStaffState(Boolean staffState) {
        this.staffState = staffState;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TStaff")
    public Set<TStaffProject> getTStaffProjects() {
        return this.TStaffProjects;
    }

    public void setTStaffProjects(Set<TStaffProject> TStaffProjects) {
        this.TStaffProjects = TStaffProjects;
    }

}

-关系表实体类

@Entity
@Table(name = "t_staff_project", catalog = "tq_monitor")
public class TStaffProject implements java.io.Serializable {

    private TStaffProjectId id;
    private TProjects TProjects;
    private TStaff TStaff;

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "staffId", column = @Column(name = "staff_id", nullable = false, length = 36)),
            @AttributeOverride(name = "projectId", column = @Column(name = "project_id", nullable = false, length = 36)) })
    public TStaffProjectId getId() {
        return this.id;
    }

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id", nullable = false, insertable = false, updatable = false)
    public TProjects getTProjects() {
        return this.TProjects;
    }

    public void setTProjects(TProjects TProjects) {
        this.TProjects = TProjects;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "staff_id", nullable = false, insertable = false, updatable = false)
    public TStaff getTStaff() {
        return this.TStaff;
    }

    public void setTStaff(TStaff TStaff) {
        this.TStaff = TStaff;
    }

}

-项目表实体类

@Entity
@Table(name = "t_projects", catalog = "tq_monitor")
public class TProjects implements java.io.Serializable {

    private String id;
    private TProjects TProjects;
    private String projectName;
    private String projectType;
    private Boolean projectState;
    private Integer projectDevLimit;
    private Set<TSubAccounts> TSubAccountses = new HashSet<TSubAccounts>(0);
    private Set<TProjects> TProjectses = new HashSet<TProjects>(0);
    private Set<TDevices> TDeviceses = new HashSet<TDevices>(0);
    private Set<TStaffProject> TStaffProjects = new HashSet<TStaffProject>(0);

    @Id
    @Column(name = "id", unique = true, nullable = false, length = 36)
    public String getId() {
        return this.id;
    }

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

    @ManyToOne(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "project_id")
    public TProjects getTProjects() {
        return this.TProjects;
    }

    public void setTProjects(TProjects TProjects) {
        this.TProjects = TProjects;
    }

    @Column(name = "project_name", length = 50)
    public String getProjectName() {
        return this.projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    @Column(name = "project_type", precision = 1, scale = 0)
    public String getProjectType() {
        return this.projectType;
    }

    public void setProjectType(String projectType) {
        this.projectType = projectType;
    }

    @Column(name = "project_state", precision = 1, scale = 0)
    public Boolean getProjectState() {
        return this.projectState;
    }

    public void setProjectState(Boolean projectState) {
        this.projectState = projectState;
    }

    @Column(name = "project_dev_limit")
    public Integer getProjectDevLimit() {
        return this.projectDevLimit;
    }

    public void setProjectDevLimit(Integer projectDevLimit) {
        this.projectDevLimit = projectDevLimit;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TProjects")
    @NotFound(action = NotFoundAction.IGNORE)
    public Set<TSubAccounts> getTSubAccountses() {
        return this.TSubAccountses;
    }

    public void setTSubAccountses(Set<TSubAccounts> TSubAccountses) {
        this.TSubAccountses = TSubAccountses;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TProjects")
    @NotFound(action = NotFoundAction.IGNORE)
    public Set<TProjects> getTProjectses() {
        return this.TProjectses;
    }

    public void setTProjectses(Set<TProjects> TProjectses) {
        this.TProjectses = TProjectses;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TProjects")
    @NotFound(action = NotFoundAction.IGNORE)
    public Set<TDevices> getTDeviceses() {
        return this.TDeviceses;
    }

    public void setTDeviceses(Set<TDevices> TDeviceses) {
        this.TDeviceses = TDeviceses;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TProjects")
    @NotFound(action = NotFoundAction.IGNORE)
    public Set<TStaffProject> getTStaffProjects() {
        return this.TStaffProjects;
    }

    public void setTStaffProjects(Set<TStaffProject> TStaffProjects) {
        this.TStaffProjects = TStaffProjects;
    }

}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐