Hi all,
I'm using a many-to-many relation to store privileges to rights. I've used a couple table to do this in the database.
Code:
Table role
role_id
title
description
Table privilege
privilege_id
entity
action
Table role_privilege
role_id
privilege_id
By doing this in Hibernate I've used annotations. Writing new privileges to rights are stored fine but loading them only gives me one result.
Here's the Java code, I'm using GigaSpaces (based on OpenSpaces) to generate Id's and such so don't mind that.
Role class
Code:
@SpaceClass
@Entity
@Table(name="ROLE")
public class Role implements Serializable{
/**
* Generated serial version id.
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name="ROLE_ID")
private String roleId;
@Column(nullable=false, name="TITLE")
private String title;
@Column(name="DESCRIPTION")
private String description;
@ManyToMany(
targetEntity=Privilege.class, fetch=FetchType.EAGER ,
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
name="ROLE_PRIVILEGE",
joinColumns=@JoinColumn(name="ROLE_ID"),
inverseJoinColumns=@JoinColumn(name="PRIVILEGE_ID")
)
private Set<Privilege> privileges;
Privilege class
Code:
@SpaceClass
@Entity
@Table(name="PRIVILEGE")
public class Privilege implements Serializable{
/**
* Generated serial version id.
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name="PRIVILEGE_ID")
private String privilegeId;
@Column(name="ENTITY", nullable=false)
private String entity;
@Column(name="ACTION", nullable=false)
private String action;
Database values
Table Role
Code:
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^13', '', 'project manager'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', 'This role has every privilege', 'account manager'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^8', 'standard user', 'employee'
Table privilege
Code:
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^10', 'edit', 'timesheet'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^11', 'delete', 'timesheet'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^12', 'insert', 'timesheet'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^15', 'edit', 'contract'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^16', 'insert', 'contract'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^17', 'delete', 'contract'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^18', 'view', 'customer'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^19', 'edit', 'customer'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^20', 'insert', 'customer'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^21', 'delete', 'customer'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^22', 'view', 'approval'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^23', 'edit', 'approval'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^24', 'insert', 'approval'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^25', 'delete', 'approval'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^7', 'view', 'timesheet'
'-167212391^36^TimeSense_container1:TimeSense^1273474552926^9', 'view', 'contract'
Table role_privilege
Code:
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^13', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^10'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^10'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^13', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^11'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^11'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^13', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^12'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^12'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^15'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^16'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^17'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^18'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^19'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^21'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^22'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^23'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^24'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^25'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^8', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^7'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^13', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^9'
'-509273202^31^TimeSense_container1:TimeSense^1273474552926^26', '-167212391^36^TimeSense_container1:TimeSense^1273474552926^9'
In Java to check my results I'm executing the following code
Code:
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).gigaSpace();
Role r = new Role();
r.setTitle("account manager");
Role foundRole = gigaSpace.read(r);
System.out.println(foundRole.getTitle());
System.out.println(foundRole.getRoleId());
for(Privilege p: foundRole.getPrivileges()) {
System.out.println(p.getAction() + " " +p.getEntity());
}
Result
Code:
account manager
-509273202^31^TimeSense_container1:TimeSense^1273474552926^26
edit timesheet
The weird thing is that only one result is added to my set collection while there should be more.
Does anyone has an idea?