0

I'm not sure how to annotate the bellow classes correctly. What I'm trying to do is to have on the User entity, a list of roles for a mapped Asset.

@Entity
class Asset{
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    var id: Long = -1
}

@Entity
class Role {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    var id: Long = -1
}

@Entity
class User{
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    var id: Long = -1

    ???
    var roles: Map<Asset, Set<Role>> = HashMap()
}

From what I discovered hibernate does not support a map of sets (link here). So I tried other options as an intermediate class like this.

But I'm struggling with the annotations. Can someone please tell what annotations I have to put on the three classes?

If someone knows a better approach instead of the map, to have the user roles by asset, please tell.

pedroh
  • 53
  • 2
  • 8
  • As far as I understand, a JPA entity design has to be able to be converted to an [ERD](https://www.smartdraw.com/entity-relationship-diagram/). The `Map>` object is hardly achievable in an ERD. How did you design you data in your database? You might need to create an `AssetRoles` entity and have a `@OneToMany Collection` in your `User` entity. Unrelated to JPA but where the syntax `var xxx: ` comes from? – Al-un Nov 29 '17 at 20:56
  • Ups it's kotlin, I'm updating the tags. I'm open to the database design (it's done by annotations) but what makes sense to me was a user_role_asset table with user_id, role_id and asset_id. I was trying to use something like you said Collection but I'm struggling with the annotation. I can't seem to get it working. – pedroh Nov 29 '17 at 21:06
  • I would use OneToMany and ManyToOne annotations. 1 Asset to many Roles and 1 User to Many Assets. – Ursache Nov 29 '17 at 21:09
  • @Ursache can you provide some sample please? – pedroh Nov 29 '17 at 21:25

0 Answers0