Hi
I have written a criteria query with some projections. I am trying to transform the result into an object instead of an list of Object[]. The root criteria is against the class which is defined as @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name ="order_type", discriminatorType = DiscriminatorType.STRING).
In the transformer.transformTuple , I am trying to differentiate the instance based on discriminator. And to get the discriminator column value, I am adding a sqlProjection to the criteria since the discriminator can't be defined as a property of the root class(correct me if I am wrong). But the criteria.list() method always throws this exception
java.lang.ArrayIndexOutOfBoundsException: 3 at org.hibernate.loader.criteria.CriteriaLoader.getResultColumnOrRow(CriteriaLoader.java:107) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606) at org.hibernate.loader.Loader.doQuery(Loader.java:701) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236) at org.hibernate.loader.Loader.doList(Loader.java:2213) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104) at org.hibernate.loader.Loader.list(Loader.java:2099) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
There are four columns in the projections including sqlProjection. The API works,If I remove the sqlProjection. can someone help me here? is there a way to do this without transformer?
Criteria c = db.createCriteria(Order.class); ProjectionList p = Projections.projectionList(); p.add(Projections.property("id")); p.add(Projections.property("cost")); p.add(Projections.property("created")); p.add(Projections.sqlProjection("order_type", new String[] {}, new Type[] { new CharacterType() })); c.setProjection(p); c.setResultTransformer(new OrderMapper()); c.list();//error here
private class OrderMapper implements ResultTransformer { public McrMapper() {} public List transformList(List arg0) { // System.out.println("transformList >>>>>>:" + arg0); return arg0; }
public Object transformTuple(Object[] o, String[] arg1) { Order s = null; String type = "B"; if ("B".equals(type)) { s = new CustomOrder(); } else if ("I".equals(type)) { s = new InternetOrder(); } //read other values return s; }
}
|