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.  [ 7 posts ] 
Author Message
 Post subject: Problems with many-to-one
PostPosted: Wed Sep 20, 2006 1:52 pm 
Beginner
Beginner

Joined: Mon Mar 06, 2006 2:19 pm
Posts: 42
Location: Belo Horizonte, Brazil
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 1.0.2 alpha

Every many-to-one association in my project seems to have the same problem. Lets suppose two objects, A and B.

A many-to-one B

If I try to use A.B, and B is not null, the attributes of B are not setted but the properties are. Something like, (attribute)B.id = 0 and (Property)B.Id = 34.

But if I use B before accessing it through A, B.id = 34 and B.Id = 34.

Anyone knows what's happening? It only happens in my many-to-one associations. Everything else seems fine.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 2:33 am 
Newbie

Joined: Wed Sep 20, 2006 8:47 pm
Posts: 5
you can set b' lazy false.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 2:06 pm 
Beginner
Beginner

Joined: Mon Mar 06, 2006 2:19 pm
Posts: 42
Location: Belo Horizonte, Brazil
You're right. I found that outer-join="true" in the relationship works too. Do you know if there is any difference between them? I think that lazy="false" is a good option if we're sure that we want it working in all relations, so we don't have to specify the outer-join in all relations. Am I right?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 5:27 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
by default in NH 1.0.2 the relationships are set lazy="false". instead of changing this behavior, i would encourage you to read up on sessions and lazy loading (Session.Lock() is one place to look) to truly understand where you want the collections to be loaded lazily and where you don't. otherwise you are condeming your app to load an entire object graph every time when it might only want the parent object. plus you won't gain the benefit of second-level caching if you decide to use that someday.

-d


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 6:18 pm 
Beginner
Beginner

Joined: Mon Mar 06, 2006 2:19 pm
Posts: 42
Location: Belo Horizonte, Brazil
But I'm not talking about collections. My problem is exactly at the other side of the relationship. The collections are ok with lazy enabled. I don't know if I am clear enough in the first message. Please, let me know.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 6:32 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Quote:
I don't know if I am clear enough in the first message. Please, let me know.

No that was my fault in miscommunicating.

Re-reading your post, I think what you are calling an "attribute", I might call a "member variable":
Code:
class A {
   private int id;

   public A() {
      this.id = -1;
   }

   public virtual int ID {
      get { return this.id; }
   }
}


Relationships (including 1-M and M-1) are lazily loaded by default so calling A.B.ID on the associated object forces NH to load the lazily loaded association of A to B. Calling A.B.id first of all is bad programming style, second would not have access to the reflected property methods that would trigger the load, I think.

I may have misunderstood your use of the word attribute and aplogize if that is so, but if I haven't and you are declaring your member variables as anything other than private (or protected in certain inheritance situations), then you should consider changing that.
-d


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 10:00 am 
Beginner
Beginner

Joined: Mon Mar 06, 2006 2:19 pm
Posts: 42
Location: Belo Horizonte, Brazil
You got it now devonl. Let's use "member variable" from here. :) My english is not so good.

You're right. My member variables are all private, but I need to access them inside their classes. Let's suppose:

Code:
class B
{
   private double price;

   public double Price
   {
      get {return price;}
      set {price=value;}
   }

   //Here is my problem
   public double TotalPrice
   {
      //Even setting a value to price before, price = 0 and dosen't work
      get{return price * units;}
      
      
      //Price has the correct value and works
      get{return Price * units;}
   }

}


The only member variables with value were the enums.

and...

//Suppose Get is a function and 5 is the id
//Here, price in b is ok , b.Price is ok and b.TotalPrice is ok
B b = Get(B, 5);

but...

A a = Get(A, 6);
//Now internally b.price = 0 but b.Price is ok

//so
double price = a.b.Price; //ok

double totalPrice = a.b.TotalPrice // not ok because it used price internally


This issue was solved as described above, but is this correct?

Lazy and proxies are the "problem".

Thank you.


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