当前位置: 首页 > 知识库问答 >
问题:

使用JPA NamedQuery获取结果列表

虞滨海
2023-03-14

我想使用以下查询获得结果列表:

SELECT x FROM XTreinamentosOrc x WHERE x.codEduca NOT IN (SELECT DISTINCT y.codCurso y FROM XRlz y WHERE y.matricula = :matricula)

我在它的实体类中使用了它,但我得到了异常:

Caused by: javax.el.PropertyNotFoundException: The class 'br.com.bb.upb.diage.atb_treinamentos.entities.XTreinamentosOrc' does not have the property 'matricula'.

当用户成功登录应用程序时,将获得matricula字段。应该通过CDI在NamedQuery中设置它来查询数据库。matricula字段位于另一个不同于此处映射的表中。

如何设置注入的setsionChave字符串在参数的矩阵,这是XRlzConsolado类类型?

豆子:

@Named(value = "ExtratoBean")
@RequestScoped
public class ExtratoBean implements Serializable {

@EJB
private XTreinamentosOrcFacade xt;

@Inject
@SessionChave
private String sessionChave;

private List<XTreinamentosOrc> xTreinamentosOrcList;

public List<XTreinamentosOrc> getxTreinamentosOrcList() {
    return QueryExtratoTreinamentos();
}

private List QueryExtratoTreinamentos() {
    List<XTreinamentosOrc> extratoTreinamentosOrcList = null;
    XRlzConsolidado x = new XRlzConsolidado();
    x.setMatricula(sessionChave);
    extratoTreinamentosOrcList = xt.findAllTreinamentosByMatricula(x);
    return extratoTreinamentosOrcList;
}
}

有3个实体:XRlz、XRlzConsolado和XTreinamentosOrc。

XRlz:

@Entity
@Table(name = "XRlz", catalog = "diage", schema = "atb")
@XmlRootElement
//named queries

public class XRlz implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Size(max = 100)
@Column(name = "nome")
private String nome;
@Column(name = "uorPos")
private Integer uorPos;
@Size(max = 100)
@Column(name = "nomeUorPos")
private String nomeUorPos;
@Column(name = "codCurso")
private Integer codCurso;
@Size(max = 100)
@Column(name = "nomeCurso")
private String nomeCurso;
@Column(name = "qtdCursos2015")
private Integer qtdCursos2015;
@Size(max = 10)
@Column(name = "dtConclusao")
private String dtConclusao;
@JoinColumn(name = "matricula", referencedColumnName = "matricula")
@ManyToOne
private XRlzConsolidado matricula;
@JoinColumn(name = "dtRef", referencedColumnName = "id")
@ManyToOne
private XDatas dtRef;

//GETTERS AND SETTERS

public XRlzConsolidado getMatricula() {
    return matricula;
}

public void setMatricula(XRlzConsolidado matricula) {
    this.matricula = matricula;
}

public XDatas getDtRef() {
    return dtRef;
}

public void setDtRef(XDatas dtRef) {
    this.dtRef = dtRef;
}

//equals/hashcode/toString 

}

XRlzConsolidado:

@Entity
@Table(name = "XRlzConsolidado", catalog = "diage", schema = "atb")
@XmlRootElement
// NamedQueries

public class XRlzConsolidado implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 8)
    @Column(name = "matricula")
    private String matricula;
    @Size(max = 100)
    @Column(name = "nome")
    private String nome;
    @Column(name = "uorPos")
    private Integer uorPos;
    @Size(max = 100)
    @Column(name = "nomeuorPos")
    private String nomeuorPos;
    @Column(name = "capacitacaoGestores")
    private Integer capacitacaoGestores;
    @Column(name = "capacitacaoAssessores")
    private Integer capacitacaoAssessores;
    @Column(name = "horas")
    private Integer horas;
    @Column(name = "bemEstar")
    private Integer bemEstar;
    @Column(name = "inovEfic")
    private Integer inovEfic;
    @Column(name = "qtdCursos2015")
    private Integer qtdCursos2015;
    @OneToMany(mappedBy = "matricula")
    private Collection<XRlz> xRlzCollection;

    public XRlzConsolidado() {
    }

    public XRlzConsolidado(String matricula) {
        this.matricula = matricula;
    }

    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

// getters and setters

    @XmlTransient
    public Collection<XRlz> getXRlzCollection() {
        return xRlzCollection;
    }

    public void setXRlzCollection(Collection<XRlz> xRlzCollection) {
        this.xRlzCollection = xRlzCollection;
    }

    // hashCode()

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof XRlzConsolidado)) {
            return false;
        }
        XRlzConsolidado other = (XRlzConsolidado) object;
        if ((this.matricula == null && other.matricula != null) || (this.matricula != null && !this.matricula.equals(other.matricula))) {
            return false;
        }
        return true;
    }

    // toString
}

XTreinamentosOrc:

@Entity
@Table(name = "XTreinamentosOrc", catalog = "diage", schema = "atb")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "XTreinamentosOrc.findAll", query = "SELECT x FROM XTreinamentosOrc x"),
@NamedQuery(name = "XTreinamentosOrc.findAllTreinamentosByMatricula", query = "SELECT x FROM XTreinamentosOrc x WHERE x.codEduca NOT IN (SELECT DISTINCT y.codCurso FROM XRlz y WHERE y.matricula = :matricula)")})

public class XTreinamentosOrc implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "codEduca")
    private int codEduca;
    @Size(max = 100)
    @Column(name = "nomeCurso")
    private String nomeCurso;
    @Column(name = "cargaHoraria")
    private Integer cargaHoraria;
    @JoinColumn(name = "tipo", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private XTipo tipo;
    @JoinColumn(name = "segmento", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private XSegmento segmento;
    @JoinColumn(name = "modalidade", referencedColumnName = "id")
    @ManyToOne
    private XModalidade modalidade;
    @JoinColumn(name = "dtRef", referencedColumnName = "id")
    @ManyToOne
    private XDatas dtRef;

    public XTreinamentosOrc() {
    }

    public XTreinamentosOrc(Integer id) {
        this.id = id;
    }

    public XTreinamentosOrc(Integer id, int codEduca) {
        this.id = id;
        this.codEduca = codEduca;
    }

    // hashCode() {

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof XTreinamentosOrc)) {
            return false;
        }
        XTreinamentosOrc other = (XTreinamentosOrc) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
    }

提前谢谢。

共有2个答案

商德泽
2023-03-14

你能发布XRlzConsolidado的代码吗?它可能包含一个字符串字段,您希望与sessionhave进行比较。您正在比较字段XRlz。带有字符串值的矩阵,但其类型为XRlzConsolidado

贲高寒
2023-03-14

我相信问题出在您的查询名称上。findAllTreinamentosByMatcera()是一个属性表达式,只能引用被管理实体的直接属性。findAllTreinamentosByMatcera将在XTreinamentosOrc类中查找名为maticia的属性。当在XTreinamentosOrc中找不到该属性时,您会得到异常

原因:javax。艾尔。PropertyNotFoundException:类“br”。通用域名格式。bb。upb。帝亚吉欧。阿特布·特雷纳曼托斯。实体。XTreinamentosOrc'没有属性“matricula”。

您需要将查询的名称更改为:

(我可能会取消方法名'find(),因此您可能需要对可接受的格式进行更多研究)

查看以下文档:

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions

 类似资料:
  • 我不熟悉使用CRUD存储库。 我有一个包含三列的数据库表: course_id,姓名,当然 我想得到一份course\u id name的列表,例如, 但是,我不想使用查询,而是使用crud存储库。 我的控制器显示有错误。我可以知道有这个错误吗? 我的控制器 服务 存储库

  • 问题内容: 我正在尝试使用PDO从表中检索数据,只是我似乎无法向浏览器输出任何内容,只是得到了一个纯白页。 问题答案: 例。 这是您的dbc类 您的PHP页面:

  • 我有期货清单 我需要的是一种获取结果列表的方法 你能在java中提供解决方案吗? 类似于

  • 问题内容: 试用Spring-JDBC。我以此为参考。我正在尝试获取姓氏相同的演员列表。运行这段代码给了我想要的结果: 我姓氏。如何获得演员列表和我拥有的列表?我是否遍历姓氏列表并每次调用一次,或者spring提供了一种方式来进行迭代并为我获取结果?请指教。 问题答案: 使用IN子句。 如何在JDBCTemplates中使用SELECTIN子句?

  • 问题内容: 我有一个函数,该函数接受在whe​​re子句中使用的参数 函数(字符串x)->现在这将创建一个sql查询,该查询将 现在,我希望此函数提供所有行,即等于 当我通过x =“ All”时。 我想创建一个通用查询,当我传递“全部”时,它将返回所有行,否则将过滤我的结果。 问题答案: 只需将where条件排除在外即可。 如果您真的想要它那么复杂的用途 仅过滤空值。

  • 我使用logstash将我的mysql表数据保存到elasticsearch中。现在我想使用特定字段从elasticsearch获取数据。我可以使用id获取数据,但无法使用其他字段检索数据。 我正在使用elasticsearch 5.6.12和Spring boot 2.0 searchcontroller.java 我想用first_name搜索,但什么都没有显示。我在这里做错了什么?