How to map entity with hibernate to create non-unique constraints?

4

I'm trying to create an entity with the following mapping:

@Entity
@IdClass(AutenticacaoPK.class)
public class Autenticacao {

  @Id
  @OneToOne(cascade = CascadeType.REFRESH)
  @JoinColumn(name = "codUsuario", unique = false)
  private Usuario usuario;

  @Id
  @OneToOne(cascade = CascadeType.REFRESH)
  @JoinColumn(name = "codEmpresa", unique = false)
  private Empresa empresa;

  @Id
  @Column(nullable = false, length = 1000)
  private String token;
}

And in the database the table looks like this:

CREATE TABLE autenticacao(
 token character varying(1000) NOT NULL,
 codusuario bigint NOT NULL,
 codempresa bigint NOT NULL,
CONSTRAINT autenticacao_pkey PRIMARY KEY (codempresa, token, codusuario),
CONSTRAINT fk_92assff1b6x84mm97rlb7jl7m FOREIGN KEY (codusuario)
 REFERENCES usuario (codusuario) MATCH SIMPLE
 ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_hb45dbp517t2eyghrwfo4ktqh FOREIGN KEY (codempresa)
 REFERENCES empresa (codempresa) MATCH SIMPLE
 ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT uk_92assff1b6x84mm97rlb7jl7m UNIQUE (codusuario),// NAO CRIAR
CONSTRAINT uk_hb45dbp517t2eyghrwfo4ktqh UNIQUE (codempresa))// NAO CRIAR

Problem is that the constraints UNIQUE (codusuario) and UNIQUE (codempresa) are being created. The constraint I wanted to create was only so that the three columns would not repeat themselves and the Foreign. Even putting "unique = false" the mentioned constraints were created.

Update

The PK Authentication class:

 public class AutenticacaoPK implements Serializable {
    private static final long serialVersionUID = 6769917015564615074L;
    protected Usuario usuario;
    protected Empresa empresa;
    protected String token;

    public AutenticacaoPK(Usuario usuario, Empresa empresa, String token) {
       this.empresa = empresa;
       this.token = token;
       this.usuario = usuario;
    }

    AutenticacaoPK() {}
}
    
asked by anonymous 11.06.2015 / 20:09

1 answer

2

I ended up mapping differently and got what I wanted (partially).

@Entity
//@IdClass(AutenticacaoPK.class)
@Table(uniqueConstraints=@UniqueConstraint(columnNames = {"codEmpresa", "codUsuario"}))
public class AutenticacaoMobile {

  @Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE)
  private Long id;

  @OneToOne(cascade = CascadeType.REFRESH)
  @JoinColumn(name = "codUsuario", unique = false)
  private Usuario usuario;

  @OneToOne(cascade = CascadeType.REFRESH)
  @JoinColumn(name = "codEmpresa", unique = false)
  private Empresa empresa;

@Column(nullable = false, length = 1000)
private String token;

I removed the AuthenticationPK.class class and added the @Table(uniqueConstraints=@UniqueConstraint(columnNames = {"codEmpresa", "codUsuario"})) annotation, which created the UNIQUE constraint for co-key and co-user keys. I also created an attribute to be the entity's PK.

In this way, I can not insert a token for the same user code with the same code.

    
15.06.2015 / 21:39