-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Criteria Projection Transformer issues
PostPosted: Sat Jan 23, 2010 11:34 pm 
Newbie

Joined: Sat Jan 23, 2010 11:10 pm
Posts: 2
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;
}

}


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria Projection Transformer issues
PostPosted: Sun Jan 24, 2010 1:11 am 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Just trying to understand what you actually want out of the query.. it appears to me that you don't need a transformer.
So you want instances of CustomOrder or InternetOrder in your results?

Criteria c = db.createCriteria(CustomOrder.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();//should give all of the instances of CustomOrder from your orders table.

or this may not be what you want..need more hints.


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria Projection Transformer issues
PostPosted: Sun Jan 24, 2010 12:09 pm 
Newbie

Joined: Sat Jan 23, 2010 11:10 pm
Posts: 2
Thanks for your reply.

The returned type can be either CustomOrder or InternetOrder since the criteria is against the super class Order.class. I can differentiate CustomOrder or InternetOrder based on discriminator defined in Order.class.

I believe the following lines will return a list of Object[] array instead of sub-classes that I expect because of projection usage.

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();

If I had not used projection, then hibernate would have taken care of instance type and returned appropriate instances based on discriminator type. Since the order table is huge and has clob columns, I don't want to get all columns from order table and get few columns using projection.


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria Projection Transformer issues
PostPosted: Mon Jan 25, 2010 12:22 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
I did not try this in my app, but i think it might work for you..
when you do this as below you should get the right instances and you need to cast them into InternetOrder or CustomOrder based on the properties.

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();//should give all of the instances of CustomOrder and InternetOrder from your orders table.

Iterate through list of objects, and cast them to CustomOrder and InternetOrder based on the properties.
Hope this helps.
Srilatha.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.