[hibernate-dev] IdClass superclasses

Gail Badner gbadner at redhat.com
Mon Dec 9 14:56:35 EST 2019


Hi,

Suppose we have the following:

public class CompositeKey implements Serializable {

  private Long id1;

  private Long id2;

  public CompositeKey(Long id1, Long id2) {
    super();
    this.id1 = id1;
    this.id2 = id2;
  }
...
}
public class InheritedKey extends CompositeKey {

  private Long id3;

  public InheritedKey(Long id1, Long id2, Long id3) {
    super(id1, id2);
    this.id3 = id3;
  }
...
}

@Entity(name = "IKE")
@IdClass(InheritedKey.class)public class InheritedKeyEntity {

  @Id
  private Long id1;

  @Id
  private Long id2;

  @Id
  private Long id3;

  private String name;
...
}


Currently, Hibernate does not include InheritedKey#id3 in
InheritedKeyEntity's composite key unless InheritedKey is annotated with
@MappedSuperclass.

I think there are some improvements that can be made.

1) As you can see, the entity, itself, has all of the ID fields annotated.
An improvement would be for Hibernate to ensure that all mapped ID
properties have been detected, and throw an exception if this is not the
case.

2) A class used as an IdClass does not need any annotations (as opposed to
a class used for an EmbeddedId class, which requires {{@Embeddable}}).
Since the class used as an IdClass does not need to be annotated, it seems
kind of strange that its superclass would need to be annotated with
{{@MappedSuperclass}} in order for its fields/properties to be persistent.
Since the field/property names must match what is annotated in an IdClass,
it is clear that the field/property in a superclass is intended to be an
ID. Perhaps we could make annotating the superclass with
{{@MappedSuperclass}} optional?

Opinions?

Thanks,
Gail


More information about the hibernate-dev mailing list