简体   繁体   English

Hibernate OGM映射本机查询的@Embeddable对象

[英]Hibernate OGM mapping @Embeddable objects of native query

How can I read list of @Embeddable objects from MongoDB with Hibernate OGM after aggregation. 聚合后如何使用Hibernate OGM从MongoDB中读取@Embeddable对象列表。

I have entity like this 我有这样的实体

@javax.persistence.Entity
public class MySession implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Type(type = "objectid") 
    private String id;
    private Date start;
    private Date end;
    @ElementCollection
    private List<MySessionEvent> events;
}

and @Embeddable object 和@Embeddable对象

@javax.persistence.Embeddable
public class MySessionEvent implements Serializable {
    private Long time;
    private String name;
}

I stuck with mapping Embeddable objects from native query 我坚持从本机查询映射可嵌入对象

String queryString = "db.MySession.aggregate([" +
            "   { '$match': { 'events.name': { '$regex': 'Abrakadabra'} }}, " +
            "   { '$unwind': '$events' }, " +
            "   { '$replaceRoot': { 'newRoot': '$events'}} " +
            "])";

List<MySessionEvent> objects = em.createNativeQuery(queryString, MySessionEvent.class).getResultList();

I get an error Caused by: org.hibernate.MappingException: Unknown entity 我收到错误Caused by: org.hibernate.MappingException: Unknown entity

Copying your comment here because it adds some details: 在此处复制您的评论,因为它添加了一些详细信息:

I have data like this [ {id:'s1', events: [{name: 'one'},{name: 'two'}]}, {id:'s2', events: [{name: 'three'},{name: 'four'}]} ] and I want result like this [{name: 'one'},{name: 'two'},{name: 'three'},{name: 'four'}] 我有这样的数据[{{id:'s1',events:[{name:'one'},{name:'two'}]]},{id:'s2',events:[{name:'three' },{name:'four'}]}],我想要这样的结果[{name:'one'},{name:'two'},{name:'three'},{name:'four'} ]

The query you are running returns the following type of results if I run it on MongoDB natively (I populated it with some random data): 如果我本机在MongoDB上运行查询,那么您正在运行的查询将返回以下类型的结果(我在其中填充了一些随机数据):

{ "name" : "Event 3", "time" : NumberLong(3) }
{ "name" : "Abrakadabra", "time" : NumberLong(5) }

This is not enough to rebuild an entity and that's the reason you are seeing the exception. 这还不足以重建实体,这就是您看到异常的原因。

Considering that you only want the list of events, this should work: 考虑到您只需要事件列表,这应该可以工作:

List<Object[]> poems = em.createNativeQuery( queryString ).getResultList();

Hibernate OGM will convert the previous result in a list of arrays. Hibernate OGM将先前的结果转换为数组列表。 Each element of the List is an array where the firs value of the array is the name of the event and the second one is the time. List的每个元素都是一个数组,其中数组的firs值是事件的名称,第二个是时间。

For supported cases like this I think HQL queries are better. 对于这种受支持的情况,我认为HQL查询更好。 You could rewrite the same example with the following: 您可以使用以下代码重写相同的示例:

    String queryString = 
            "SELECT e.name " +
            "FROM MySession s JOIN s.events e " +
            "WHERE e.name LIKE 'Abrakadabra'";
    List<Object[]> events = em.createQuery( queryString ).getResultList();

Note that I decided not to return the time because in your comment you didn't request it, but this will work as well: 请注意,我决定不返回时间,因为在您的评论中您没有要求时间,但这也可以:

    String queryString = 
            "SELECT e.time, e.name " +
            "FROM MySession s JOIN s.events e " +
            "WHERE e.name LIKE 'Abrakadabra'";
    List<Object[]> events = em.createQuery( queryString ).getResultList();

它无法识别实体,请确保您所有的实体也都在persistence.xml

<class>org.example.package.MySessionEvent</class>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM