0

I use SINGLE_TABLE inheritance startegy to map my usres (see code example bellow).

Is there a way to map UnActiveRegularUser and UnActiveBusinessUser from "ACTIVE_USERS" table to another table, for example "UNACTIVE_USERS" and keep the inheritance startegy?

Note:

-The point here is to avoid code duplication between ex. RegularUser Vs UnActiveRegularUser (since they use the same properties) but still to map them to 2 different tables: "ACTIVE_USERS" and "UNACTIVE_USERS".

-strategy = InheritanceType.SINGLE_TABLE should not be changed.

-May adding another abstraction layer solve this problem?

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "ACTIVE_USERS")
public class User {
    @Id @GeneratedValue
    protected Long id;

    @Column(nullable = false)
    protected String name;
}


@Entity
public class RegularUser extends User{
//more getters and settres
}

@Entity
public class UnActiveRegularUser extends User{
//same getters and setters as in RegularUser
}

@Entity
public class BusinessUser extends User {
//more getters and settres
}

@Entity
public class UnActiveBusinessUser extends User {
//same getters and setters as in BusinessUser
}

Thanks, Nathan

1 Answers1

0

Persisting fields to another table won't prevent code duplication. I think you should just make UnActiveBusinessUser extend BusinessUser, and UnactiveRegularUser extend RegularUser.

Note that if a user can become unactive (i.e. it is a RegularUser and becomes an UnactiveRegularUser), inheritance is not the right solution: an object can't go from one type to another. Since it seems UnactiveRegularUser doesn't have anything more than RegularUser, I'm not sure this subclass is useful.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I separated RegularUser and UnActiveRegularUser to 2 entities, cause as far as I know, you can't map 1 entity to 2 different columns.The problem with the inheritances style you suggested (ex.UnActiveBusinessUser extend BusinessUser) is that they will still be mapped to @Table(name = "ACTIVE_USERS")...Is there a way to separate the mapping? – user1062078 Jul 08 '12 at 09:46
  • I don't really understand what you want. You want to keep the inheritance strategy (all enetities mapped to a single table), but you also want to map entities to several tables. This is contradictory. – JB Nizet Jul 08 '12 at 09:49
  • I want to map RegularUser & BusinessUser to "ACTIVE_USERS" and UnActiveRegularUser & UnActiveBusinessUser to "UNACTIVE_USERS" table.I used inheritance to avoid code duplication at the entity definition level, but the problem then is that all the entities are still mapped only to "ACTIVE_USERS" table... – user1062078 Jul 08 '12 at 10:05
  • JPA doesn't support mixed inheritance strategies in a hierarchy of classes. I no enetiy has any association to a `User` (and only associations to concrete subclasses), you should map the User as a MappedSuperclass, and have two hierarchies (with ActiveUser and InactiveUser as roots, both extending User) mapped with single table inheritance. – JB Nizet Jul 08 '12 at 10:13