[hibernate-dev] IdClass superclasses

Steve Ebersole steve at hibernate.org
Tue Dec 10 16:30:26 EST 2019


Actually I was thinking of @EmbeddedId, but you were asking about @IdClass.

I think (2) sounds more natural.


On Tue, Dec 10, 2019 at 3:12 PM Steve Ebersole <steve at hibernate.org> wrote:

> To me this is perfectly consistent with how inheritance works in entity
> hierarchies.  Unless a super type is marked with @Entity
> or @MappedSuperclass, the "attributes" defined on that specific class are
> not picked up.
>
> Maybe I'm misunderstanding you?
>
> On Mon, Dec 9, 2019 at 1:58 PM Gail Badner <gbadner at redhat.com> wrote:
>
>> 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
>> _______________________________________________
>> hibernate-dev mailing list
>> hibernate-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/hibernate-dev
>>
>>


More information about the hibernate-dev mailing list