How to model a polymorphic relationship in Hibernate 6 for foreign entities with different key types?

I have the following tables (pseudocode):

conversations
  id bigserial

subscribers
  id serial

asset_categories
  id smallserial

requirements
  id serial
  requireable_type text
  requireable_id bigint

For Hibernate I have a MappedSuperclass similar to this:

@MappedSuperclass
public abstract class BaseEntity<T extends Serializable> {
  private T id;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public T getId() {
    return id;
  }
}

And I have these entities:

Conversation<Long>
Subscriber<Integer>
AssetCategory<Short>
Requirement<Integer>

As you can see, requirements.requireable_type and requirements.requireable_id supports a polymorphic association to the three other types (and bigint of requireable_id covers the id types of the foreign keys).

But I haven’t been able to come up with the way to implement this in my Requirement entity.

I’ve tried an @Any like this in Requirement:

  @Any(fetch = FetchType.LAZY)
  @AnyDiscriminator(DiscriminatorType.STRING)
  @AnyDiscriminatorValues({
    @AnyDiscriminatorValue(discriminator = "subscriber", entity = Subscriber.class),
    @AnyDiscriminatorValue(discriminator = "conversation", entity = Conversation.class),
    @AnyDiscriminatorValue(discriminator = "asset_category", entity = AssetCategory.class)
  })
  @AnyKeyJavaClass(Long.class)
  @Column(name = "requireable_type")
  @JoinColumn(name = "requireable_id")
  public Requireable getRequireable() { return requireable; }  // I have an interface Requireable they all "implement"

But then only the Conversation can be associated and the Requirement persisted. The others fail with errors like java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long (java.lang.Integer and java.lang.Long are in module java.base of loader 'bootstrap')

This seems like a normal-enough scenario where the polymorphic relationship is to various tables with different types of identifiers.

So… how do I support this relationship? Should I jettison the @Any annotations and create … something? A CompositeUserType?

2

I asked in the comment if all the IDs are generated, because I was already thinking that in that case you can manage them all as Long attributes, and let Hibernate store in that the values that are passed from the DB layer. In this case, you won’t fall in the case of having a value in an id field that overflows the capacity of the associated column type, during INSERTs.

I was also thinking if you could use the columnDefinition attribute of the @Column annotation to specify the correct db type (e.g. to support a coherent automatic schema generation for tests), but if the annotation is put on the superclass, then you can’t, unless moving them on an overriding method on the subclasses.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật