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 was as follows:
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
The problem is that the UNIQUE (codusuario) and UNIQUE (codempresa) are being created. The Constraint I wanted to create was only so that the three columns did not repeat themselves and the Foreign. Even putting "Unique=false" the mentioned Constraint were created.
Updating
The Authenticatcaopk 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() {}
}
How is your class
AutenticacaoPK
?– Victor Stafusa
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, Stringtoken) {
this.empresa = empresa;
this.token= token;
this.usuario = usuario;
}
AutenticacaoPK() {}}
@Victorstafusa– raphaRezzi