1

I have a table who has a composite key(picture one) and one of them is a foreign key that refers another table (picture two)

How can I map this using hibernate???

enter image description here

enter image description here

The code below is the code I made. But its is not working. I think it is not the way to make this map.

The class CamposObrigatoriosID is the class that have

@Embeddable
public class CamposObrigatoriosID implements Serializable {

    private static final long serialVersionUID = 1L;


    @Column(name=“PERFIL_CMPOBR”)
    private Long perfil;    
    @Column(name=“ORDEM”)
    private Long ordem;
   @Column(name=“NOMETABELA”)
    private String nomeTabela;public Long getPerfil() {
        return perfil;
    }
...

    @Override
    public boolean equals(Object arg0) {
        if(arg0 instanceof CamposObrigatoriosID) {
            CamposObrigatoriosID that = (CamposObrigatoriosID) arg0;
            return this.perfil.equals(that.perfil) && this.ordem.equals(that.ordem) && this.nomeTabela.equals(that.nomeTabela);
        }
        return false;
    }
    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return perfil.hashCode() + ordem.hashCode() + nomeTabela.hashCode();
    }

@Embeddable
public class CamposObrigatoriosID implements Serializable {

    private static final long serialVersionUID = 1L;


    @Column(name=“PERFIL_CMPOBR”)
    private Long perfil;    
    @Column(name=“ORDEM”)
    private Long ordem;
   @Column(name=“NOMETABELA”)
    private String nomeTabela;
    .....

@Entity
@Table(name=“USUARIO_PERFIL”)
public class UsuarioPerfil {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name=“CODIGO_USRPERFIL”)
    private Long id;

    @Column(name=“DESCRICAO_USRPERFIL”)
    private String desccricaoUsrPerfil;
    ...

Error: SQLState:42000, ISC error code:335544351

Jan Černý
  • 1,268
  • 2
  • 17
  • 31
Sérgio Thiago Mendonça
  • 1,161
  • 2
  • 13
  • 23
  • 1
    If you want some help, please put directly your code in the question instead of links to images. – DamCx Apr 24 '18 at 13:40
  • Can we have the full `CamposObrigatoriosID` class, plz? Because your ID seems to be large... – DamCx Apr 24 '18 at 13:54
  • 1
    Possible duplicate of [JPA Composite Key with Object references](https://stackoverflow.com/questions/23066432/jpa-composite-key-with-object-references) – crizzis Apr 24 '18 at 14:54

1 Answers1

0

You should use a "derived identity". Although you do not include the code for CamposObrigatorios, I'm guessing it should look something like this:

@Entity
@Table(name = "CAMPOS_OBRIGATORIOS")
public class CamposObrigatorios {

    @EmbeddedId
    private CamposObrigatoriosID id;

    @ManyToOne
    @JoinColumn(name = "PERFIL_CMPOBR", referencedColumnName = "CODIGO_USRPERFIL")
    @MapsId("perfil") // maps 'perfil' attribute of embedded id
    private UsuarioPerfil usuarioPerfil;

    ...
}

Derived identities are discussed (with examples) in the JPA 2.1 spec in section 2.4.1.

Brian Vosburgh
  • 3,146
  • 1
  • 18
  • 18