使用MongoDB的Hibernate OGM无法解析属性

时间:2018-04-26 16:21:54

标签: mongodb hibernate hql hibernate-mapping hibernate-ogm

这个HQL查询给我错误:

String q4 = "SELECT i, COUNT(ie) FROM CorsoStudi cs \n"
            + "\t JOIN cs.inserogati ie JOIN ie.insegn i \n"      
            + "\t WHERE cs.nome = 'Laurea in Informatica' \n"
            + "\t GROUP BY i"; 

错误是:

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: insegn. of: component[_id,annierogazione,annoaccademico,crediti,discriminante,discriminantemodulo,hamoduli,id_facolta,insegn,inserogato_padre,modulo,nomemodulo,nomeunita,programma] 

这是InsErogato:

@Embeddable 
public class InsErogato { 

    private Integer _id; 

    private String annoaccademico; 

    @Embedded 
    private Insegn insegn; 

    @Embedded
    private Discriminante discriminante; 

    private Integer modulo;  

    private String discriminantemodulo; 

    private String nomemodulo; 

    private Double crediti; 

    private String programma; 

    private Integer id_facolta; 

    private String hamoduli;  

    @Embedded 
    private InsErogatoPadre inserogato_padre; 

    private String nomeunita; 

    private Integer annierogazione; 

    // constructors, getters and setters and toString  
}  

这是Insegn:

@Embeddable
public class Insegn { 

    private Integer _id; 

    private String nomeins;  

    private String codiceins; 

    // constructors, getters and setters and toString 
}  

主要:

// begin transaction 
entityManager.getTransaction().begin();  

List<Object[]> insegn = entityManager
        .createQuery(q4, Object[].class) 
        .getResultList(); 

for(Object[] i : insegn) {
    Insegn ins = (Insegn)i[0]; 
    Long count = (Long)i[1]; 

    System.out.println("nomeins: " + ins.getNomeins() + ", numero inserogati: " + count); 
}  

// commit transaction
entityManager.getTransaction().commit();  

MongoDB结构:

https://i.stack.imgur.com/qFusC.jpg  
https://i.stack.imgur.com/k04HK.png
https://i.stack.imgur.com/H8nhS.png
https://i.stack.imgur.com/eYl2M.png 

我试图更改查询,但Hibernate在“inserogato”中找不到“insegn”(以及“discriminante”)属性,但他可以从中找到其他简单属性(如“annoaccademico”等)。

同样的查询适用于PostgreSQL的Hibernate ORM。

也许我必须在注释中添加一些内容,或者更改mongoDB结构(?)。

我正在使用Hibernate OGM 5.3.1.Final和MongoDB 3.6.3 JDBC驱动程序。

2 个答案:

答案 0 :(得分:0)

  

同样的查询适用于带有PostgreSQL的Hibernate ORM

但是PostgreSQL是一个SQL数据库,MongoDB是一个NoSQL数据库。 Hibernate OGM是一种将Hibernate ORM添加到NoSQL数据库的方法。 &#39;(H)SQL&#39;对于OGM来说是有限的(见下文)。

您不会说出您的应用程序部署的内容。我使用WildFly 12.0.0.Final。我已经使用OGM与MongoDB一起工作在版本11&amp; 12。

  

entityManager.getTransaction()开头();

我使用(WildFly)容器来处理事务。注释我的EJB。

@TransactionManagement(TransactionManagementType.CONTAINER)

我不相信你可以使用(H)SQL本身使用Hiberate OGM但是:

  

使用JPQL - 仅适用于现在的简单查询

     

使用NoSQL本机查询将结果映射为托管实体

     

使用Hibernate Search查询 - 主要是全文查询

它在文档中说:

  

特别是通知,不支持的是:

     

跨实体加入

     

JPQL功能特别是聚合函数,如count

     

JPQL更新和删除查询

我的一个问题是:

Query query = mongoDBEntityManager.createQuery("FROM FoodsCosmeticsMedicines f WHERE f.ean = :ean")
                .setParameter("ean", ean);

实体(@Expose用于JSON)

@Entity(name = "FoodsCosmeticsMedicines")
@Indexed
@Table(name = "foodsCosmeticsMedicines")
public class FoodsCosmeticsMedicines implements Serializable {

    // Arrays of Objects
    @Expose(deserialize = true, serialize = true)
    @Embedded
    ProductCharacteristics productCharacteristics;
    @Expose(deserialize = true, serialize = true)
    @Embedded
    CalcNutrition calcNutrition;
    @Expose(deserialize = true, serialize = true)
    @Embedded
    Nutrients nutrients;
    @Expose(deserialize = true, serialize = true)
    @Embedded
    Enumbers enumbers;
    @Expose(deserialize = true, serialize = true)
    @Embedded
    ChemicalsMineralsVitamins chemicalsMineralsVitamins;
    @Expose(deserialize = true, serialize = true)
    @Embedded
    Lifestyle lifestyle;
    .....
}

你为什么要使用JOIN?

使用MySQL我使用Hibernate作为我的ORM我会使用注释来映射与实体的关系,例如:

@OneToOne(cascade = CascadeType.ALL, mappedBy = "product", fetch = FetchType.LAZY)
private UriEntity uri;
  

entityManager.getTransaction()提交();

这是无关紧要的,因为您所做的一切都已被阅读。也许你排除了坚持?

我确定您已阅读过该文档。我找到了我遇到的问题的所有答案:

Hibernate OGM 5.3.1.Final: Reference Guide

答案 1 :(得分:0)

目前,不支持group by的查询。您需要为此特定用例运行本机MongoDB查询。有关详细信息,请参阅the reference documentation

我认为嵌入式实体而不是集合的连接会使解析器感到困惑,即使它是有效的查询。你应该看到一个更明确的例外情况(相当于你正在尝试的那个):

SELECT ie.i, COUNT(ie)
FROM CorsoStudi cs
    JOIN cs.inserogati ie
WHERE cs.nome = 'Laurea in Informatica'
GROUP BY ie.i;

它应该抛出:

java.lang.UnsupportedOperationException: The GROUP BY clause is not supported

也不支持聚合函数。