I have the model User on my system that has a OneToMany relationship with Role, role is currently a trait and I have other roles inherinting from it: AdministratorRole, VisitorRole, ResidentRole. My doubt is how can I model (best way) this system to my entities database. Below my current "Role" models:
PS: Ignore the //TODOS
trait Role extends KeyedEntity[Long]
{
val id:Long = 0
val identifier = ""
val idMetaData:Long = 0
//val permissions = List[Permission]()
}
class AdministratorRole(val idCondo:Long) extends Role {
override val identifier = "AdministratorRole"
//TODO Map relationships
lazy val fromCondo:Condo = null
}
class ResidentRole(override val id:Long, val idUnit:Long) extends Role{
override val identifier = "ResidentRole"
//TODO Map relationships
lazy val fromHousingUnit:HousingUnit = null
}
class VisitorRole(val idFromUser:Long) extends Role {
override val identifier = "VisitorRole"
//TODO Map relationships
lazy val fromUser = null
}
And here my current "Role" entities:
create table Roles(
id int primary key,
identifier varchar(255) NOT NULL,
idUser int NOT NULL REFERENCES Users(id)
);
create table AdministratorRoles(
idRole int NOT NULL UNIQUE REFERENCES Roles(id),
idCondo int NOT NULL REFERENCES Condos(id)
);
create table ResidentRoles(
idRole int NOT NULL UNIQUE REFERENCES Roles(id),
idUnit int NOT NULL REFERENCES Units(id)
);
create table VisitorRoles(
idRole int NOT NULL UNIQUE REFERENCES Roles(id),
idFromUser int NOT NULL REFERENCES Users(id)
);
How can I map these models/entities? I'm really having a hard time. Since now, thanks for the help.