-->
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: Custom SQL: run-time binding
PostPosted: Thu Mar 17, 2005 6:36 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 9:15 am
Posts: 26
Hi,

I am using a <where> tag for a class to control selection of data.
In the selection I would like Hibernate to autonamtically substitute an
expression (method calls actualy) with the value that results from
these expression being executed.

For example, the following when parsed :
where child0_.g1<={s.getAccessRights("browser.actor")[0]} AND
child0_.g2<=2

If s.getAccessRights("browser.actor")[0] = 3
(Note s is in fact a reference within the persisted class but not a property of
the schema).

should result in:
where child0_.g1<= 3 AND
child0_.g2<=2

Is this possible? If so how? If not do I have other alternatives?

TIA,
Hugo Ferreira.


Hibernate version:
Hibernate 3 RC1

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="pt">

<class
name="Child"
table="browser.actor"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
where="g1&lt;={s.getAccessRights(&quot;browser.actor&quot;)[0]} AND g2&lt;=2"
>

<meta attribute="class-description">
Class generated via hbm2java.
Meta generated session aware
constructor is included.

@author hbm2java
</meta>

<id
name="id"
column="idactor"
type="long"
>
<generator class="sequence">
<param name="sequence">idactor_seq</param>
</generator>
</id>

<property
name="name"
type="string"
update="true"
insert="true"
access="property"
column="nameac"
/>

<property
name="email"
type="string"
update="true"
insert="true"
access="property"
column="email"
/>

<property
name="g1"
type="integer"
update="true"
insert="true"
access="property"
column="g1"
/>

<property
name="g2"
type="integer"
update="true"
insert="true"
access="property"
column="g2"
/>

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
none

Full stack trace of any exception that occurs:
none

Name and version of the database you are using:
PostgreSQL 7.4

The generated SQL (show_sql=true):
select child0_.idactor as idactor,
child0_.nameac as nameac0_,
child0_.email as email0_,
child0_.address as address0_,
child0_.typela as typela0_,
child0_.g1 as g6_0_,
child0_.g2 as g7_0_
from browser.actor child0_
where child0_.g1<={s.getAccessRights("browser.actor")[0]} AND
child0_.g2<=2]

Debug level Hibernate log excerpt:
17 Mar 2005 09:58:06.758 [DEBUG] [main] [org.hibernate.util.JDBCExceptionReporter] - could not execute query [select child0_.idactor as idactor, child0_.nameac as nameac0_, child0_.email as email0_, child0_.address as address0_, child0_.typela as typela0_, child0_.g1 as g6_0_, child0_.g2 as g7_0_ from browser.actor child0_ where child0_.g1<={s.getAccessRights("browser.actor")[0]} AND child0_.g2<=2]
org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"

at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139)
at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:154)
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:101)
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43)
at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:515)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50)
at org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:231)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:117)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1206)
at org.hibernate.loader.Loader.doQuery(Loader.java:364)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:204)
at org.hibernate.loader.Loader.doList(Loader.java:1524)
at org.hibernate.loader.Loader.list(Loader.java:1507)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:365)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:268)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:788)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at Test.testCreateActor(Test.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.doRun(TestRunner.java:109)
at junit.textui.TestRunner.run(TestRunner.java:72)
at junit.textui.TestRunner.run(TestRunner.java:57)
at Test.main(Test.java:45)
17 Mar 2005 09:58:06.763 [WARN] [main] [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: 42601
17 Mar 2005 09:58:06.763 [ERROR] [main] [org.hibernate.util.JDBCExceptionReporter] - ERROR: syntax error at or near "{"

org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:1527)
at org.hibernate.loader.Loader.list(Loader.java:1507)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:365)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:268)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:788)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at Test.testCreateActor(Test.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.doRun(TestRunner.java:109)
at junit.textui.TestRunner.run(TestRunner.java:72)
at junit.textui.TestRunner.run(TestRunner.java:57)
at Test.main(Test.java:45)


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.