- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 CriteriaBuilder
中的联接进行选择,但在 Eclipse 中收到此错误。我该如何修复它?
Hibernate version: hibernate-jpa-2.0-api<br />
Java Version: 1.8
fonte cannot be solved or is not a field
NotificacaoDao.java
@Stateless
public class NotificacaoDao {
@PersistenceContext(unitName = "PostgreSQLDS")
private EntityManager em;
@EJB
private NotificacaoDao NotificacaoDao;
public List<Notificacao> getResultList(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) throws ApplicationException{
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Notificacao> cq = cb.createQuery(Notificacao.class);
Metamodel m = em.getMetamodel();
EntityType<Notificacao> Notificacao_ = m.entity(Notificacao.class);
Root<Notificacao> myObj = cq.from(Notificacao_);
Join<Notificacao, Fonte> fontes = myObj.join(Notificacao_.fonte); // HERE I'M GETTING THE ERROR
cq.where(NotificacaoDao.getFilterCondition(cb, myObj, filters));
Predicate filterCondition = NotificacaoDao.getFilterCondition(cb, myObj, filters);
filterCondition = cb.and(filterCondition, cb.equal(myObj.get("excluido"), "N"));
cq.where(filterCondition);
if (sortField != null) {
if (sortOrder == SortOrder.ASCENDING) {
cq.orderBy(cb.asc(myObj.get(sortField)));
} else if (sortOrder == SortOrder.DESCENDING) {
cq.orderBy(cb.desc(myObj.get(sortField)));
}
}
return em.createQuery(cq).setFirstResult(first).setMaxResults(pageSize).getResultList();
} catch(Exception e) {
throw new ApplicationException("myException", e);
}
}
Notificacao.java
@Entity
@Table(name = "tb_notificacao", schema = "indicadores")
@NamedQuery(name = "Notificacao.findAll", query = "SELECT n FROM Notificacao n")
@FilterDef(name="notificacaoNaoExcluido", defaultCondition="excluido = 'N'")
public class Notificacao implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "tb_notificacao_codnotificacao_seq", sequenceName = "TB_NOTIFICACAO_CODNOTIFICACAO_SEQ", schema = "indicadores", allocationSize = 1)
@GeneratedValue(generator = "tb_notificacao_codnotificacao_seq")
@Column(name = "codnotificacao", nullable = false)
private Integer codnotificacao;
private String descricao;
private String excluido;
private String nome;
// bi-directional many-to-one association to CargaNotificacao
@OneToMany(mappedBy = "notificacao")
private List<CargaNotificacao> cargaNotificacoes;
// bi-directional many-to-one association to Fonte
@Inject
@ManyToOne
@JoinColumn(name = "codfonte")
private Fonte fonte;
// bi-directional many-to-one association to UsuarioNotificacao
@OneToMany(mappedBy = "notificacao")
@Filter(name="usuarioNaoExcluido", condition="excluido = 'N'")
private List<UsuarioNotificacao> usuarioNotificacoes;
public Notificacao() {
}
// getters and setters
}
Fonte.java
@Entity
@Table(name = "tb_fonte", schema = "indicadores")
@NamedQuery(name = "Fonte.findAll", query = "SELECT f FROM Fonte f")
public class Fonte implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "tb_fonte_codfonte_seq", sequenceName = "TB_FONTE_CODFONTE_SEQ", schema = "indicadores", allocationSize = 1)
@GeneratedValue(generator = "tb_fonte_codfonte_seq")
@Column(name = "codfonte", nullable = false)
private Integer codfonte;
private String nome;
// bi-directional many-to-one association to Indicador
@OneToMany(mappedBy = "fonte")
@Filter(name="indicadorNaoExcluido", condition="excluido = 'N'")
private List<Indicador> indicadores;
// bi-directional many-to-one association to Notificacao
@OneToMany(mappedBy = "fonte")
@Filter(name="notificacaoNaoExcluido", condition="excluido = 'N'")
private List<Notificacao> notificacoes;
public Fonte() {
}
// getters and setters
}
最佳答案
嗯,在元模型上基本上有三种使用方法:
我建议您使用的解决方案更接近您正在做的事情,即第 3 点。
第 3 点解决方案:替换以下代码:
Metamodel m = em.getMetamodel();
EntityType<Notificacao> Notificacao_ = m.entity(Notificacao.class);
Root<Notificacao> myObj = cq.from(Notificacao_);
Join<Notificacao, Fonte> fontes = myObj.join(Notificacao_.fonte); // HERE I'M GETTING THE ERROR
使用新代码:
Metamodel m = em.getMetamodel();
EntityType<Notificacao> notificacao_ = m.entity(Notificacao.class);
Root<Notificacao> myObj = cq.from(notificacao_);
Join<Notificacao, Fonte> fontes = myObj.join(notificacao_.getSingularAttribute("fonte",Fonte.class));
第 1 点和第 2 点解决方案请注意,Notificacao_ 必须是静态或生成的类,并且绝不能是 em.getMetamodel() 的实例。另请注意,在您的情况中,Notificacao_ 是变量而不是类,如下所示:
EntityType<Notificacao> Notificacao_ = m.entity(Notificacao.class);
如果您需要更多信息,请告诉我。
关于Java CriteriaBuilder Join - 无法解决或不是字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40513761/
我正在尝试更改以下 HQL 以使用 JPA 标准: select distinct d from Department d left join fetch d.children c where d.p
我对 CriteriaBuilder API 有疑问: 我想计算一列的结果,并返回该计数的结果以及该列的不同值的列表。 | Table_fruit | count(Tab
有什么区别: 在 Builder 上的第一次测试运行: Predicate predicate = root.get(PersonEntity_.name).in(names); criteriaBu
我正在开发一个经常使用 CriteriaBuilder.Coalesce 的大型代码库。和 Root 。例如, criteriaBuilder.coalesce(root.get('adjustedW
我有这样的代码 @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) public Re
我必须重写一些代码,但在互联网上找不到正确的信息。原始部分如下所示: String query = "select * from sublog_type_map_v where 1 = 1"; if
这是我从 CriteriaBuilder 获得的 hibernate 查询: select generatedAlias0 from MyObject as generatedAlias0 where
SELECT * FROM Emp_Main WHERE status = 'QUIT' AND to_Char(effective_date,'YYYY')= '2018'; 在此示例中,我已将代码
我正在尝试使用 @Embeddable与 CriteriaBuilder过滤父结果 Entity基于嵌入的属性。我使用 Eclipse Link 来生成元数据类。 这是嵌入的类/实体: @Embedd
谁能提供一个如何使用 CriteriaBuilder 编写案例查询的示例? 最佳答案 以下是使用 CriteriaBuilder 的示例 case 表达式(这适用于 JPA 2): Hashtable
我想写这个SQL查询 SELECT * FROM A LEFT OUTER JOIN B ON A.IDRESOURCE=B.IDRESOURCE AND B.IDLANGUAGE=22; 使用 J
我想将日期与数据库中的时间戳进行比较。我目前使用的是 predicates.add(criteriaBuilder.between(eventRoot.get("time"), dBegin, dEn
使用 Criteriabuilder 编写查询时,如何在两列上添加相等条件?例如: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); C
我有一个实体 @Entity @Table(name = "`petition`") @Getter @Setter public class Petition extends Auditable {
嗯,我正在使用 CriteriaBuilder 和 PredicateList 来处理 JPA 并从数据库中提取信息,事实是它包含带重音的数据,我需要进行搜索,即使我搜索不带重音的单词,我也会找到带重
我有一个要附加到 or 条件中的列表 我面临的问题是当我遍历 List 并将其添加到 CategoryBuilder 然后它采用最后一个 Predicate 例子如下: public static S
我正在尝试计算给定的纬度/经度和距离,并且只返回落在该圆距离内的行(数据库也有纬度/经度坐标)。我不知道的是我会怎么做。我目前正在使用 native SQL 并使用纯文本和计算它的半正弦公式,如果我是
我正在尝试了解如何从联接中仅选择记录上的最新日期。我的 People 实体与 Membership 实体相结合。我的成员(member)实体有一个 RefMembershipStatus 实体。我试图
我目前正在这样。 final CriteriaBuilder builder = ...; final boolean flag = ...; if (flag) { builder.isTr
我有这样的 SQL 语句: SELECT * FROM person inner join (select max(validityId) as maxID from person group by
我是一名优秀的程序员,十分优秀!