Another problem remains, but it is a data problem.
As it had been done in the book, there is a relationship between categories:
Code:
@ManyToOne
private Category parentCategory;
...
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE} mappedBy = "parentCategory")
private Set<Category> childCategories = new HashSet<Category>();
However, there is a chance to manipulate the database manually, and according to my first example, someone can insert a Child category whose related category isn't a Parent Category (according to the discriminator formula)
At this moment Hibernate cannot instantiate an appropriate object and the program simply crashes.
The actual problem is that in the error log is difficult to see that the there is a data problem.
Is it possible to catch the creation of the object before it's created in order to ensure that there won't be any problem?
Actually, I don't want to create a different kind of object, I only want to create a good warning containing the row that is creating the problem.
I tried with an Interceptor (extends EmptyInterceptor) executing a Query (entityManager.createNativeQuery) in the public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) method before returning super.onLoad(entity, id, state, propertyNames, types);.
But it failed (and the transaction is rollbacked)
Can anyone help me with this?