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: Proper Delete Query in nHibernate
PostPosted: Tue Sep 21, 2010 9:35 am 
Newbie

Joined: Tue Sep 21, 2010 9:09 am
Posts: 2
hi everyone, im totally new to nhibernate.
currently im trying to use it in C# project.

but I ran across problem when I want to delete record.

here's my mapping for ItemUnit table
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Testing"
                   namespace="Model">

  <class name="ItemUnit" table="ItemUnit">

    //This is how I setup the composite ID
    <composite-id>
      <key-many-to-one name="ItemDetail" class="Item" column="Item" lazy="false" />
      <key-many-to-one name="UnitDetail" class="Unit" column="Unit" lazy="false" />
    </composite-id>

    <property name="Coefisien"   column="Coefisien"  type="String"/>
    <property name="AuStatus"  column="AuStatus"  type="String"/>
    <property name="AuUser"  column="AuUser"  type="int"/>
    <property name="AuTime"  column="AuTime"  type="DateTime"/>

  </class>
</hibernate-mapping>



and here's my code:
Code:
            //This is code to get pre-coded ID from a combobox

            PairData pairItem = (PairData)cmbItem.SelectedItem;
            int itemKey = pairItem.key;

            // Setup nhibernate configuration

            Configuration config = new Configuration();
            config.AddAssembly(typeof(ItemUnit).Assembly);

            // Setup nhibernate session

            ISessionFactory factory = config.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();

            IQuery query = session.CreateQuery("SELECT a FROM ItemUnit a WHERE a.ItemDetail = " + itemKey);           
            IList<ItemUnit> itemunit = query.List<ItemUnit>();

            session.Delete(itemunit);


i got error message "Unknown entity class: System.Collections.Generic.List`1[[Model.ItemUnit, Testing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"

when I tried to change the code like this:

Code:
            //This is code to get pre-coded ID from a combobox

            PairData pairItem = (PairData)cmbItem.SelectedItem;
            int itemKey = pairItem.key;

            // Setup nhibernate configuration

            Configuration config = new Configuration();
            config.AddAssembly(typeof(ItemUnit).Assembly);

            // Setup nhibernate session

            ISessionFactory factory = config.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();

            Object result = session.CreateSQLQuery("DELETE from ItemUnit");
            session.Delete(result);


I got error message "Unknown entity class: NHibernate.Impl.SqlQueryImpl". FYI i have already using NHibernate.Impl.

so, how to do it properly?


thx :)


Top
 Profile  
 
 Post subject: Re: Proper Delete Query in nHibernate
PostPosted: Wed Sep 22, 2010 1:46 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Is your mapping files marked as "Embedded Resource" ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Proper Delete Query in nHibernate
PostPosted: Wed Sep 22, 2010 2:17 am 
Newbie

Joined: Tue Sep 21, 2010 9:09 am
Posts: 2
[quote=wolli]Is your mapping files marked as "Embedded Resource" ?[/quote]

thx for ur reply, yes.

btw, I succeded using this CreateSQLQuery:

Code:
    Object result = session.CreateSQLQuery("DELETE from ItemUnit where item = :itm")
                                .AddScalar("count", NHibernateUtil.Int32)
                                .SetParameter("itm", itemKey)
                                .UniqueResult();


so, my delete problem, less or more, is solved.
although I still want to know how to delete using CreateQuery.


Top
 Profile  
 
 Post subject: Re: Proper Delete Query in nHibernate
PostPosted: Wed Sep 22, 2010 2:30 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Ah, sorry, should've read your code more carefully. session.Delete doesn't accept lists. This should work:

IQuery query = session.CreateQuery("SELECT a FROM ItemUnit a WHERE a.ItemDetail = " + itemKey);
IList<ItemUnit> itemunit = query.List<ItemUnit>();

foreach( ItermUnit iu in itemunit)
session.Delete(iu);


CreateQuery can only be used for select.

_________________
--Wolfgang


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.