0

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.

adheus
  • 3,985
  • 2
  • 20
  • 33
  • This is a database modelling question, not a Scala question. Essentially what you need are three tables: a user table, a role_type table, a mapping table that maps a user to a role_type. –  Jan 20 '15 at 21:31
  • I don't know if Trait is the better solution too on this case. So this is why I tagged Scala. – adheus Jan 20 '15 at 21:35
  • The use of `trait` here is fine, actually recommended. –  Jan 20 '15 at 21:37

0 Answers0