-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate - Multiple associations (foreign keys) in one link
PostPosted: Thu Nov 22, 2012 4:04 pm 
Newbie

Joined: Thu Nov 22, 2012 4:00 pm
Posts: 1
My Problem:

I want to map the primary keys of 3 different tables in one link table with hibernate.

A pic how it should look like:

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

I managed it to save the objects but I'm not able to load it with an select.

First of all, my classes:

SysTenant.java:

Code:
  @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;
    }
}


SysUser.java:

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

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

    private long sysUserId;
    private String firstname;

    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;
    }


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

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

}


SysRole.java:

Code:
@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:

Code:
@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;
        }
    }
}


To test if the objects are saved, I use the following method:

Code:
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);
    }


After executing, the correct object IDs are saved to the database and the linking table.

Here's my Problem:

If i want to select the persisted sysUser and receive the role and tennant HashSet, referenced by the IDs in the sysTennantSysUserSysRoleAssocion table with this code:

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


the list stays null.

Is the select correct or are there any other mistake in my model?

I would appreciate it, if somebody could help me out.

Thanks,

Lou


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.