I have a question related to javax.validation.constraints.NotNull annotation
.
In my project i do have classes tree like following :
@Inheritance(strategy = InheritanceType.JOINED)
class Ssss {
@ManyToOne
private Xxxx x;
public Xxxx getXxxx() {
return x;
}
}
@Inheritance(strategy = InheritanceType.JOINED)
class Yyyy extends Ssss {
@Override
//some not important annotations
public Xxxx getXxxx() {
return super.getXxxx();
}
}
@Inheritance(strategy = InheritanceType.JOINED)
class Zzzz extends Ssss {
@Override
//some not important annotations
@NotNull
public Xxxx getXxxx() {
return super.getXxxx();
}
}
This three classes are stored in database as three tables.
For schema creation i'm using Hibernate
:
hibernate.hbm2ddl.auto=create
Is it an expected behavior that Hibernate adds NOT NULL
on xxxx_object_id
field stored in table generated for super class ssss like following:??
I could not find any relevant information about how hibernate treats @NotNull on inherited getters.
Can anyone help me on this issue?
Best Regards.
Michal
Yes. Hibernate has constraints which it keeps checking in case of conflicts.
Here is an example:
@Inheritance(strategy = InheritanceType.JOINED)
class Ssss {
@ManyToOne
private Xxxx x;
public Xxxx getXxxx() {
return x;
}
}
If it was this much, then hibernate has no Conflict as it makes x
of type Xxxx
as null
But here is the issue, in this code:
@Inheritance(strategy = InheritanceType.JOINED)
class Zzzz extends Ssss {
@Override
//some not important annotations
@NotNull
public Xxxx getXxxx() {
return super.getXxxx();
}
}
Here Hibernate via @NotNull
annotation is told to make the x
type of Xxxx
as @NotNull
In the above two cases, there is a conflict, for Ssss it can be Null and Zzzz it cannot be null. To infer that and resolve the conflict, Hibernate makes Xxxx type variable of Ssss as NotNull as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With