如何使用构造函数

时间:2016-01-05 09:49:12

标签: jpa constructor jpql

我必须从查询中创建一个对象列表,从数据库中检索数据并进行一些修改才能正确显示

例如我有一张表

Id | m1 | m2 | m3 | m4 | m5 | m6 | m8 | m9 | m10 | m11 | m12 |

1 | janvier | mars | juillet | december | null | null | null | null | null | null | null |

这是如何将行显示到我的数据库中 我想重新排列每一行,以便在我想要创建的列表中找到正确的位置 这应该怎么显示

Id | m1 | m2 | m3 | m4 | m5 | m6 | m8 | m9 | m10 | m11 | m12 |

1 | 1 | null | null | 1 | null | null | 1 | null | null | null | 1 |

这意味着如果他找到了janvier'他将把1放在第一列 如果他找到12月,他将会在第12栏中

直到现在我创建了名为visite的实体:

 package model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

@Entity
@NamedQueries({ @NamedQuery(name = "Visite.findAll", query = "select o from Visite o") })
public class Visite implements Serializable {
    private static final long serialVersionUID = -6069673717468618734L;
    @Column(length = 4000)
    private String annee;
    @Column(nullable = false, length = 100)
    private String client;
    @Column(length = 4000)
    private String dep;
    @Column(length = 4000)
    private String etat;
    @Id
    @Column(nullable = false, length = 100)
    private String idvisite;
    @Column(length = 4000)
    private String ingaff;
    @Column(length = 20)
    private String m1;
    @Column(length = 20)
    private String m10;
    @Column(length = 20)
    private String m11;
    @Column(length = 20)
    private String m12;
    @Column(length = 20)
    private String m2;
    @Column(length = 20)
    private String m3;
    @Column(length = 20)
    private String m4;
    @Column(length = 20)
    private String m5;
    @Column(length = 20)
    private String m6;
    @Column(length = 20)
    private String m7;
    @Column(length = 20)
    private String m8;
    @Column(length = 20)
    private String m9;
    @Column(length = 4000)
    private String moisvisite;
    @Column(length = 4000)
    private String nbvisites;
    @Column(length = 4000)
    private String prbvisite;

    public Visite() {
    }

    public Visite(String annee, String client, String dep, String etat, String idvisite, String ingaff, String m1,
                  String m10, String m11, String m12, String m2, String m3, String m4, String m5, String m6, String m7,
                  String m8, String m9, String moisvisite, String nbvisites, String prbvisite) {
        this.annee = annee;
        this.client = client;
        this.dep = dep;
        this.etat = etat;
        this.idvisite = idvisite;
        this.ingaff = ingaff;
        this.m1 = m1;
        this.m10 = m10;
        this.m11 = m11;
        this.m12 = m12;
        this.m2 = m2;
        this.m3 = m3;
        this.m4 = m4;
        this.m5 = m5;
        this.m6 = m6;
        this.m7 = m7;
        this.m8 = m8;
        this.m9 = m9;
        this.moisvisite = moisvisite;
        this.nbvisites = nbvisites;
        this.prbvisite = prbvisite;
    }

    public String getAnnee() {
        return annee;
    }

    public void setAnnee(String annee) {
        this.annee = annee;
    }

    public String getClient() {
        return client;
    }

    public void setClient(String client) {
        this.client = client;
    }

    public String getDep() {
        return dep;
    }

    public void setDep(String dep) {
        this.dep = dep;
    }

    public String getEtat() {
        return etat;
    }

    public void setEtat(String etat) {
        this.etat = etat;
    }

    public String getIdvisite() {
        return idvisite;
    }

    public void setIdvisite(String idvisite) {
        this.idvisite = idvisite;
    }

    public String getIngaff() {
        return ingaff;
    }

    public void setIngaff(String ingaff) {
        this.ingaff = ingaff;
    }

    public String getM1() {
        return m1;
    }

    public void setM1(String m1) {
        this.m1 = m1;
    }

    public String getM10() {
        return m10;
    }

    public void setM10(String m10) {
        this.m10 = m10;
    }

    public String getM11() {
        return m11;
    }

    public void setM11(String m11) {
        this.m11 = m11;
    }

    public String getM12() {
        return m12;
    }

    public void setM12(String m12) {
        this.m12 = m12;
    }

    public String getM2() {
        return m2;
    }

    public void setM2(String m2) {
        this.m2 = m2;
    }

    public String getM3() {
        return m3;
    }

    public void setM3(String m3) {
        this.m3 = m3;
    }

    public String getM4() {
        return m4;
    }

    public void setM4(String m4) {
        this.m4 = m4;
    }

    public String getM5() {
        return m5;
    }

    public void setM5(String m5) {
        this.m5 = m5;
    }

    public String getM6() {
        return m6;
    }

    public void setM6(String m6) {
        this.m6 = m6;
    }

    public String getM7() {
        return m7;
    }

    public void setM7(String m7) {
        this.m7 = m7;
    }

    public String getM8() {
        return m8;
    }

    public void setM8(String m8) {
        this.m8 = m8;
    }

    public String getM9() {
        return m9;
    }

    public void setM9(String m9) {
        this.m9 = m9;
    }

    public String getMoisvisite() {
        return moisvisite;
    }

    public void setMoisvisite(String moisvisite) {
        this.moisvisite = moisvisite;
    }

    public String getNbvisites() {
        return nbvisites;
    }

    public void setNbvisites(String nbvisites) {
        this.nbvisites = nbvisites;
    }

    public String getPrbvisite() {
        return prbvisite;
    }

    public void setPrbvisite(String prbvisite) {
        this.prbvisite = prbvisite;
    }
}

我创建了一个新对象,以便创建一个带有参数的构造函数,以便在我的会话bean中使用它

package model;

public class VisiteConstructeur {
    private String annee;
    private String client;
    private String dep;
    private String etat;
    private String idvisite;
    private String ingaff;
    private String m1;
    private String m10;

    private String m11;
    private String m12;
        private String m2;
    private String m3;
    private String m4;
    private String m5;
    private String m6;

    public void setAnnee(String annee) {
        this.annee = annee;
    }

    public String getAnnee() {
        return annee;
    }

    public void setClient(String client) {
        this.client = client;
    }

    public String getClient() {
        return client;
    }

    public void setDep(String dep) {
        this.dep = dep;
    }

    public String getDep() {
        return dep;
    }

    public void setEtat(String etat) {
        this.etat = etat;
    }

    public String getEtat() {
        return etat;
    }

    public void setIdvisite(String idvisite) {
        this.idvisite = idvisite;
    }

    public String getIdvisite() {
        return idvisite;
    }

    public void setIngaff(String ingaff) {
        this.ingaff = ingaff;
    }

    public String getIngaff() {
        return ingaff;
    }

    public void setM1(String m1) {
        this.m1 = m1;
    }

    public String getM1() {
        return m1;
    }

    public void setM10(String m10) {
        this.m10 = m10;
    }

    public String getM10() {
        return m10;
    }

    public void setM11(String m11) {
        this.m11 = m11;
    }

    public String getM11() {
        return m11;
    }

    public void setM12(String m12) {
        this.m12 = m12;
    }

    public String getM12() {
        return m12;
    }

    public void setM2(String m2) {
        this.m2 = m2;
    }

    public String getM2() {
        return m2;
    }

    public void setM3(String m3) {
        this.m3 = m3;
    }

    public String getM3() {
        return m3;
    }

    public void setM4(String m4) {
        this.m4 = m4;
    }

    public String getM4() {
        return m4;
    }

    public void setM5(String m5) {
        this.m5 = m5;
    }

    public String getM5() {
        return m5;
    }

    public void setM6(String m6) {
        this.m6 = m6;
    }

    public String getM6() {
        return m6;
    }

    public void setM7(String m7) {
        this.m7 = m7;
    }

    public String getM7() {
        return m7;
    }

    public void setM8(String m8) {
        this.m8 = m8;
    }

    public String getM8() {
        return m8;
    }

    public void setM9(String m9) {
        this.m9 = m9;
    }

    public String getM9() {
        return m9;
    }

    public void setMoisvisite(String moisvisite) {
        this.moisvisite = moisvisite;
    }

    public String getMoisvisite() {
        return moisvisite;
    }

    public void setNbvisites(String nbvisites) {
        this.nbvisites = nbvisites;
    }

    public String getNbvisites() {
        return nbvisites;
    }

    public void setPrbvisite(String prbvisite) {
        this.prbvisite = prbvisite;
    }

    public String getPrbvisite() {
        return prbvisite;
    }
    private String m7;
    private String m8;
    private String m9;
    private String moisvisite;
    private String nbvisites;
    private String prbvisite;
    public VisiteConstructeur(String annee, String client, String dep, String etat, String idvisite, String ingaff, String m1,
                  String m10, String m11, String m12, String m2, String m3, String m4, String m5, String m6, String m7,
                  String m8, String m9, String moisvisite, String nbvisites, String prbvisite) {


        this.annee = annee;
        this.client = client;
        this.dep = dep;
        this.etat = etat;
        this.idvisite = idvisite;
        this.ingaff = ingaff;
        this.m1 = m1;
        this.m10 = m10;
        this.m11 = m11;
        this.m12 = m12;
        this.m2 = m2;
        this.m3 = m3;
        this.m4 = m4;
        this.m5 = m5;
        this.m6 = m6;
        this.m7 = m7;
        this.m8 = m8;
        this.m9 = m9;
        this.moisvisite = moisvisite;
        this.nbvisites = nbvisites;
        this.prbvisite = prbvisite;
    }
}

这是我的功能,我被禁止制作条件:

public List MyListResult(){

Query query =
    em.createQuery("SELECT o FROM Visite o ");


List<Object[]> rows = query.getResultList();
List<VisiteConstructeur> result = new ArrayList<>(rows.size());
for (Object[] row : rows) {
    result.add(new VisiteConstructeur((String) row[0],(String) row[1],(String) row[2],(String) row[3],(String) row[4],(String) row[5],
                                      (String) row[6],(String) row[7],(String) row[8],(String) row[9],
                                     (String) row[10],(String) row[11],(String) row[12],(String) row[13],
                                      (String) row[14],(String) row[15],(String) row[16],(String) row[17],
                                    (String) row[18],(String) row[19],(String) row[20]));


}
return result;

}

1 个答案:

答案 0 :(得分:0)

使用以下代码。

public List<VisiteConstructeur> MyListResult() {
     List<VisiteConstructeur> result=  em.createQuery("select new model.VisiteConstructeur(annee,client,dep,etat,idvisite,ingaff,m1,
                  m10,m11,m12,m2,m3,m4,m5,m6,m7,
                  m8,m9,moisvisite,nbvisites,prbvisite)from Visite").list();
    return result;
}