Question

Is it possible to have a parent abstract class say "Human", with "name" and "phone" both required (no nullable) and then subclass it with another entity say "Man" and make the "phone" field nullable?

I couldn't find this in the docs, so either is very easy or can´t be done. I ask it because I have a somehow big entity, and my sub entity only makes a couple of those fields mandatory. It would be a pain to create a whole new class and break inheritance.

PD: The inverse case also works for me: Have all fields nullable, and only make required the ones I really want in the sub entity.

Was it helpful?

Solution

If Human is MappedSuperClass you can use AttributeOverride

Man:
  type: entity
  attributeOverride:
    phone:
      nullable: false

And with annotations:

@AttributeOverrides({
    @AttributeOverride(
        name="phone",
        column=@Column(nullable=false)
    )
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top