简体   繁体   中英

NHibernate returning wrong results of SQL query

I have a query which contains in the select clause:

    d1.id,
    d1.title,
    d1.original_doc,
    d2.id,
    d2.title

And in the from clause:

    left outer join documents d2 on d1.original_doc = d2.id

Let's say in the DB I have these rows: ID TITLE ORIGINAL_DOC

    1 Title1 
    2 Title2 1

For the first row, NHibernate will return 1, Title1, null, 1, Title1 instead of 1, Title1, null, null, null.

For the second row, it will return 2, Title2, 1, 2, Title2 instead of 2, Title2, 1, 1, Title1.

Note that the third and fourth column should not contain different values, as the condition of the join is them being the same. What seems to be happening is that I am getting the same d1 row joined as d2, instead of the row defined by the condition.

Now the weird part: If I turn .ShowSql() on and copy-paste the query into Oracle Toad, it actually returns the correct results!

I am getting the result set in NHibernate by using var data = session.CreateSQLQuery(selectQuery).List()

Anyone got any ideas?

I reported exactly this as a bug , but it's not a bug. NH manages the result internally using column names. If they have the same name, it picks the first. You need to give it unique names in the select clause:

SELECT
  d1.id as id1,
  d1.title as title1,
  d1.original_doc as original_doc1,
  d2.id as id2,
  d2.title as title2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM