-->
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.  [ 10 posts ] 
Author Message
 Post subject: [updated] sess.load<> doesnt, but sess.createQuery wor
PostPosted: Mon Feb 18, 2008 11:45 am 
Newbie

Joined: Fri Feb 15, 2008 12:13 pm
Posts: 15
Hi, I'm in trouble with loading a collection-property.

first, some basic information:

Code:
<class name="Umm.Poco.Event, Umm" table="tb_umm_event">
  <bag name="Messages" table="tb_umm_message" inverse="true" order-by="ora_timestamp desc">
    <key column="FK_EVENT" />
    <one-to-many class="Umm.Poco.UmmMessage, Umm"/>
  </bag>
</class>

Code:
<class name="Umm.Poco.UmmMessage, Umm" table="tb_umm_msg">
  <many-to-one name="Event_" class="Umm.Poco.Event, Umm" column="FK_EVENT" not-null="true" cascade="save-update"/>
</class>


Code:
public class Controler
    {
        static void Main()
        {
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure();
            cfg.AddAssembly(typeof(Controler).Assembly);

            ISessionFactory sessionFactory = cfg.BuildSessionFactory();

            ISession session = sessionFactory.OpenSession();
            ITransaction tx = session.BeginTransaction();
            Event e2 = session.Load<Event>(334);
            Event e = (Event)session.CreateQuery("from Umm.Poco.Event where Event_Id = 335").UniqueResult();
           
            tx.Commit();
            session.Close();
   }
}


e will always have the correct initialies e.Messages collection, but e2.Messages will always be empty. It doesn't mind which IDs I use.


Can someone help and tell why session.Load<> might not work for me?

thanks a lot :o)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 7:51 am 
Newbie

Joined: Fri Feb 15, 2008 12:13 pm
Posts: 15
any idea?

i found several possibilites to get the collection initialised, but it never succeeded through session.Load. I'm very confused now :S


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 1:49 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
The ids in your example are different.. I presume that's not the case in the life environment?

Try turning nhibernate.show_sql on in the session factory configuration and see what it's outputting for each part of that.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 3:41 am 
Newbie

Joined: Fri Feb 15, 2008 12:13 pm
Posts: 15
no, it really does not matter which id I use. I just had to use different in one run (caching reason).

here the code and corresponding logs:

Code:
            ISession session = sessionFactory.OpenSession();
            ITransaction tx = session.BeginTransaction();
            Event e2 = session.Load<Event>(334);
                       
            tx.Commit();
            session.Close();

results in
Quote:
19.02.08 08:54:39 DEBUG - opened session
19.02.08 08:54:40 DEBUG - begin
19.02.08 08:54:40 DEBUG - Obtaining IDbConnection from Driver
19.02.08 08:54:41 DEBUG - loading [Event#334]
19.02.08 08:54:44 DEBUG - commit
19.02.08 08:54:44 DEBUG - flushing session
19.02.08 08:54:44 DEBUG - Flushing entities and processing referenced collections
19.02.08 08:54:44 DEBUG - Processing unreferenced collections
19.02.08 08:54:44 DEBUG - scheduling collection removes/(re)creates/updates
19.02.08 08:54:44 DEBUG - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
19.02.08 08:54:44 DEBUG - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
19.02.08 08:54:44 DEBUG - executing flush
19.02.08 08:54:44 DEBUG - registering flush begin
19.02.08 08:54:44 DEBUG - registering flush end
19.02.08 08:54:44 DEBUG - post flush
19.02.08 08:54:44 DEBUG - before transaction completion
19.02.08 08:54:44 DEBUG - aggressively releasing database connection
19.02.08 08:54:44 DEBUG - Closing connection
19.02.08 08:54:44 DEBUG - transaction completion
19.02.08 08:54:44 DEBUG - running AdoTransaction.Dispose()
19.02.08 08:54:44 DEBUG - closing session
19.02.08 08:54:44 DEBUG - running BatcherImpl.Dispose(true)


and

Code:
ISession session = SessionFactory.OpenSession();

            try
            {
                string q = "from Umm.Poco.Event where Event_id = :id";
                value = (T)session.CreateQuery(q).SetParameter("id", id).UniqueResult();
            }           
            finally
            {
                session.Close();
            }

results in
Quote:
20.02.08 08:38:00 DEBUG - opened session
20.02.08 08:38:00 DEBUG - find: from Umm.Poco.Event where Event_id = :id
20.02.08 08:38:00 DEBUG - named parameters: {id=387}
20.02.08 08:38:00 DEBUG - compiling query
20.02.08 08:38:01 DEBUG - flushing session
20.02.08 08:38:01 DEBUG - Flushing entities and processing referenced collections
20.02.08 08:38:01 DEBUG - Processing unreferenced collections
20.02.08 08:38:01 DEBUG - scheduling collection removes/(re)creates/updates
20.02.08 08:38:01 DEBUG - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
20.02.08 08:38:01 DEBUG - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
20.02.08 08:38:01 DEBUG - dont need to execute flush
20.02.08 08:38:01 DEBUG - HQL: from Umm.Poco.Event where Event_id = :id
20.02.08 08:38:01 DEBUG - SQL: select event0_.event_id as event1_1_, event0_.Event_bezeichnung as Event2_1_ from tb_umm_event event0_ where (Event_id=? )
20.02.08 08:38:01 DEBUG - Opened new IDbCommand, open IDbCommands: 1
20.02.08 08:38:01 DEBUG - Building an IDbCommand object for the SqlString: select event0_.event_id as event1_1_, event0_.Event_bezeichnung as Event2_1_ from tb_umm_event event0_ where (Event_id=? )
20.02.08 08:38:01 DEBUG - BindNamedParameters() 387 -> id [0]
20.02.08 08:38:01 DEBUG - binding '387' to parameter: 0
20.02.08 08:38:01 INFO - select event0_.event_id as event1_1_, event0_.Event_bezeichnung as Event2_1_ from tb_umm_event event0_ where (Event_id=:p0 )
20.02.08 08:38:01 DEBUG - select event0_.event_id as event1_1_, event0_.Event_bezeichnung as Event2_1_ from tb_umm_event event0_ where (Event_id=:p0 ); :p0 = '387'
20.02.08 08:38:01 DEBUG - Obtaining IDbConnection from Driver
20.02.08 08:38:01 DEBUG - Opened IDataReader, open IDataReaders: 1
20.02.08 08:38:01 DEBUG - processing result set
20.02.08 08:38:01 DEBUG - result set row: 0
20.02.08 08:38:01 DEBUG - returning '387' as column: event1_1_
20.02.08 08:38:01 DEBUG - result row: 387
20.02.08 08:38:01 DEBUG - Initializing object from DataReader: [Umm.Poco.Event#387]
20.02.08 08:38:01 DEBUG - Hydrating entity: Umm.Poco.Event#387
20.02.08 08:38:01 DEBUG - returning '2008-16' as column: Event2_1_
20.02.08 08:38:01 DEBUG - done processing result set (1 rows)
20.02.08 08:38:01 DEBUG - Closed IDataReader, open IDataReaders :0
20.02.08 08:38:01 DEBUG - Closed IDbCommand, open IDbCommands: 0
20.02.08 08:38:01 DEBUG - aggressively releasing database connection
20.02.08 08:38:01 DEBUG - Closing connection
20.02.08 08:38:01 DEBUG - total objects hydrated: 1
20.02.08 08:38:01 DEBUG - resolving associations for: [Umm.Poco.Event#387]
20.02.08 08:38:01 DEBUG - creating collection wrapper:[Umm.Poco.Event.Messages#387]
20.02.08 08:38:01 DEBUG - Entity load: Umm.Poco.Event(ID = 387)
20.02.08 08:38:01 DEBUG - done materializing entity [Umm.Poco.Event#387]
20.02.08 08:38:01 DEBUG - initializing non-lazy collections
20.02.08 08:38:01 DEBUG - after autocommit
20.02.08 08:38:01 DEBUG - aggressively releasing database connection
20.02.08 08:38:01 DEBUG - transaction completion
20.02.08 08:38:01 DEBUG - closing session
20.02.08 08:38:01 DEBUG - running BatcherImpl.Dispose(true)


Sure, I see there's a difference. But I cannot get the cause.
Only significiant difference in code is using session.CreateQuery instead of session.Load<>..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 11:35 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Well, yes, there's a difference - Load and Query wouldn't be expected to work the same. Turn on show_sql in the hibernate settings (not the log4net loggers) and post that output. Again, run it with the same id if you can to help isolate the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 12:15 pm 
Newbie

Joined: Fri Feb 15, 2008 12:13 pm
Posts: 15
okay, sorry for the wrong logs.

I'm not sure which part of log is of interest.

however, in both cases,
Quote:
DEBUG 20.02.08 17:02:10 [ 3260]: Static select for one-to-many Umm.Poco.Event.Messages: SELECT messages0_.FK_EVENT as FK18___1_, messages0_.UMM_MSG_ID as UMM1_1_, messages0_.UMM_MSG_ID as UMM1_2_0_, messages0_.Provider as Provider2_0_, messages0_.Providers_msg_id as Providers4_2_0_, messages0_.Start_Event as Start5_2_0_, messages0_.Stop_Event as Stop6_2_0_, messages0_.Event_Reason as Event7_2_0_, messages0_.Estimated_Stopevent_From as Estimated8_2_0_, messages0_.Estimated_Stopevent_To as Estimated9_2_0_, messages0_.PrimaryEnergie as Primary10_2_0_, messages0_.Generating_Unit as Generating11_2_0_, messages0_.Event_Update as Event12_2_0_, messages0_.Net_Capacity as Net13_2_0_, messages0_.Applicable_Capacity as Applicable14_2_0_, messages0_.Reduced_Output as Reduced15_2_0_, messages0_.State as State2_0_, messages0_.Ora_Timestamp as Ora17_2_0_, messages0_.FK_EVENT as FK18_2_0_, messages0_.provider as provider0_ FROM tb_umm_msg messages0_ WHERE messages0_.FK_EVENT=? ORDER BY messages0_.event_update desc

is the same.

significiant difference in both logs begin at "Instantiated session factory". In error case, no sql will follow then.

Could you tell me whats significiant in the logs, since I guess its quite much to post hundreds of log-lines?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 12:23 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
When you say e2.Messages is 'empty' do you mean null or count == 0? And are you sure it's getting initialised? The collection is lazily loaded (from your mapping) so when you examine it (in the debugger or in code) you should see some SQL generated at that stage.

If you're definite that both those ids have some messages in the database I'm stuck; maybe someone else can help.

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 12:33 pm 
Newbie

Joined: Fri Feb 15, 2008 12:13 pm
Posts: 15
its empty if i initialise the list in Event's Constructor. If not, it will be null.

In error case, no sql is generated on session.Refresh(event) respectivly accessing the collection.


further, i can guarantee its no matter of id, since i can use the same for both tries.


my workaround is using a utitliy-method like this:
Code:
..
//value = session.Load<T>(id); doesn't work/ collections null/empty
string idProp = ((SingleTableEntityPersister)SessionFactory.GetClassMetadata(typeof(T))).IdentifierPropertyName;
string q = "from " + typeof(T).FullName + " where " + idProp + " = :id";
value = (T)session.CreateQuery(q).SetParameter("id", id).UniqueResult(); // works. collections correctly initialized
..


anyway, I don't like this workaround. its useless generated complexity.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 12:44 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
One last thing (I don't really have any other ideas, sorry) - in your mapping the table names for the collection are different - in the Event class the mapping specifies "tb_umm_message" whereas the class itself is "tb_umm_msg".

Other than that, sorry, I don't know what else it could be. You definitely shouldn't have to do the workaround you describe though.

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 4:16 am 
Newbie

Joined: Fri Feb 15, 2008 12:13 pm
Posts: 15
SteveMc wrote:
in your mapping the table names for the collection are different - in the Event class the mapping specifies "tb_umm_message" whereas the class itself is "tb_umm_msg".


Oh..you're right. tb_umm_msg is the correct table name.

Nevertheless, the error remains.
And by the way I'm absolutely confused now. Why did it work even with the wrong specified table name in collection-mapping?

:veryconfused:


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