0

I have an abstract class User which is extended by RegularUser and BusinessUser. The tables in MySQL are RegularUser and BusinessUser. I created the abstract User class as both RegularUser and BusinessUser contain similar data.

So if I were to have the Id annotation in the User class like so:

public abstract class User {
    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long userId;
    // ...
}

And the Entity/table declaration in the BusinessUser and RegularUser classes like this:

@Entity
@Table(name="BUSINESS_USER")
public class BusinessUser extends User {
    // ...
}

and this:

@Entity
@Table(name="REGULAR_USER")
public class RegularUser extends User {
    // ...
}

Would this work properly? Or do both declarations need to be in the same class? Is this possible/allowed? I'm simply a bit confused on how to approach this.

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
Jake Miller
  • 2,432
  • 2
  • 24
  • 39
  • The id annotation, IIRC, needs to belong in the concrete class declaration and not the abstract class. Then, you can use abstract methods for get and setid without having an id field in the abstract class. – Compass Jun 16 '16 at 19:30
  • @Compass so I should move the Id to the BusinessUser and the RegularUser classes then? Have those classes hold the Id's so I can set both the Id and Entity annotations in those classes? I'll try this out and let you know how it works! Thanks – Jake Miller Jun 16 '16 at 19:32
  • Ah, there is actually a way to do this using: http://stackoverflow.com/questions/4089376/hibernate-id-via-inheritance depends on whether or not you want a unified ID generation, or if you could potentially want separate ID generators for each type of user, i.e. BU has IDs that start with 7. – Compass Jun 16 '16 at 19:38
  • @Compass This helps. It also lead me to a few more topics/questions which also help me out and answered my questions. Thank you! – Jake Miller Jun 16 '16 at 19:43

0 Answers0