0

My database table looks like:

ID      ACCESSID    ENTITYGUID      NAME    NAMEID      OWNERGUID   TIMECREATED     VALUEID     VALUETYPE  

I want to only retrieve a list with two fields, name AND valueId, not the whole record. I'm attempting this as follows:

via projections:

public void getAllEntityMetadata(int GUID) {
        Criteria c = session.createCriteria(RevMetadata.class)
                .add(Restrictions.eq("entityGUID", GUID))
                .setProjection(Projections.property("name"))
                .setProjection(Projections.property("nameId"));

        List<RevMetadata> m = (List<RevMetadata>) (RevMetadata) c.list();
        for (RevMetadata item : m) {
            System.out.println("-> All entity metadata for GUID: " + item.getName() + " : " + item.getValueId());
        }
}

I get the error:

Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to wakiliproject.Persistence.RevMetadata
    at wakiliproject.Persistence.PersistRevMetadata.getAllEntityMetadata(PersistRevMetadata.java:112)
    at wakiliproject.HomeController.showMetastring(HomeController.java:492)
    ... 40 more

Via query by example, I'm only getting the output below but no data. No error gets called:

public void getAllEntityMetadataEg(int GUID) {
        RevMetadata m = new RevMetadata();
        m.setEntityGUID(GUID);
        Example e = Example.create(m);

        Criteria c = session.createCriteria(RevMetadata.class).add(e);
        for (Iterator it = c.list().iterator(); it.hasNext();) {
            RevMetadata item = (RevMetadata) it.next();
            System.out.println("-> All entity metadata for GUID: " + item.getName() + " : " + item.getValueId());
        }
}

The output:

Hibernate: select this_.id as id1_4_0_, this_.accessId as accessId2_4_0_, this_.entityGUID as entityGU3_4_0_, this_.name as name4_4_0_, this_.nameId as nameId5_4_0_, this_.ownerGUID as ownerGUI6_4_0_, this_.timeCreated as timeCrea7_4_0_, this_.valueId as valueId8_4_0_, this_.valueType as valueTyp9_4_0_ from METADATA this_ where (this_.accessId=? and this_.entityGUID=? and this_.nameId=? and this_.ownerGUID=? and this_.timeCreated=? and this_.valueId=?)

None of the approaches work. Please help me make it work so that I can get a list with two data objects of name and nameId

Update:

The database table entity class:

public class RevMetadata implements Serializable {
    private String name;
    private int valueId;

}
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142

4 Answers4

1

If only some specified columns will be retrieved, the result will be List<Object[]>.

In your case, Object[] will be populated with name value => index 0 and nameId => index 1. Need to cast it to target value's type.

Criteria criteria = session.createCriteria(RevMetadata.class);
criteria.add(Restrictions.eq("entityGUID", GUID));

criteria.setProjection(Projections.projectionList()
                        .add(Projections.property("name"))
                        .add(Projections.property("nameId")));

List<Object[]> criteriaResult = criteria.list();

List<RevMetadata> results = new ArrayList<RevMetadata>();
//then get result into the class properties
for (Object[] object : criteriaResult) {
    //cast to target value type
    String name = (String)object[0];
    int nameId = ((Integer)object[1]).intValue();

    RevMetadata result = new RevMetadata();
    result.setName(name);
    result.setValueId(nameId);

    results.add(result);
}

//now results of List<RevMetadata> is ready
Wundwin Born
  • 3,467
  • 19
  • 37
1

The projections needs to be added to projectList if you want to get multiple columns:

Projections.projectionList()
.add(Projections.property("name"))
.add(Projections.property("valueId"))

Also the c.list() returns a List and this list contains the fields added to your projection list so it contains an object array with first field as your name property and second field as your valueId property. So your c.list() should be like:

List<Object[]> rows = (List<Object[]>) c.list(); 

So here are the changes that works:

Criteria c = session.createCriteria(RevMetadata.class)
                .add(Restrictions.eq("entityGUID", GUID));

        c.setProjection(Projections.projectionList()
                .add(Projections.property("name"))
                .add(Projections.property("valueId")));

        List<Object[]> rows = (List<Object[]>) c.list(); 
        System.out.println(rows);
        for (Object[] row : rows) {
            System.out.println(row[0] + " and " + row[1]);
        }
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
0
List m = c.list();
    for(Iterator it = c.iterator(); it.hasNext()) {
        Object[] row = (Object[]) it.next();
        System.out.println("-> All entity metadata for GUID: " + row[0] + " : " +row[1]);
    }
Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
  • I did that but now I get the error: Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to wakiliproject.Persistence.RevMetadata at wakiliproject.Persistence.PersistRevMetadata.getAllEntityMetadata(PersistRevMetadata.java:114) That is at the line: for (RevMetadata item : m) { Still not working! – Program-Me-Rev Aug 26 '14 at 07:54
  • I think it would be more helpful for the OP and further visitors, when you add some explaination to your intension. – Reporter Aug 26 '14 at 08:42
  • I tried your edited response, but still no success. I get the error `java.lang.ClassCastException: java.lang.Integer cannot be cast to [Ljava.lang.Object;` – Program-Me-Rev Aug 26 '14 at 09:31
  • Integer[] row = it.next(); – Pratik Shelar Aug 26 '14 at 09:44
0

It seems that the Problem is the double casting

(List<RevMetadata>) (RevMetadata) c.list();

Try to remove the cast

(RevMetadata)
meleagros
  • 482
  • 1
  • 5
  • 17
  • I did that but now I get the error: Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to wakiliproject.Persistence.RevMetadata at wakiliproject.Persistence.PersistRevMetadata.getAllEntityMetadata(PersistRevMetadata.java:114) That is at the line: for (RevMetadata item : m) { Still not working! – Program-Me-Rev Aug 26 '14 at 07:47
  • It seems that you have a general problem. see for dirrent query solutions http://stackoverflow.com/questions/12618489/jpa-criteria-api-select-only-specific-columns – meleagros Aug 26 '14 at 08:21