Put default value in column

Asked

Viewed 854 times

0

I need to leave a default value in this table column, I am mapping the Persistances with Hibernate (summarizing the database is created according to the HQL/JPA I define in Omains.

How do I set a default value for the column ?

@NotEmpty(message="O campo foto não pode ficar em branco !")
@Column(name = "foto_usuario", length = 50, nullable = false)
private String foto;

2 answers

1

As his colleague Wagner posted, an ex:

@NotEmpty(message="O campo foto não pode ficar em branco !")
@Column(name = "foto_usuario", nullable = false, columnDefinition="varchar(50) default 'same one value'")
private String foto;

or, those callback methods of hibernate, ex:

@PrePersist
void defaultValue() {
   foto = "sua imagem aqui";
}

Now to be honest, if it is a default value why not assign direct to the bean?

@NotEmpty(message="O campo foto não pode ficar em branco !") 
@Column(name = "foto_usuario", length = 50, nullable = false) 
private String foto = "sua foto default aqui";
  • But if I put it right in the bean like your last example, when the guy doesn’t put anything, he’ll get that one ? and when he setar a photo goes to what he sent, it’s the same thing ? works as default ?

  • Every time you instill an object it will get that property filled, if you are looking for the bank it will have the value that was previously assigned.

  • I think I’ll be clearer now, I need the user to put a url photo in the registration field, if he doesn’t put I want a pattern from our company, which you think is the most appropriate way to do this ?

  • the three way of right, I would leave configured directly on the object because this default image will not change, is one of those phantom images of registration that serves for everyone, but if you have an image of this for male user and another default for female user then you do not escape from having to use @Prepersiste

0

Try it this way:

@Column(name="suacoluna", columnDefinition="float default 0")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.