Let's consider the following objects :

  • Article which can be of different types
  • Equipment which point to an Article, some fields on equipment are only used for some types.

I want to add constraint to the Equipment, this constraint depend on the type or Article. I have three choices :

  1. Use triggers so I can query the type of article with another select
    • Pro : only database jobs
    • Coins : extra select. Until now I refrain to use triggers for "static(/generic ?)" constraint (= not depending of the old value like you can do in trigger and not in CHECK).
  2. copy the article_type in a dedicated column of equipment so I can apply a CHECK CONSTRAINT.
    • Pros : Easy to implements, all fits in the same table
    • Coins : Extra dedicated column.
  3. Use inheritance for each type of Article and each type of Equipment
    • Pros : apply differents constraint on each resulting table
    • Coins : more tables to manage, UNION/JOINS depending on the implementation.

The second solution would be implemented like this in the ORM part :

// ensure we're accessing using getter and not the non-existant field.
public ArticleTypeEnum getTypeOfArticle(){
    return article.getType();
protected void setTypeOfArticle(ArticleTypeEnum aEnum){
    // ignore silently

So this is fairly easy and the article won't ever be null when the getter will be called.

The third solution is currently not considered for myself but is part of the question.

This is because of others requirements : being able to query transparently between each type from the same view. This is technically not possible for JPA to query fields specific to one of the types, I would need a complicated workaround for this, if it's doable, considering my current stack.

So is my analysis right ? Is there others alternatives ?

My question does not apply only for the analysis of this only sample but on the whole database, which will likely result to a It depends answer.

Finaly is the method 2 has a name ? So I could search maybe for more information here on the net.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.