This NHibernate query seems to use use an integer data type for the constant parameter, causing wrong results:
double myValue = 1.5;
Session.Query<Foo>().Where(x => x.IntProp < myValue*x.AnotherIntProp).ToList();
Works On My Machine against SQL-server 2008 dialect, generating the following SQL on NHibernate 4.0.4.4000 according to NHibernate profiler:
select fooentity0_.Id as Id3_,
fooentity0_.IntProp as IntProp3_,
fooentity0_.AnotherIntProp as AnotherI3_3_
from [FooEntity] fooentity0_
where cast(fooentity0_.IntProp as FLOAT(53)) < 1.5 /* #p0 */ * cast(fooentity0_.AnotherIntProp as FLOAT(53))
So I guess it is a dialect-specific bug, which dialect are you using?
Related
I have a colum "datetime", like this: 2012-06-04 15:40:20.
I want to create a query in Doctrine that I get the data of previous time. Less than 2012-06-04 15:40:20. How can I realize that in Doctrine.
Sorry, I just have no clue.
If I understand your question correctly, I believe the syntax is just:
$datetime = // your timestamp
->where('t.somefield < ?', date('Y-m-d H:i:s', strtotime($datetime))
I am not familiar with Doctrine, but here is standard SQL to do what you want:
select *
from t
where t.datetime in (select max(datetime)
from t
where datetime < '2012-06-04 15:40:20'
)
If Doctrine supports standard SQL syntax, then something like this would work (you might have to input the date/time constant in a different way).
I'm trying to get a query to work which returns a list of locId's from the database when fed a long and a lat.
Here's the sql:
eg: lat = "-37.8333300" : lon = "145.000000" : radius = (5 * 0.621371192) ^ 2
SELECT locId,longitude,latitude FROM tbliplocations WHERE (69.1*([longitude]- "&lon&") * cos("&lat&"/57.3))^2 + (69.1*([latitude]- "&lat&"))^2 < "&radius
Here's the error I receive:
The data types float and int are incompatible in the '^' operator.
I'm unsure of a workaround, can anyone point me in the right direction?
Answer:
Using SQL Server 2008 R2
SELECT city FROM tbliplocationsnew WHERE POWER((69.1*([longitude]- "&lon&") * cos("&lat&"/57.3)),2) + POWER((69.1*([latitude]- "&lat&")),2) < "&radius
Not sure what database you use, but I think that "^2" in SQL does not mean "squared" like in maths. You should use a math "power" function, like POWER(number,2) in SQL Server (since you use VB maybe you use SQL Server ?)
You need to have two of the same data type it's saying. SQL thinks "5" is an int. So, you should be able to trick it into treating it as a float, by putting "5.0" instead.
How do you represent this query as a Zend_Db_Select?
select * from t where id = x'0cc175b9c0f1b6a831c399e269772661';
The database is MySQL, using either PDO or mysqli adapters.
You have probably to use Zend_Db_Expr:
$adapter->select()->from('t')->where('id = ?', new Zend_Db_Expr("x'0cc175b9c0f1b6a831c399e269772661'"));
so we upgraded to newer Nhibernate and Fluent Nhibernate.
now I' getting this exception:
FailedNHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 459
On this hql, which worked fine before the upgrade.
SELECT s.StudId, s.StudLname, s.StudFname, s.StudMi, s.Ssn, s.Sex, s.Dob, et.EnrtypeId, et.Active, et.EnrId, sss.StaffLname, sss.StaffFname, sss.StaffMi,vas.CurrentAge FROM CIS3G.Jcdc.EO.StudentEO s , CIS3G.Jcdc.EO.EnrollmentEO e , CIS3G.Jcdc.EO.EnrollmentTypeEO et , CIS3G.Jcdc.EO.VwStaffStudentStaffEO sss, CIS3G.Jcdc.EO.VwAgeStudentEO vas WHERE ( e.EnrId = et.EnrId ) AND ( s.StudId = vas.StudId ) AND ( s.StudId = e.StudId ) AND ( et.EnrtypeId *= sss.EnrtypeId ) AND ( Isnull ( sss.StudStaffRoleCd , 1044 ) = 1044 ) AND ( s.StudId = 4000 )
Clearly it does nto like the *= syntax, I tried rewritign is as ansi sql outer join and no joy.
Can anyone tell me what ineed to change the sql to so I can get the outer join to work correctly?
Thanks,
Eric-
We got it working with the ansi sql and calling nhibernate slightly differently.
Is there any way to force antlr to allow t-sql, or to stop antlr parsing all together?
All this sql worked fine before we got the version that added antlr parsing.
In MANY places we are passing T-Sql to NHibernate, becasue we're porting from another app with all the complex SQl already written, and We'd like to avoid having to rewrite & retest all the sql as ansi standard.
Shouldn't the antrl parsing only be applied to HQL only not direct SQL anyway? You reduce the
native compatibility for other DBs, like ours.
Thanks,
Eric=
You don't need to do explicit joins with NHibernate in order to get the related entities. Instead, map those relations as many-to-one.
For example, your Student class could have a References(x => x.EnrollmentType). With that, you'd only need to select the student (you don't even need HQL if you know the Id; you can use session.Get) and you could just navigate to the other properties.
I recommend you read the NHibernate documentation
In HQL, how can I use bitwise operators? I want the resulting SQL query to look something like
SELECT RoleId, RoleName, RolePerms WHERE (RolePerms & #Parameter) = #Parameter
However, writing this HQL
select from Role where (RolePerms & :param) = :param
gives me this error: NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown.
I found a solution to this. Writing the HQL this way works:
select r from Role r where (r.Permissions & :param) > 0
From section 13.8 of the NHibernate documentation, HQL does not support these bitwise operators. You'd have to revert to native SQL (see section 15).
According to the NHibernate Jira it looks like this should be working as of 2.1.0Beta2, https://nhibernate.jira.com/browse/NH-1192.