Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

535
Visualizações
Hibernate with PostgreSQL using default value in Entity

I'm developping an API using Springboot and PostgreSQL 12. I created a user table with hibernate and the entity field that is problematic is the following:

@Column(columnDefinition = "BOOLEAN NOT NULL DEFAULT FALSE")
private Boolean emailVerificationStatus = false;

My table seems to be correctly generated :

create table users (
   id int8 generated by default as identity,
    email varchar(120) not null,
    email_verification_status boolean default false not null,
    email_verification_token varchar(255),
    encrypted_password varchar(255) not null,
    first_name varchar(50) not null,
    last_name varchar(50) not null,
    user_id varchar(255) not null,
    primary key (id)
)

My issue is while running the application I receive an error message :

org.hibernate.PropertyValueException: not-null property references a null or transient value : fr.mycompany.app.io.entity.UserEntity.emailVerificationStatus

By default, when I create the user, it sends a null value. I can solve this issue by setting the value to false in my service layer but it's not very elegant. Also, When I remove the option nullable = false, it works fine but the value is set to null instead of getting the default value. It seems that postgresql doesn't take the default value (false) as value when I try to send a user to my API. I want to force changing null value to false by default.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

According to the hibernate documentation you can try to use the following approach:

@Entity
@DynamicInsert
public class YourEntity {

  @ColumnDefault("false")
  private Boolean emailVerificationStatus;

  // ...
}

Please note that the entity above is annotated with the @DynamicInsert annotation so that the INSERT statement does not include any entity attribute that does not contain a value. So, this is allow you to avoid case described in the @MikeOrganek's answer.

over 4 years ago · Santiago Trujillo Relatório

0

You need to solve this in your service layer.

PostgreSQL is following the SQL standard. The default clause value is in effect only if the column is omitted from the column list or if default is specified in the value list. When Hibernate runs the insert, the emailVerificationStatus is explicitly included as null.

Please see: http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt

The section "<insert statement>," "General Rules," 4b, on page 389 defines insert behavior.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda