2

我的问题:

我想用hibernate在一个链接表中映射3个不同表的主键。

一张照片应该是什么样子:

http://postimage.org/image/uoqv1p7mz/

我管理它来保存对象,但我无法通过选择加载它。

首先我的课:

SysTenant.java:

        @Entity
        @Table(name = "SYSTENANT")
        public class SysTenant {


    private long sysTenantId;
    private String name;

    private Set<SysTenantSysUserSysRoleAssociation> tenantUserRoleAssociation = new HashSet<SysTenantSysUserSysRoleAssociation>();


    @Id
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "increment")
    @Column(name = "SYSTENANTID", nullable = false)
    public long getSysTenantId() {
        return sysTenantId;
    }

    public void setSysTenantId(long sysTenantId) {
        this.sysTenantId = sysTenantId;
    }

    @Column(name = "NAME")
    public String getName() {
        return name;
    }

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

    @OneToMany(mappedBy = "pk.sysTenant")
    public Set<SysTenantSysUserSysRoleAssociation> getTenantUserRoleAssociation() {
        return tenantUserRoleAssociation;
    }

        public void setTenantUserRoleAssociation(
            Set<SysTenantSysUserSysRoleAssociation> tenantUserRoleAssociation) {
        this.tenantUserRoleAssociation = tenantUserRoleAssociation;
    }
}

系统用户.java:

@Entity
@Table(name = "SYSUSER")
public class SysUser {
    //----------------------
    // Constants
    //----------------------

    //----------------------
    // Properties
    //----------------------

    private long sysUserId;
    private String firstname;

    private long tenantId;

    private Set<SysTenantSysUserSysRoleAssociation> tenantUserRoleAssociation = new HashSet<SysTenantSysUserSysRoleAssociation>();


    //----------------------
    // Getter/Setter
    //----------------------

    @Id
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "increment")
    @Column(name = "SYSUSERID", nullable = false)
    public long getSysUserId() {
        return sysUserId;
    }

    public void setSysUserId(long sysUserId) {
        this.sysUserId = sysUserId;
    }

    @Column(name = "FIRSTNAME")
    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }


    @Column(name="TENANTID")
    public long getTenantId() {
        return tenantId;
    }

    public void setTenantId(long tenantId) {
        this.tenantId = tenantId;
    }

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.sysUser")
    public Set<SysTenantSysUserSysRoleAssociation> getTenantUserRoleAssociation() {
        return tenantUserRoleAssociation;
    }

    public void setTenantUserRoleAssociation(
            Set<SysTenantSysUserSysRoleAssociation> tenantUserRoleAssociation) {
        this.tenantUserRoleAssociation = tenantUserRoleAssociation;
    }

}

SysRole.java:

@Entity
    @Table(name = "SYSROLE")
    public class SysRole {

        private long sysRoleId;
        private String name;

private Set<SysTenantSysUserSysRoleAssociation> tenantUserRoleAssociation = new HashSet<SysTenantSysUserSysRoleAssociation>();

private Set<SysRoleSysTaskAssociation> roleTaskAssociation = new HashSet<SysRoleSysTaskAssociation>();

        // ----------------------
        // Getter/Setter
        // ----------------------

        @Id
        @GeneratedValue(generator = "generator")
        @GenericGenerator(name = "generator", strategy = "increment")
        @Column(name = "SYSROLEID", nullable = false)
        public long getSysRoleId() {
            return sysRoleId;
        }

        public void setSysRoleId(long sysRoleId) {
            this.sysRoleId = sysRoleId;
        }

        @Column(name = "NAME")
        public String getName() {
            return name;
        }

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

        @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.sysRole")
        public Set<SysRoleSysTaskAssociation> getRoleTaskAssociation() {
            return roleTaskAssociation;
        }

        public void setRoleTaskAssociation(Set<SysRoleSysTaskAssociation> roleTaskAssociation) {
            this.roleTaskAssociation = roleTaskAssociation;
        }

        @OneToMany(mappedBy = "pk.sysRole")
        public Set<SysTenantSysUserSysRoleAssociation> getTenantUserRoleAssociation() {
            return tenantUserRoleAssociation;
        }

        public void setTenantUserRoleAssociation(
                Set<SysTenantSysUserSysRoleAssociation> tenantUserRoleAssociation) {
            this.tenantUserRoleAssociation = tenantUserRoleAssociation;
        }

    }

SysTenantSysUserSysRoleAssociation:

@Entity
@Table(name = "SYSTENANT_SYSUSER_SYSROLE")
@AssociationOverrides({
        @AssociationOverride(name = "pk.sysTenant", joinColumns = @JoinColumn(name = "SYSTENANTID")),
        @AssociationOverride(name = "pk.sysUser", joinColumns = @JoinColumn(name = "SYSUSERID")),
        @AssociationOverride(name = "pk.sysRole", joinColumns = @JoinColumn(name = "SYSROLEID")) })
public class SysTenantSysUserSysRoleAssociation {


    private TenantUserRolePK pk = new TenantUserRolePK();

    //----------------------
    // Getter/Setter
    //----------------------

    @EmbeddedId
    public TenantUserRolePK getPk() {
        return pk;
    }

    public void setPk(TenantUserRolePK pk) {
        this.pk = pk;
    }

    @Transient
    public SysTenant getSysTenant() {
        return pk.getSysTenant();
    }

    public void setSysTenant(SysTenant sysTenant) {
        pk.setSysTenant(sysTenant);
    }

    @Transient
    public SysUser getSysUser() {
        return pk.getSysUser();
    }

    public void setSysUser(SysUser sysUser) {
        pk.setSysUser(sysUser);
    }

    @Transient
    public SysRole getSysRole() {
        return pk.getSysRole();
    }

    public void setSysRole(SysRole sysRole) {
        pk.setSysRole(sysRole);
    }

    //----------------------
    // Classes
    //----------------------

    @SuppressWarnings("serial")
    @Embeddable
    private class TenantUserRolePK implements Serializable {

        private SysTenant sysTenant;
        private SysUser sysUser;
        private SysRole sysRole;


        @ManyToOne
        public SysTenant getSysTenant() {
            return sysTenant;
        }

        public void setSysTenant(SysTenant sysTenant) {
            this.sysTenant = sysTenant;
        }

        @ManyToOne
        public SysUser getSysUser() {
            return sysUser;
        }

        public void setSysUser(SysUser sysUser) {
            this.sysUser = sysUser;
        }

        @ManyToOne
        public SysRole getSysRole() {
            return sysRole;
        }

        public void setSysRole(SysRole sysRole) {
            this.sysRole = sysRole;
        }
    }
}

为了测试对象是否被保存,我使用以下方法:

public void sysTenantSysUserSysRoleAssociationTest(){
        SysTenantSysUserSysRoleAssociation association = new SysTenantSysUserSysRoleAssociation();

        SysUser sysUser = new SysUser();
        sysUser.setFirstname("Steve");

        association.setSysUser(sysUser);

        SysRole sysRole = new SysRole();
        sysRole.setName("User");


        association.setSysRole(sysRole);

        SysTenant sysTenant = new SysTenant();
        sysTenant.setName("Tenant1");

        association.setSysTenant(sysTenant);


        new SysUserDAO().createUser(sysUser);
        new SysDataDAO().saveSysObject(sysRole);
        new SysDataDAO().saveSysObject(sysTenant);

        new SysDataDAO().saveSysObject(association);
    }

执行后,正确的对象 ID 被保存到数据库和链接表中。

这是我的问题:

如果我想选择持久的 sysUser 并接收角色和 Tennant HashSet,由 sysTennantSysUserSysRoleAssocion 表中的 ID 引用,代码如下:

public void getSysUser(){
        SysDataDAO sysDataDato = new SysDataDAO();
        List<?> result = sysDataDato.executeSysSelect("From SysUser");
    }

列表保持为空。

选择是正确的还是我的模型中有任何其他错误?

如果有人可以帮助我,我将不胜感激。

谢谢,

4

0 回答 0