-->
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.  [ 1 post ] 
Author Message
 Post subject: Projections class is final
PostPosted: Sat May 27, 2006 1:34 am 
Beginner
Beginner

Joined: Thu Jul 28, 2005 1:19 pm
Posts: 24
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1


The following is comming from projections class header

/**
* The <tt>criterion</tt> package may be used by applications as a framework for building
* new kinds of <tt>Projection</tt>. However, it is intended that most applications will
* simply use the built-in projection types via the static factory methods of this class.<br/>
* <br/>
* The factory methods that take an alias allow the projected value to be referred to by
* criterion and order instances.

Dose that mean we can write our own peojections like stddev, to_char, trunc especially to use with oracle. Another developer from my team suggested that this is not a good practice referring back to the paragraph listed above.

What I am doing is basically extending SimpleProjection for a TruncProjection and AggregateProjection for a StddevProjection and other projections mainly extending SimpleProjection and AggregateProjection.

Can someone from the hibernate team please advise. Personally I think that there is a need to implement such projections and doing that should not be a problem. Am I wrong?

Thank You for your advise.


Please see below the code for the classes, this is a first effort its not finalized. Any suggestion/advise is realy appreciated.


*********************************
import org.hibernate.criterion.AggregateProjection;
public class ExtendedProjections {

private ExtendedProjections() {
}

public static StdProjection stddev(String propertyName) {
return new StdProjection(propertyName);
}

public static TruncProjection trunc(String propertyName, String format) {
return new TruncProjection(propertyName, format);
}
public static ToCharProjection toChar(String propertyName, String format) {
return new ToCharProjection(propertyName, format);
}
public static ToCharProjection toChar(AggregateProjection projection, String format) {
return new ToCharProjection(projection, format);
}
}



***************************************

mport org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.AggregateProjection;
import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.SimpleProjection;
import org.hibernate.type.Type;
import org.hibernate.util.StringHelper;

public class ToCharProjection extends SimpleProjection {
private final String aggregate;
protected String propertyName;
private String format;
private AggregateProjection projection;

public ToCharProjection(String propertyName, String format) {
this("to_char", propertyName, null, format);
}

public ToCharProjection(AggregateProjection projection, String format) {
this("to_char", null, projection, format);
}
public ToCharProjection(String aggregate, String propertyName,
AggregateProjection projection, String format) {
this.aggregate=aggregate;
this.propertyName = propertyName;
this.format = format;
this.projection = projection;
}

public String toString() {
if(propertyName!=null && !propertyName.equals(""))
return aggregate+"(" + propertyName + ","+ format+ ')';
else
return aggregate+"(" + projection.toString() + ","+ format+ ')';
}

public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
return projection.getTypes(criteria, criteriaQuery);
//return new Type[] { criteriaQuery.getType(criteria, propertyName) };
}

public String toSqlString(Criteria criteria, int loc, CriteriaQuery criteriaQuery)
throws HibernateException {
StringBuffer buffer = new StringBuffer()
.append(aggregate)
.append("(");
if(propertyName!=null && !propertyName.equals(""))
buffer.append( criteriaQuery.getColumn(criteria, propertyName) );
else if(projection!=null){
buffer.append(projection.toString());
}
else
;//TODO:
buffer.append( ", '"+format+"'" ).append(") as y")
.append(loc)
.append('_');
return buffer.toString();
}

}

**********************************


public class TruncProjection extends SimpleProjection {
protected final String propertyName;
private final String aggregate;
private final String format;

public TruncProjection(String propertyName, String format) {
this.aggregate = "trunc";
this.propertyName = propertyName;
this.format = format;
}

public String toString() {
return aggregate + "(" + propertyName + ","+ format+ ')';
}

public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
return new Type[] { criteriaQuery.getType(criteria, propertyName) };
}

public String toSqlString(Criteria criteria, int loc, CriteriaQuery criteriaQuery)
throws HibernateException {
StringBuffer buffer = new StringBuffer()
.append(aggregate)
.append("(")
.append( criteriaQuery.getColumn(criteria, propertyName) )
.append( ", '"+format+"'" ).append(") as y")
.append(loc)
.append('_');
return buffer.toString();
}

public boolean isGrouped() {
return true;
}
/*
public String toGroupSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
throw new UnsupportedOperationException("not a grouping projection");
}*/
public String toGroupSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
return StringHelper.replace( aggregate+"("+criteriaQuery.getColumn(criteria, propertyName)+", '"+format+"')", "{alias}", criteriaQuery.getSQLAlias(criteria) );
}
}


*******************************************************


public class StdProjection extends AggregateProjection {
public StdProjection(String propertyName) {
super("stddev", propertyName);
}
public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
return new Type[] { Hibernate.DOUBLE };
}
}


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.