简体   繁体   中英

Hibernate OGM Aggregate Query result fields not working with Wrapper Types

I am using Hibernate OGM (5.2.0.Alpha1) with Mongodb (3.4)

Example:

@Entity
@Table(name = "jangad")
@JsonInclude(Include.NON_EMPTY)
public class Jangad {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonSerialize(using = ToStringSerializer.class)
    public ObjectId id;

    public String name;

    @OneToMany(mappedBy = "jangad")
    public Set<SalesStoneCharacteristics> setOfSalesStoneCharacteristics;

    // Note : Integer data type for this not working with aggregation result.
    public Integer totalStones;

    getter....
    setter....

}


@Entity
@Table(name = "sales_stone_details")
@JsonInclude(Include.NON_EMPTY)
// @JsonFilter(value = SalesUtils.MY_CUSTOM_FILTER_FOR_SURAT_POLISH_STONES)
public class SalesStoneCharacteristics {
     @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "SALES_STONE_CHARACTERISTICS_ID", unique = true, nullable = false)
    @JsonSerialize(using = ToStringSerializer.class)
    public ObjectId id;

    private String name;

    @ManyToOne
    @JsonIgnore
    private Jangad jangad;

    setter....
    getter....
}

Dao Layer...

public <T> List<T> executeQuery(String query, Integer startPosition, Integer noOfRecords, T t) {
        List<T> listOfT = new ArrayList<>();

        if (SalesUtils.isObjectisNullOrEmpty(startPosition, noOfRecords)) {
            listOfT = entityManager.createNativeQuery(query.toString(), t.getClass()).getResultList();
        } else {
            listOfT = entityManager.createNativeQuery(query.toString(), t.getClass()).setFirstResult(startPosition)
                    .setMaxResults(noOfRecords).getResultList();
        }
        return SalesUtils.isListIsNullOrEmpty(listOfT) ? new ArrayList<>() : listOfT;
    }

Service Layer....(Error)

@Transaction
public void executeQuery() {

    String query = "db.jangad.aggregate({'$project': { 'totalStones': { '$size':'$setOfSalesStoneCharacteristics' }}} , { '$match' : { '_id' :ObjectId('5a60784e8daff90498ba74e4')} } )";

    List<Jangad> listOfJangads = jangadDao.executeQuery(sb.toString(), null, null, new Jangad());
    if (!SalesUtils.isListIsNullOrEmpty(listOfJangads)) {                   
        System.out.println(listOfJangads.get(0).getTotalStones()) // Error
    }
}

Temporary Solution : Changed field type of totalStones from Integer to int of Jangad.java

Error Explaination of Service Layer....(Error)

here if i set totalStones type as Wrapper type (like Integer,Double) then it gives error, so i must put that field types to int or double.

It seems like aggregation result is not working with any Java Wrapper type.

Is i am right?????

There are two problems with this query. The first one is that you are not passing it as an array and therefore the parser thrown an exception. You should add the [ and ] like so:

String query = "db.jangad.aggregate([{'$project': { 'totalStones': { '$size':'$setOfSalesStoneCharacteristics' }}} , { '$match' : { '_id' :ObjectId('5a60784e8daff90498ba74e4')} }] )";

This should work.

But if you are interested only to the totalStones , why don't you just return that value? It will look like this:

String totalStones = "db.jangad.aggregate([{'$project': { 'totalStones': { '$size':'$setOfSalesStoneCharacteristics' }}} , { '$match' : { '_id' :ObjectId('5a60784e8daff90498ba74e4')} }, {'$project':{ '_id': 0}}] )";
Integer size = (Integer) em.createNativeQuery( query ).getSingleResult();

You don't have to create an object every time if you only need specific fields, and you don't have to map the totalStones field on the entity.

EDIT : Because of a bug in recognizing 0, this last query will only work for versions after Hibernate OGM 5.2.CR1

If you want/need to create the entity object, make sure that the query returns all the fields or OGM will create an incomplete object. In this case the Jangad will only have the id and the totalStones property set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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