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.  [ 4 posts ] 
Author Message
 Post subject: Incorrect SQL generation in case of paging
PostPosted: Wed Jan 28, 2009 1:47 pm 
Newbie

Joined: Tue May 30, 2006 11:54 am
Posts: 6
Hi all!

Short description:
We found a problem with SQL generation when try to use the following query: we JOIN two tables (in both these tables there is a column called "Name") and make ordering (ORDER BY) by "Name" column from one of the tables, and then we make paging (using SetFirstResult, SetMaxResults). SQL generated for this query is incorrect: it removes prefix (actually - table name) and we obtain ambiguous column name.

We wrote the unit-test and it fails on NH2.0. Unfortunately I couldn't find how to attach them :(

So the question is:

Do you know the way how to solve this problem? Any workaround? Or may be quick fix already exists?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2009 6:26 pm 
Newbie

Joined: Wed Feb 20, 2008 10:45 am
Posts: 10
Unfortunately it's not very easy to help you, when you provide no information about your db type or even a simple SQL statement to prove your point.

The only thing I can think of is that you might be missing aliasses for your tables?


Top
 Profile  
 
 Post subject: Detailed description
PostPosted: Fri Jan 30, 2009 7:17 am 
Newbie

Joined: Tue May 30, 2006 11:54 am
Posts: 6
Sorry about short first post - I thought somebody struggled with it and didn;t want to waste place here.
But, ok:
NHibernate 2.0GA
MS SQL Server 2005 with correspondent dialect.

Below is the test package that fully describes the problenm and how to reproduce it.

To run test:
1. Move these files to folder created in directory …\NHibernate\src\NHibernate.Test\ and include it into the project
2. Create NHTEST database, specify connection string in …\NHibernate\src\NHibernate.Test\hibernate.cfg.xml
3. Run.

Hope this will help

Mapping file (Mappings.hbm.xml):
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="NHibernate.Test">
    <class name="NHibernate.Test.Eproc2Test.User" table="Principal">
        <!-- Id -->
        <id name="Id" type="Guid" column="Id" unsaved-value="00000000-0000-0000-0000-000000000000">
            <generator class="guid.comb" />
        </id>
        <property name="Name" column="Name" not-null="true" />
        <many-to-one fetch="join" name="Organisation" class="NHibernate.Test.Eproc2Test.Organisation" column="OrganisationId"/>
    </class>
   
    <class name="NHibernate.Test.Eproc2Test.Organisation" table="Organisation">
        <!-- Id -->
        <id name="Id" type="Guid" column="Id" unsaved-value="00000000-0000-0000-0000-000000000000">
            <generator class="guid.comb" />
        </id>
       
        <property name="Name" column="Name" not-null="true" />
    </class>
</hibernate-mapping>


Organisation.cs
Code:
using System;

namespace NHibernate.Test.Eproc2Test
{
    public class Organisation
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
    }
}


User.cs
Code:
using System;

namespace NHibernate.Test.Eproc2Test
{
    public class User
    {
        public const string REL_ORGANISATION = "Organisation";

        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Organisation Organisation { get; set; }
    }
}


Fixture.cs
Code:
using System.Collections;
using NHibernate.Criterion;
using NHibernate.SqlCommand;
using NUnit.Framework;

namespace NHibernate.Test.Eproc2Test
{
    [TestFixture]
    public class AmbigiousFieldTest : TestCase
    {
        private const string ORG_ALIAS = "OrganisationAlias";
        private const string ORG_NAME_ALIAS = "OrganisationName";
        private const string PROP_NAME = "Name";

        [Test]
        public void SortByAmbigiousField()
        {
            var criteria = cfg.BuildSessionFactory().OpenSession().CreateCriteria(typeof(User));

            criteria.CreateCriteria(User.REL_ORGANISATION, ORG_ALIAS, JoinType.InnerJoin);
            criteria.SetProjection(Projections.Property(PROP_NAME));
            criteria.SetProjection(Projections.Alias(Projections.Property(ORG_ALIAS+"."+PROP_NAME), ORG_NAME_ALIAS));
            criteria.AddOrder(Order.Asc(ORG_NAME_ALIAS));

            // bug appeared when paging enabled
            criteria.SetFirstResult(0);
            criteria.SetMaxResults(15);

            // exception is thrown
            criteria.List();
        }

        /// <summary>
        /// Mapping files used in the TestCase
        /// </summary>
        protected override IList Mappings
        {
            get { return new[] { "Eproc2Test.Mappings.hbm.xml" }; }
        }

        /// <summary>
        /// Assembly to load mapping files from (default is NHibernate.DomainModel).
        /// </summary>
        protected override string MappingsAssembly
        {
            get { return "NHibernate.Test"; }
        }
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 1:37 pm 
Newbie

Joined: Wed Feb 20, 2008 10:45 am
Posts: 10
Any luck solving your problem? I haven't got any time at the moment, but if you come any closer to a reason to your issue, please post...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.