@NotNull on subclass getter affects parent class table
Asked Answered
H

3

7

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

Helical answered 7/2, 2017 at 8:37 Comment(0)
T
1

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.

Teetotaler answered 7/2, 2017 at 9:18 Comment(0)
L
0

@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)

Leelah answered 7/2, 2017 at 9:30 Comment(1)
Thanks for Your response but You are not entirely right. As @PascalThivent wrote "If a property of your entity is annotated @NotNull, its columns will be declared as not null in the DDL schema generated by Hibernate." Look at this topic: #3677278 After changing hibernate.validator.apply_to_ddl to false, there was no NOT NULL on column xxxx_object_id :)Helical
H
0

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

Helical answered 8/2, 2017 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.