使用JPA NamedQuery获取结果列表
Using JPA NamedQuery to obtain result list
我想使用如下查询获取结果列表:
SELECT x FROM XTreinamentosOrc x WHERE x.codEduca NOT IN (SELECT DISTINCT y.codCurso y FROM XRlz y WHERE y.matricula = :matricula)
我在它的实体 class 中使用了它,但后来我得到了异常:
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 字段位于与此处映射的另一个 table 中。
如何在 XRlzConsolidado class 类型的参数矩阵中设置注入的 sessionChave 字符串?
豆子:
@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、XRlzConsolidado 和 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;
}
}
提前致谢。
你能 post 编码 XRlzConsolidado
吗?它可能包含要与 sessionChave
进行比较的字符串字段。您正在将字段 XRlz.matricula 与字符串值进行比较,但它的类型是 XRlzConsolidado
我认为问题出在您的查询名称上。 findAllTreinamentosByMatricula() 是一个 属性 表达式,只能引用被管理实体的直接 属性。 findAllTreinamentosByMatricula 将在 XTreinamentosOrc class 中查找名为 matricula 的 属性。当在 XTreinamentosOrc 中找不到 属性 时,您会得到异常:
Caused by: javax.el.PropertyNotFoundException: The class 'br.com.bb.upb.diage.atb_treinamentos.entities.XTreinamentosOrc' does not have the property 'matricula'.
您需要将查询名称更改为:
@NamedQuery(name = "XTreinamentosOrc.find", query = "SELECT x FROM XTreinamentosOrc x WHERE x.codEduca NOT IN (SELECT DISTINCT y.codCurso FROM XRlz y WHERE y.matricula = :matricula)")})
(对于可接受的格式,我可能会使用方法名称 'find() so you may need to do some more research')
查看此文档:
我想使用如下查询获取结果列表:
SELECT x FROM XTreinamentosOrc x WHERE x.codEduca NOT IN (SELECT DISTINCT y.codCurso y FROM XRlz y WHERE y.matricula = :matricula)
我在它的实体 class 中使用了它,但后来我得到了异常:
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 字段位于与此处映射的另一个 table 中。
如何在 XRlzConsolidado class 类型的参数矩阵中设置注入的 sessionChave 字符串?
豆子:
@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、XRlzConsolidado 和 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;
}
}
提前致谢。
你能 post 编码 XRlzConsolidado
吗?它可能包含要与 sessionChave
进行比较的字符串字段。您正在将字段 XRlz.matricula 与字符串值进行比较,但它的类型是 XRlzConsolidado
我认为问题出在您的查询名称上。 findAllTreinamentosByMatricula() 是一个 属性 表达式,只能引用被管理实体的直接 属性。 findAllTreinamentosByMatricula 将在 XTreinamentosOrc class 中查找名为 matricula 的 属性。当在 XTreinamentosOrc 中找不到 属性 时,您会得到异常:
Caused by: javax.el.PropertyNotFoundException: The class 'br.com.bb.upb.diage.atb_treinamentos.entities.XTreinamentosOrc' does not have the property 'matricula'.
您需要将查询名称更改为:
@NamedQuery(name = "XTreinamentosOrc.find", query = "SELECT x FROM XTreinamentosOrc x WHERE x.codEduca NOT IN (SELECT DISTINCT y.codCurso FROM XRlz y WHERE y.matricula = :matricula)")})
(对于可接受的格式,我可能会使用方法名称 'find() so you may need to do some more research')
查看此文档: