Michal W
Michal W

Reputation: 330

@NotNull on subclass getter affects parent class table

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:??

Postgres

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

Upvotes: 7

Views: 3426

Answers (3)

Michal W
Michal W

Reputation: 330

Thanks @Tahir.

I was testing same scenario but with annotation @Size.
Superclass(Ssss) had some String field:

@Inheritance(strategy = InheritanceType.JOINED)
class Ssss {
    @ManyToOne
    private String description;

    @Size(min = 100, max = 150)
    public String getDescription() {
       return description;
    }
}

@Inheritance(strategy = InheritanceType.JOINED)
class Yyyy extends Ssss {

    @Override
    @Size(min = 10, max = 60)
    public String getDescription() {
       return description;
    }        
}

@Inheritance(strategy = InheritanceType.JOINED)
class Zzzz extends Ssss {
    
    @Override
    @Size(min = 200, max = 255)
    public String getDescription() {
       return description;
    }
}

In such situation Hibernate generated column with same max width as for superclass (Ssss): enter image description here
Am I right that Hibernate couldn't resolve conflict so considered max only from Ssss? Do You know the rulles of resolving similar conflicts and what is Hibernate behavior when using annotations like @Pattern, @Min or @Max?

Regards

Upvotes: 0

vineeth sivan
vineeth sivan

Reputation: 510

@NotNull is a JSR 303 Bean Validation annotation. This is intended for validation, to ensure that the instance value of that Entity property (field) will bounce any processes that violate that constraint. This has nothing to do with the table creation

Instead if we use @Column(nullable = false) This is for the database. Specifically, when using the option to generate a schema (If you are using hibernate to generate DDL), it assures that the "NULLABLE" property is assigned to the corresponding database table column (field).

Please see the below link for more clarity Confusion: @NotNull vs @Column(nullable = false)

Upvotes: 0

Tahir Hussain Mir
Tahir Hussain Mir

Reputation: 2626

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.

Upvotes: 1

Related Questions