JPA+Eclipselink Composite Key with Object

Asked

Viewed 36 times

0

I am creating a class of stocks where the key of the table will be the product code+local, so that the system can control various stocks of the same product in different locations. I created an entity called Stocks and another called Estoquespk ( This to store the composite key), but when executing this returns the error below.

The Mapping [B2_COD] from the Embedded ID class [class entities.Estoquespk] is an invalid Mapping for this class. An embeddable class that is used with an Embedded ID Specification (attribute [id] from the source [class entities.Stocks]) can only contain basic mappings. Either remove the non basic Mapping or change the Embedded ID Specification on the source to be Embedded.

Below is the composite key

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;


@Embeddable
public class EstoquesPK implements Serializable {
    private static final long serialVersionUID = -637018809489152388L;
    
    private Produtos B2_COD;
    @Column(length = 2)
    private String B2_LOCAL;

    public Produtos getB2_COD() {
        return B2_COD;
    }

    public void setB2_COD(Produtos b2_COD) {
        B2_COD = b2_COD;
    }

    public String getB2_LOCAL() {
        return B2_LOCAL;
    }

    public void setB2_LOCAL(String b2_LOCAL) {
        B2_LOCAL = b2_LOCAL;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((B2_COD == null) ? 0 : B2_COD.hashCode());
        result = prime * result + ((B2_LOCAL == null) ? 0 : B2_LOCAL.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        EstoquesPK other = (EstoquesPK) obj;
        if (B2_COD == null) {
            if (other.B2_COD != null)
                return false;
        } else if (!B2_COD.equals(other.B2_COD))
            return false;
        if (B2_LOCAL == null) {
            if (other.B2_LOCAL != null)
                return false;
        } else if (!B2_LOCAL.equals(other.B2_LOCAL))
            return false;
        return true;
    }
    
    

}

Following Entity using the composite key

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;

@Entity
public class Estoques implements Serializable {
    private static final long serialVersionUID = 5999236902534007386L;

    @EmbeddedId
    private EstoquesPK id;

    private int B2_QATU;

    private double B2_CMUNIT;

    private double B2_VATU;

    private double B2_QFIM;

    private double B2_VFIM;

    @Column(length = 10)
    private String B2_DTFECHA;

    @Column(length = 10)
    private String B2_DTULTINV;

    @Column(length = 10)
    private String B2_DTCADASTRO;

    @Column(length = 10)
    private String B2_ALTERA;

    @Column(length = 10)
    private String B2_DTINTEG;

    public EstoquesPK getId() {
        return id;
    }

    public String getDescric() {
        return id.getB2_COD().getB1_DESC();
    }
    
    
    
    public void setId(EstoquesPK id) {
        this.id = id;
    }

    public int getB2_QATU() {
        return B2_QATU;
    }
    
    public void setB2_QATU(int b2_QATU) {
        B2_QATU = b2_QATU;
    }

    public double getB2_CMUNIT() {
        return B2_CMUNIT;
    }

    public void setB2_CMUNIT(double b2_CMUNIT) {
        B2_CMUNIT = b2_CMUNIT;
    }

    public double getB2_VATU() {
        return B2_VATU;
    }

    public void setB2_VATU(double b2_VATU) {
        B2_VATU = b2_VATU;
    }

    public double getB2_QFIM() {
        return B2_QFIM;
    }

    public void setB2_QFIM(double b2_QFIM) {
        B2_QFIM = b2_QFIM;
    }

    public double getB2_VFIM() {
        return B2_VFIM;
    }

    public void setB2_VFIM(double b2_VFIM) {
        B2_VFIM = b2_VFIM;
    }

    public String getB2_DTFECHA() {
        return B2_DTFECHA;
    }

    public void setB2_DTFECHA(String b2_DTFECHA) {
        B2_DTFECHA = b2_DTFECHA;
    }

    public String getB2_DTULTINV() {
        return B2_DTULTINV;
    }

    public void setB2_DTULTINV(String b2_DTULTINV) {
        B2_DTULTINV = b2_DTULTINV;
    }

    public String getB2_DTCADASTRO() {
        return B2_DTCADASTRO;
    }

    public void setB2_DTCADASTRO(String b2_DTCADASTRO) {
        B2_DTCADASTRO = b2_DTCADASTRO;
    }

    public String getB2_ALTERA() {
        return B2_ALTERA;
    }

    public void setB2_ALTERA(String b2_ALTERA) {
        B2_ALTERA = b2_ALTERA;
    }

    public String getB2_DTINTEG() {
        return B2_DTINTEG;
    }

    public void setB2_DTINTEG(String b2_DTINTEG) {
        B2_DTINTEG = b2_DTINTEG;
    }

}

The only thing I’m using differently is that in the composite key the field B2_COD is an object of the class Products. I have tried to do this mapping in several ways and have not succeeded. Please if any can help me I thank you. Hug

1 answer

0

You got a lot of problems there The first, nomenclature does not follow the conventions of Java, but going to the problem,

Second, you should define a meaningful name for the attributes, different from the BD column

ex:

@Column(name = "B2_QATU")
private String nomeSignificativo;

and finally the problem is that the Estoquespk should not contain other objects, so you will solve by declaring like this,

@Column(name = "B2_COD")
private String idProduto;

and in the Stocks class you can add a product reference more or less like this

@JoinColumn(updatable = false)
@OneToOne
@JoinColumn(name = "B2_COD")
private Produto produto

Browser other questions tagged

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