Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
[b]Hibernate version:3.1[/b]
Hi to all,
I have this java code with hibernate:
Session s = HibernateUtil.currentSession();
s.clear();
ArrayList<MyObjectObj> result = new ArrayList();
Criteria crit = s.createCriteria(MyObjectObjclass);
if((date1!=null)&&(!date1.toString().trim().equals(""))&&(date2!=null)&&(!date2.toString().trim().equals("")) ){
crit.add(Restrictions.between("data",date1,date2 ));
}
crit.add(Restrictions.eq("objectType", "anObjectType"));
if((myObject.getCodObject()!=null)&&(!myObject.getCodObject().toString().trim().equals(""))){
crit.add(Restrictions.eq("codObject", myObject.getCodObject()));
}
ProjectionList proList = Projections.projectionList();
if(myObject.getObjectType()!=null){
proList.add(Projections.groupProperty("object_type"));
}
if(myObject.getTotalSmiles()!=null){
proList.add(Projections.sum("totalSmiles"),"totalSmiles");
}
crit.setProjection(proList);
result.addAll(crit.list());
return result;
But, the sql that results from the code is something like
"SELECT FROM TABLENAME ..."
generating errors and not
"SELECT this._field1, this._filed2 ... FROM TABLENAME ...".
The cause seem to be the code lines:
"if(myObject.getObjectType()!=null){
proList.add(Projections.groupProperty("object_type"));
}
if(myObject.getTotalSmiles()!=null){
proList.add(Projections.sum("totalSmiles"),"totalSmiles");
}
"
beacause it works fine without those code lines.
HERE THE ERROR GENERATED (with data1='2009-01-01' and data2='2009-12-31'):
select from tablename this_ where this_.data between ? and ?
[WARN] JDBCExceptionReporter - SQL Error: 1064, SQLState: 42000
[ERROR] JDBCExceptionReporter - Syntax error or access violation message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from tablename this_ where this_.data between '2009-01-01' and '2009-01-3' at line 1"
[WARN] RequestProcessor - Unhandled Exception thrown: class org.hibernate.exception.SQLGrammarException
14-apr-2009 10.37.03 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet action threw exception
java.sql.SQLException: Syntax error or access violation message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from tablename this_ where this_.data between '2009-01-01' and '2009-01-3' at line 1"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2251)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1586)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
....
I'm newbie with Hibernate query java configuration.
Any ideas?
Thanks in advance.