Hi!
In my database I have Employee's and Statement's. Between those there is a many-to-one relationship, each Employee can have multiple Statement's, each Statement is owned by exactly one Employee.
I believe my mapping is correct, at least it works for most queries, but it doesn't for the following one:
Quote:
Code:
from Statement sm
where sm.acctNumber = :acctNumber
or sm.employee.lastName = :lastName
What I'm trying to do is obviously to select statements by their account number or by their corresponding Employee's last name.
Hibernate creates a query like this:
Quote:
Code:
select [...]
from tblStatement statement0_, tblEmployee employee1_
where (statement0_.acctNumber= ? or employee1_.lastName like ? and statement0_.EmployeeID=employee1_.ID)
Since AND's are evaluated first, I would expect it to put parenthesis around the two conditions that are OR'ed:
Quote:
Code:
select [...]
from tblStatement statement0_, tblEmployee employee1_
where ((statement0_.acctNumber= ? or employee1_.lastName like ?) and statement0_.EmployeeID=employee1_.ID)
If I specify an existing account number together with a non-existing lastname, the SQL code generated by Hibernate returns the row containing that account number joined with each and every row in tblEmployee, since the AND condition is evaluated first and is false (because the lastname is non-existing) only the account number condition is taken into consideration that is true for every join of that row in tblStatement and every row in tblEmployee.
Am I getting something wrong? Or is it something else?
Thanks for your help in advance!
Here's Hibernate version, excerpts of mapping files, etc.:
Hibernate version: 2.1.8
Mapping documents:Quote:
Statement.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="someProject.beans.Statement"
table="tblStatement"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="ID"
column="ID"
type="java.lang.Integer"
>
<generator class="native">
</generator>
</id>
[...]
<many-to-one
name="employee"
class="someProject.beans.Employee"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="EmployeeID"
/>
[...]
Quote:
Employee.hbm.xml:
Code:
<hibernate-mapping>
<class
name="someProject.beans.Employee"
table="tblEmployee"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="ID"
column="ID"
type="java.lang.Integer"
>
<generator class="native">
</generator>
</id>
[...]
<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="lastName"
/>
[...]
</class>
</hibernate-mapping>
Name and version of the database you are using: SQL Server 2000