How can I make JPA not generate a CLASE_PK?

0

Having a Question table, and having another table called answers.

My problem is that when using JPA to be able to do the mapping of the tables, I create the class question and the class answers, apart from that I create an additional class that is questionPK , this class contains the IDs either idQuestion and idQuestionResponse.

My problem arises when carrying out the following implementation of the classes in the class MEncuestaPreguntasRepositoryImpl , since the Question class does not contain its ID, then I must make the query directly to the class QuestionPK , this class contains the class ID Question.

This is the structure of the project

This is the class MEncuestaPreguntasRepositoryImpl

@Repository
public class MEncuestaPreguntasRepositoryImpl implements MEncuestaPreguntasRepository {

    private final static Logger LOGGER = LoggerFactory.getLogger(MEncuestaPreguntasRepository.class);

    @PersistenceContext(unitName = BDEncuestaConf.NAME_PERSISTENCE_UNIT)
    private EntityManager entityManager;

    @Override
    public List<MEncuestaPreguntas> obtenerPreguntas(int codEncuesta) throws Exception {
        LOGGER.info("OBTENER PREGUNTAS....");
        try {

            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery cq = cb.createQuery();
            Root<Preguntas> root = cq.from(Preguntas.class);

            cq.select(root).where(
                    cb.equal(root.get(MPreguntasPK_.codPregunta).get(MRespuesta_.codPregunta), codEncuesta)
            );

            Query q = entityManager.createQuery(cq);
            return q.getResultList();
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            throw e;
        }
    } 

The next error it shows me is when I try to add the id directly from the QuestionPK class.

no suitable method found for get(SingularAttribute<MEncuestaPreguntasPK,Integer>)
    method Path.<Y#1>get(SingularAttribute<? super MEncuestaPreguntas,Y#1>) is not applicable

This is the Question class,

@Entity
@Table(schema = "dbo", name = "M_ENCUESTA_PREGUNTAS")
public class MEncuestaPreguntas implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected MEncuestaPreguntasPK mEncuestaPreguntasPK;
    @Basic(optional = false)
    @Column(name = "DESCRIPCION")
    private String descripcion;
    @Column(name = "ESRESPUESTAMULTIPLE")
    private Boolean esrespuestamultiple;
    @Column(name = "ORDEN")
    private Integer orden;
    @Column(name = "TIPO")
    private Character tipo;
    @Column(name = "FLKOBSERVACION")
    private Boolean flkobservacion;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "mEncuestaPreguntas", fetch = FetchType.LAZY)
    private Set<MEncuestaRespuestas> mEncuestaRespuestasSet;
    @JoinColumn(name = "CODENCUESTA", referencedColumnName = "CODENCUESTA", insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private MEncuesta mEncuesta;

    public MEncuestaPreguntas() {
    }

    public MEncuestaPreguntas(MEncuestaPreguntasPK mEncuestaPreguntasPK) {
        this.mEncuestaPreguntasPK = mEncuestaPreguntasPK;
    }

    public MEncuestaPreguntas(MEncuestaPreguntasPK mEncuestaPreguntasPK, String descripcion) {
        this.mEncuestaPreguntasPK = mEncuestaPreguntasPK;
        this.descripcion = descripcion;
    }

    public MEncuestaPreguntas(int codencuesta, int codpregunta) {
        this.mEncuestaPreguntasPK = new MEncuestaPreguntasPK(codencuesta, codpregunta);
    }

    public MEncuestaPreguntasPK getMEncuestaPreguntasPK() {
        return mEncuestaPreguntasPK;
    }

    public void setMEncuestaPreguntasPK(MEncuestaPreguntasPK mEncuestaPreguntasPK) {
        this.mEncuestaPreguntasPK = mEncuestaPreguntasPK;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public Boolean getEsrespuestamultiple() {
        return esrespuestamultiple;
    }

    public void setEsrespuestamultiple(Boolean esrespuestamultiple) {
        this.esrespuestamultiple = esrespuestamultiple;
    }

    public Integer getOrden() {
        return orden;
    }

    public void setOrden(Integer orden) {
        this.orden = orden;
    }

    public Character getTipo() {
        return tipo;
    }

    public void setTipo(Character tipo) {
        this.tipo = tipo;
    }

    public Boolean getFlkobservacion() {
        return flkobservacion;
    }

    public void setFlkobservacion(Boolean flkobservacion) {
        this.flkobservacion = flkobservacion;
    }

    @XmlTransient
    public Set<MEncuestaRespuestas> getMEncuestaRespuestasSet() {
        return mEncuestaRespuestasSet;
    }

    public void setMEncuestaRespuestasSet(Set<MEncuestaRespuestas> mEncuestaRespuestasSet) {
        this.mEncuestaRespuestasSet = mEncuestaRespuestasSet;
    }

    public MEncuesta getMEncuesta() {
        return mEncuesta;
    }

    public void setMEncuesta(MEncuesta mEncuesta) {
        this.mEncuesta = mEncuesta;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (mEncuestaPreguntasPK != null ? mEncuestaPreguntasPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof MEncuestaPreguntas)) {
            return false;
        }
        MEncuestaPreguntas other = (MEncuestaPreguntas) object;
        if ((this.mEncuestaPreguntasPK == null && other.mEncuestaPreguntasPK != null) || (this.mEncuestaPreguntasPK != null && !this.mEncuestaPreguntasPK.equals(other.mEncuestaPreguntasPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.adenbpo.encuestas.repository.entity.MEncuestaPreguntas[ mEncuestaPreguntasPK=" + mEncuestaPreguntasPK + " ]";
    }

}

And this is the QuestionPK class:

@Embeddable
public class MEncuestaPreguntasPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "CODENCUESTA")
    private int codencuesta;
    @Basic(optional = false)
    @Column(name = "CODPREGUNTA")
    private int codpregunta;

    public MEncuestaPreguntasPK() {
    }

    public MEncuestaPreguntasPK(int codencuesta, int codpregunta) {
        this.codencuesta = codencuesta;
        this.codpregunta = codpregunta;
    }

    public int getCodencuesta() {
        return codencuesta;
    }

    public void setCodencuesta(int codencuesta) {
        this.codencuesta = codencuesta;
    }

    public int getCodpregunta() {
        return codpregunta;
    }

    public void setCodpregunta(int codpregunta) {
        this.codpregunta = codpregunta;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int) codencuesta;
        hash += (int) codpregunta;
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof MEncuestaPreguntasPK)) {
            return false;
        }
        MEncuestaPreguntasPK other = (MEncuestaPreguntasPK) object;
        if (this.codencuesta != other.codencuesta) {
            return false;
        }
        if (this.codpregunta != other.codpregunta) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.adenbpo.encuestas.repository.entity.MEncuestaPreguntasPK[ codencuesta=" + codencuesta + ", codpregunta=" + codpregunta + " ]";
    }

}

Is it possible to make the entities CLASSES_PK before doing the mapping?

THANK YOU IN ADVANCE.

    
asked by Dominic A.Villanueva 25.10.2018 в 04:27
source

0 answers