Announcement Announcement Module
Collapse
No announcement yet.
@OneToMany with composite key Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @OneToMany with composite key

    I have a One to One relation between Employee and Role table with EmployeeRoleMap as the join table. I have tried using the Join table and it does not work since the join table has an active flag(multiple records with only one as 'Y') which I am unable to factor in. Can this be factored in ?

    Since this was not possible I tried mapping in the join table but the empRoleMapList list was coming up as empty without any exceptions. Can someone let me know where I am going wrong.

    Code:
    My table structure has
    EMP - 
    EMP_ID(PK)
    EMP_ID_KEY(PK)
    ....
    
    EMP_ROLE_MAP - 
    EMP_ID(PK)
    EMP_ID_KEY(PK)
    ROLE_ID(PK)
    ROLE_ID_KEY(PK)
    ACTIVE_FLG
    
    ROLE - 
    ROLE_ID
    ROLE_ID_KEY
    ....
    The hibernate objects

    Code:
    @Entity
    @Table(schema = "PROJECT", name = "EMPLOYEE")
    public class Employee {
    
    @Id
    private EmployeePK id;
    
    // Doesn't work since the join table has a active flag
    //@OneToOne
    //@JoinTable(
    //schema = "PROJECT", 
    //name = "EMP_ROLE_MAP", 
    //joinColumns = {@JoinColumn(name = "EMP_ID"),@JoinColumn(name = "EMP_ID_KEY")},
    //inverseJoinColumns = {@JoinColumn(name = "ROLE_ID"),@JoinColumn(name = "ROLE_ID_KEY")}
    //)
    //private Role role;
    	
    @OneToMany
    @JoinColumns({
    @JoinColumn(referencedColumnName="EMP_ID", name="id.empId"),
    @JoinColumn(referencedColumnName="EMP_ID_KEY", name="id.empIdKey")
        })
    private List<EmployeeRoleMap> empRoleMapList;
    
    }
    Code:
    @Entity
    @Table(schema="BBP_ALLOC",name="EMP_ROLE_MAP")
    @AssociationOverrides({
    @AssociationOverride(name = "id.empId", joinColumns = @JoinColumn(name = "EMP_ID")),
    @AssociationOverride(name = "id.empIdKey", joinColumns = @JoinColumn(name = "EMP_ID_KEY"))
            })
    public class BBPUserRoleMap {
    	
    @Id
    private BBPUserRoleMapPK id;
    
    @ManyToOne
    @JoinColumns({
    @JoinColumn(name="EMP_ID", referencedColumnName="EMP_ID", insertable=false, updatable=false),
    @JoinColumn(name="EMP_ID_KEY", referencedColumnName="EMP_ID_KEY", insertable=false, updatable=false)
    })
    private BBPUser bbpUser;
    
    	
    @ManyToOne
    @JoinColumns({
    @JoinColumn(name="ROLE_ID"),
    @JoinColumn(name="ROLE_ID_KEY")
    })
    private Role role;
    Last edited by Java Developer; Jul 13th, 2011, 05:46 AM.
Working...
X