Doubts how to leave a unique attribute - JPA

Asked

Viewed 1,825 times

1

I am creating an entity, and I have a CPF field and this field that is unique, how to map this CPF field?

My entity.

@Entity

public class Client {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long codigo;
private String nome;
private String cpf;
private String telefoneFixo;
private String telefoneCelular;
  • This can help you http://www.ibm.com/support/knowledgecenter/pt-br/SSRTLW_8.0.4/org.eclipse.jpt.doc.user/getting_started004.htm

2 answers

2

  • thank you! try here....

  • I tried to do this way, and allowed to save duplicate, now I did @Column(Unique=true) on top of the field Cpf got. @Table(uniqueConstraints={@Uniqueconstraint(columnNames={"Cpf"})})

  • I will do some testing and supplement my reply. Thank you for the feedback

2

Try:

@Column(unique=true)
private String cpf;

EDIT

The suggestion sent by adelmo00 is a better practice than the one I suggested because it allows the definition of one or more fields as unique. If you are sure that only the Cpf field needs to be unique there is no problem in using my suggestion, however, if you later want to define more attributes as unique, not to be typing @Column(Unique=true) for each field, I recommend using adelmo00’s suggestion.

  • thanks a friend for the help.

Browser other questions tagged

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