-->
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.  [ 2 posts ] 
Author Message
 Post subject: one-to-one wrong mapping
PostPosted: Thu Dec 13, 2007 3:32 pm 
Newbie

Joined: Thu Dec 13, 2007 3:13 pm
Posts: 9
hi there,

i encountered a very strange problem with nhibernate and sql server 2005 express.
i have 3 classes all related to each other in SQL Server.
when i do the query stated below ("from vehicle v where v.device.imei = '123456'") , it retrieves a row from the database. but the wrong row.
if i do a manual select in sql (select * from vehicles,devices where vehicles.devicid = devices.deviceid and devices.deviceIMEI = '123456') it returns the correct vehicle, but it doesnt with the HQL statement.
any ideas what to do?
it works on 2 machines, but not on another one...
i´m kinda desperate ^^

NHibernate version:
Build 1.2.0.GA

Mapping documents:

<class name="MyNamespaces.Vehicle, MyFile" table="Vehicles" lazy="true">
<id name="VehicleID" column="VehicleID" type="int">
<generator class="identity"/>
</id>

<one-to-one name="Device" class="MyNamespace.Device, MyFile"
fetch="join"/>

</class>

<class name="MyNamespace.Waypoint, MyFile" table="WayPoints" lazy="false">
<id name="ID" column="WaypointID" type="Int64">
<generator class="identity"/>
</id>
<many-to-one name="Vehicle" class="MyNamespace.Vehicle, MyFile" column="VehicleID"/>


</class>
<class name="MyNameSpace.Device, MyFile" table="Devices" lazy="false">
<id name="DeviceID" column="DeviceID" type="integer">
<generator class="identity"/>
</id>
<property name="Type" column="DeviceType" type="integer"/>
<property name="IMEI" column="DeviceIMEI" type="string" length="15"/>
</class>

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


Code:
IQuery q = session.CreateQuery(string.Format("from Vehicle as v where v.Device.IMEI = '{0}'",data.IMEI));
                   


Full stack trace of any exception that occurs:
none

Name and version of the database you are using:
SQL Server 2005 Express

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: Re: one-to-one wrong mapping
PostPosted: Mon Dec 17, 2007 12:47 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
jkersch wrote:
Code:
IQuery q = session.CreateQuery(string.Format("from Vehicle as v where v.Device.IMEI = '{0}'",data.IMEI));
                   



First of all, use a parameter:

Code:
IQuery q = session.CreateQuery("from Vehicle as v where v.Device.IMEI = :imei");
q.SetSting("imei", data.IMEI);


Emedded strings leave you open to SQL injection attacks, and are widely recommended against. If you are still having problems, reply back.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

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.