I'm having this in my @Entity class UpcomingOffer
:
@Entity
public class UpcomingOffer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private Long id;
@NotNull // Must not be null!
@OneToOne
private Store store;
// ...
}
but for some reason I am able to save a StoreOffer
without having the Store
set:
UpcomingOffer upcomingOffer = new UpcomingOffer();
DateTime date = new DateTime(upcomingOfferDto.getDate());
upcomingOffer.setDate(date);
upcomingOffer.setStore(null);
Saving it does not cause an Exception:
Session session = getSessionFactory().getCurrentSession();
session.saveOrUpdate(upcomingOffer);
which causes the entry of the upcoming offer to be updated and not being associated with the Store
that owns that UpcomingOffer
.
Why isn't Hibernate throwing an exception here when it is annotated with @NotNull
?
Try adding hibernate-validator to your classpath
If using Maven add this to your POM:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.0.Final</version>
</dependency>
If gradle add this to build.gradle
'org.hibernate:hibernate-validator:4.3.0.Final'
If you are not using a dependency management / build tool, then you would have to manually download the jar and place it in your classpath
Another possible issue, maybe related, maybe not, your @OneToOne should be paired with a @JoinColumn on either the Store entity or the UpcomingOffer entity. If you are doing bidriectional mapping one of them should have the mappedBy property defined as well. If you are letting Hibernate generate the database, its possible it is not generating it as intended too.