-->
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: SQL is missing parenthesis ==> foreign key condition igno
PostPosted: Thu Mar 17, 2005 9:04 pm 
Newbie

Joined: Thu Feb 17, 2005 7:31 pm
Posts: 1
Location: Iselin, NJ
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


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.