-->
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.  [ 11 posts ] 
Author Message
 Post subject: DELETE and INSERT in HQL
PostPosted: Thu Nov 23, 2006 6:20 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear members,

1) How to use INSERT and DELETE in HQL ? (if that is not possible,how would be the syntax if i want to Delete and Insert using sql instead of hql?)

2) I'm still facing problems in the Lazy loading.
When i'm loading an object that has collections as member varialbes , i see that all these collections are loaded too (when setting breakpoints) !
An example would be perfect as a demonstration for the steps to enable lazy loading , from mapping to c# classes ...

thanks for your time,
Regards,
JojoRico

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 9:01 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
mmm, I'm a Hibernate user not a NHibernate user,

are you sure that the collection is not lazy fetched? see the queries, I believe you invoke the accessor unwillingly using your debugger.

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 9:05 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
mmm, I'm a Hibernate user not a NHibernate user,

are you sure that the collection is not lazy fetched? see the queries, I believe you invoke the accessor unwillingly using your debugger.

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 11:03 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hello,

I do not think it's possible to use INSERT or DELETE in HQL, but you'd rather use the session object (session.Save(object) or session.Delete(object)) to accomplish this.

If you have a specific reason to use SQL anyway you could do this like this:
Code:
ISession session =  ... ; // instantiate your session here
string sql = string.Format("DELETE FROM {0} WHERE {0}.name='a name'","{nameoftable}");
session.CreateSQLQuery(sql,"nameoftable",typeof(somemappedtype).UniqueResult();
session.Dispose();


Still, I'd advise you to consider using purely NHibernate. I'm sure there must be a way to solve your problem with it, and I don't think it's a good idea to mix NHibernate queries with SQL-queries (it won't promote portability between databases).

Lazy fetching : you cannot use your debugger to know this because, once you inspect the value of the property, the collection will be fetched.
What you can do: check out the logs -> these will mention when the collection is lazily fetched.
On the other hand, what you can do to quickly test this: get your object using a NHibernate session, dispose the session and then try to access the collection: if you get a lazy loading error you'll know the collection was not loaded.

Please rate this post if it helped.
X.
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 11:27 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Thanks pasha,xasp

although the reply of pasha was good, but i've found xasp reply more useful in my case but thanks for you both ..
in fact, i have the profile (sqlserver 2005 helper) which i'm using to trace my database activity, so i'm sure that lazy loading is not functionning in my case ! i think i did what is supposed to be done in order to have lazy laoding enabled, and i don't know what am i missign. that's why i need the steps with aas much detail as there may be !


Regards,
JojoRico

_________________
In Code We Trust


Top
 Profile  
 
 Post subject: Enable lazy loading
PostPosted: Sun Nov 26, 2006 5:10 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

Fact is, to enable lazy loading of collections, there's nothing specific you should do. It's rather not enabling lazy loading that requires you to add some specific tags.
Following is a declaration of a lazy-loaded collection:
Code:
<set name="CityCollection" lazy="true" inverse="true" cascade="all-delete-orphan">
      <key column="countryid"/>
      <one-to-many class="AccWareLib.City,AccWareLib"/>
    </set>


Note the lazy="true" tag: if fact this info is redundant because lazy loading of collections is true by default. Unless you set lazy="false", the collection should be loaded lazily.

Which leads me to think there might be a problem with you code. For example: is it possible that somewhere in the setter of a property you're trying to access the property holding the collection? This would again trigger the loading of the collection. Try putting a breakpoint in the getter of the property holding the collection and see who's calling it.

Please rate this post if it helped.
X.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 26, 2006 4:23 pm 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear xasp,

i'm using NHibernate v1.0.2, is it still applying (lazy) by default as you've already said ?
what about proxies ? virtual ? don't they affect the Lazy loading besides of other details ? i'm pointing to these details(steps) in my question .

thanks for your reply,
Regards,
JojoRico

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 10:12 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

As far as I understood it, the lazy loading of collections by default predates the version 1.2.x (so it should also be lazy by default for 1.0.3).
Since version 1.2.x your class itself will be lazy by default : which means that for your class a proxy might be created under certain circumstances unless you specify lazy=false on class-level.

For example:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="classname" table="tablename" [b]lazy="false"[/b]>
   
    <property name="prop1" column="prop1col" type="String"/>
   
    <many-to-one name="association" column="association_id" class="Association"/>   

    <set name="Collection" lazy="true" cascade="all-delete-orphan" inverse="true">
      <key column="class_id"/>
      <one-to-many class="anotherclassname"/>
    </set>
  </class>
</hibernate-mapping>


NHibernate will create proxies for your classes unless they're marked as being lazy. Note that this applies to associations, not collections. Fetching a collection should always return the objects themselves not a proxy.
You'll find an example of such an association above ("Association"). In the example above, if class 'Association' was marked lazy=true you'll get a proxy to Association, otherwise you'll get an Association-object.

This means that:
a) if you get an object A that is marked as being lazy=true, you'll get that object A
b) if that object A references itself another object B that is being marked lazy=true, your object A will contain a proxy to that referenced object B (the proxy of B will only be initialized when you first call one of it's properties).
c) if your object A has a collection of objects C that are marked as lazy=true and you initialize that collection, you'll get a collection of objects C (not proxies).

Please rate this post if it helped.
X.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 10:17 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Small correction on last post:

The phrase
"NHibernate will create proxies for your classes unless they're marked as being lazy."
should read:
"NHibernate will create proxies for your classes when they're referenced in associations, unless they're marked as being lazy=false."

Please rate this post if it helped.
X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 1:58 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear xasp,

i'm using NHibernate v1.0.2, so what are the exact steps to follow in order to enable lazy loading ?!!!

Regards,
JojoRico

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 3:49 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hello,

I think just declaring a collection without any lazy-attribute should do the trick, and if you want to be sure, declare the collection with "lazy=true".

For example:
Code:
<set name="Collection" lazy="true" cascade="all-delete-orphan" inverse="true">
      <key column="class_id"/>
      <one-to-many class="anotherclassname"/>
    </set>


If this doesn't work, there must be a problem somewhere else. You might also consider posting your mapping files and code files.

X.


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