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

Asked

Viewed 724 times

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?

  • 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

1 answer

2

I ended up mapping in another way 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 Autenticacaopk.class class and added the annotation @Table(uniqueConstraints=@UniqueConstraint(columnNames = {"codEmpresa", "codUsuario"})), that created the UNIQUE Constraint for passwords and codUsuario. I also created an attribute to be the PK of Entity.

This way, I can’t enter a token for the same codUsuario with the same codEmpresa.

  • You tried to use @PrimaryKeyJoinColumn instead of @JoinColumn in its original strategy? Which version of Hibernate?

Browser other questions tagged

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