-->
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.  [ 3 posts ] 
Author Message
 Post subject: NHibernate generates invalid SQL Statement
PostPosted: Tue Apr 18, 2006 10:48 am 
Newbie

Joined: Wed Feb 01, 2006 10:45 am
Posts: 18
Location: Salzburg, Austria
Hi Folks,

i think i found a bug in NHibernate 1.0.2. My HQL produces following exception:
Code:
2006-04-18 16:09:37,031 [1940] WARN  NHibernate.Util.ADOExceptionReporter - System.Data.OracleClient.OracleException: ORA-00907: missing right parenthesis


The reason for this lies in the way NHibernate integrates formulas into its
generated SQL statement (same mapping and same HQL works in Hibernate 3.1).

SQL Generated by NHibernate 1.0.2:
Code:
select invoice0_.RECH_SYSNR as RECH_SYSNR0_, customer1_.KUNDE_SYSNR as KUNDE_SY1_1_, invoiceite2_.RECHPOS_SYSNR as RECHPOS_1_2_, invoice0_.KUNDE_SYSNR as KUNDE_SY2_0_, invoice0_.BEZAHLTKZ as BEZAHLTKZ0_,
            ( SELECT sum(r.menge * a.preis)
            FROM RECHNUNGSPOS r, invoice0_.ARTIKEL invoice0_.a
            WHERE r.artikel_sysnr = a.art_sysnr
              AND r.rech_sysnr = invoice0_.RECH_SYSNR) as f0_0_, customer1_.NACHNAME as NACHNAME1_, customer1_.ROLLE as ROLLE1_, customer1_.LOESCHKZ as LOESCHKZ1_, customer1_.LETZTEAEND as LETZTEAEND1_, customer1_.PLZ as PLZ1_, customer1_.GESCHLECHT as GESCHLECHT1_, customer1_.ORT as ORT1_, customer1_.VORNAME as VORNAME1_, customer1_.TELEFON as TELEFON1_, customer1_.STRASSE as STRASSE1_, customer1_.GEBURTSDATUM as GEBURTSD7_1_, invoiceite2_.MENGE as MENGE2_, invoiceite2_.ARTIKEL_SYSNR as ARTIKEL_4_2_, invoiceite2_.RECH_SYSNR as RECH_SYSNR2_, invoiceite2_.RECH_SYSNR as RECH_SYSNR__, invoiceite2_.RECHPOS_SYSNR as RECHPOS_1___ from RECHNUNG invoice0_ left outer join KUNDE customer1_ on invoice0_.KUNDE_SYSNR=customer1_.KUNDE_SYSNR left outer join RECHNUNGSPOS invoiceite2_ on invoice0_.RECH_SYSNR=invoiceite2_.RECH_SYSNR where (invoice0_.RECH_SYSNR=:p0) order by invoiceite2_.RECHPOS_SYSNR desc


SQL Generated by Hibernate 3.1:
Code:
select invoice0_.RECH_SYSNR as RECH1_3_0_, customer1_.KUNDE_SYSNR as KUNDE1_0_1_, invoiceite2_.RECHPOS_SYSNR as RECHPOS1_4_2_, invoice0_.KUNDE_SYSNR as KUNDE2_3_0_, invoice0_.BEZAHLTKZ as BEZAHLTKZ3_0_,
            ( SELECT sum(r.menge * a.preis)
            FROM RECHNUNGSPOS r, ARTIKEL a
            WHERE r.artikel_sysnr = a.art_sysnr
              AND r.rech_sysnr    = invoice0_.RECH_SYSNR )
            as formula0_0_, customer1_.ANREDE_SYSNR as ANREDE2_0_1_, customer1_.NACHNAME as NACHNAME0_1_, customer1_.VORNAME as VORNAME0_1_, customer1_.STRASSE as STRASSE0_1_, customer1_.PLZ as PLZ0_1_, customer1_.ORT as ORT0_1_, customer1_.GEBURTSDATUM as GEBURTSD8_0_1_, customer1_.TELEFON as TELEFON0_1_, customer1_.GESCHLECHT as GESCHLECHT0_1_, customer1_.ROLLE as ROLLE0_1_, customer1_.LETZTEAEND as LETZTEAEND0_1_, customer1_.LOESCHKZ as LOESCHKZ0_1_, invoiceite2_.MENGE as MENGE4_2_, invoiceite2_.RECH_SYSNR as RECH3_4_2_, invoiceite2_.ARTIKEL_SYSNR as ARTIKEL4_4_2_, invoiceite2_.RECH_SYSNR as RECH3_0__, invoiceite2_.RECHPOS_SYSNR as RECHPOS1_0__ from RECHNUNG invoice0_ left outer join KUNDE customer1_ on invoice0_.KUNDE_SYSNR=customer1_.KUNDE_SYSNR left outer join RECHNUNGSPOS invoiceite2_ on invoice0_.RECH_SYSNR=invoiceite2_.RECH_SYSNR where invoice0_.RECH_SYSNR=? order by invoiceite2_.RECHPOS_SYSNR desc


The exception is caused by following code snippet:
( SELECT sum(r.menge * a.preis)
FROM RECHNUNGSPOS r, invoice0_.ARTIKEL invoice0_.a
WHERE r.artikel_sysnr = a.art_sysnr

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="com.poi.poa.cross.model.Title, CROSS_ASP.model" table="ANREDE">
        <id name="id" column="ANREDE_SYSNR" type="Int64">
            <generator class="increment" />
        </id>
        <property name="text" column="MYTEXT" />
        <property name="isdeleted" column="LOESCHKZ" />
    </class>
</hibernate-mapping>

Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="com.poi.poa.cross.model.Invoice, CROSS_ASP.model"
         table="RECHNUNG" dynamic-insert="true" dynamic-update="true">
        <id name="id" column="RECH_SYSNR" type="Int64">
            <generator class="increment" />
      </id>
       <many-to-one name="customer" column="KUNDE_SYSNR" 
                 class="com.poi.poa.cross.model.Customer, CROSS_ASP.model"
                 not-null="true" />      
      <set name="invoiceItems" table="RECHNUNGSPOS"
                inverse="true" cascade="all" order-by="RECHPOS_SYSNR desc">
            <key column="RECH_SYSNR" />
          <one-to-many class="com.poi.poa.cross.model.InvoiceItem, CROSS_ASP.model" />
      </set>
       
      <property name="isPaid" column="BEZAHLTKZ" />
      
       <property name="total"
           formula="
            ( SELECT sum(r.menge * a.preis)
            FROM RECHNUNGSPOS r, ARTIKEL a
            WHERE r.artikel_sysnr = a.art_sysnr
              AND r.rech_sysnr = RECH_SYSNR)" />
      
   </class>
</hibernate-mapping>

Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="com.poi.poa.cross.model.InvoiceItem, CROSS_ASP.model" table="RECHNUNGSPOS"
         dynamic-insert="true" dynamic-update="true">
        <id name="id" column="RECHPOS_SYSNR" type="Int64">
            <generator class="increment" />
      </id>
     
      <property name="quantity"    column="MENGE" type="Double" />
      

      <many-to-one name="invoice" column="RECH_SYSNR"
                class="com.poi.poa.cross.model.Invoice, CROSS_ASP.model" />

      <many-to-one name="article" column="ARTIKEL_SYSNR"
                class="com.poi.poa.cross.model.Article, CROSS_ASP.model" />
     
   </class>
</hibernate-mapping>

Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">   
   <class name="com.poi.poa.cross.model.Customer, CROSS_ASP.model"
          table="KUNDE" dynamic-insert="true" dynamic-update="true">
        <id name="id" column="KUNDE_SYSNR" type="Int64">
            <generator class="increment" />
        </id>
        <property name="lastname"    column="NACHNAME" />
        <property name="firstname"    column="VORNAME" />
        <property name="street"      column="STRASSE" />
        <property name="zipcode"   column="PLZ" />
        <property name="city"      column="ORT" />
        <property name="birthday"   column="GEBURTSDATUM" type="DateTime"  />
        <property name="tel"      column="TELEFON" />
        <property name="sex"      column="GESCHLECHT" />
        <property name="role"      column="ROLLE" />
        <property name="lastchange" column="LETZTEAEND" type="DateTime" />
        <property name="isdeleted"  column="LOESCHKZ" type="Boolean"/>
    </class>
</hibernate-mapping>


Used HQL Statement:
Code:
        #region IInvoiceDAO Members

        public Invoice loadInvoice(Int64? id)
        {
            return (Invoice)sessionFactory.OpenSession()
                                         .CreateQuery("FROM Invoice i " +
                                                      "LEFT JOIN FETCH i.customer " +
                                                      "LEFT JOIN FETCH i.invoiceItems " +
                                                      "WHERE i.id = :iid")
                                         .SetParameter("iid", id)
                                         .UniqueResult();
        }


Does anyone know how to solve this?

Thanks in advance.

_________________
greetings,
kris


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 12:25 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
The formula parser in NHibernate is pretty bad (as it was in Hibernate 2.1 compared to 3.1), so most of these errors are going to be hard to work around.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 2:06 am 
Newbie

Joined: Wed Feb 01, 2006 10:45 am
Posts: 18
Location: Salzburg, Austria
hi sergey,

should i post this bug anywhere?

Do you have an idea how to "rewrite" my mapping? Maybe
without using a formula.

The other possiblity i see to solve this is to use a second
sql statement to determine the sum.

thanks

_________________
greetings,
kris


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