-->
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: hibernate hydrated objects' properties are null except id
PostPosted: Wed Jan 02, 2008 5:48 pm 
Newbie

Joined: Wed Jan 02, 2008 5:04 pm
Posts: 2
Hello,

hibernate-3.2.5
hibernate-annotations-3.3.0
hibernate-commons-annotations-3.3.0
mysql 5.0
tomcat 6.0.14


I have the following model classes, only relevant portions mentioned:

Code:
@Entity
@Table (name="insertion")
public class Insertion extends BaseObject implements AuditableModel {
...

@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER, optional=false)
   @JoinColumn ( name="unit_id", nullable=false)
   public Unit getUnit() {
      return unit;
   }

...
}

@Entity
@Table (name="unit")
public class Unit extends BaseObject implements AuditableModel {
...

@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER, optional=false)
   @JoinColumn ( name="menu_id", nullable=false)
   public Menu getMenu() {
      return menu;
   }
...
}

@Entity
@Table (name="menu")
public class Menu extends BaseObject implements AuditableModel {
..

@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
   @JoinColumn ( name="background_content_id")
   public Content getBackground() {
      return background;
   }

...
}

@Entity
@Table (name="content")
public class Content extends BaseObject implements AuditableModel {

...

}



Code:
BaseObject

is
Code:
org.appfuse.model.BaseObject


So, an Insertion has a Unit, which has a Menu which has a background (Content).

So, when an insertion is loaded, it should load its unit, and its menu and the background content.

The issue is, if I access the content in Menu.setBackground() class, the Content object's Id is set, but rest of the member attributes are null.

But if I access the Menu object itself, from Unit class, as in Unit.setMenu(), the Menu class has its Id set, and other attributes also have values. (like Customer, createdDate, etc. etc.)

I did a sql output of the queries that hibernate generates, and one query which is invoked on getUnit() of Insertion is as below:

Code:
select unit0_.id as id11_10_, unit0_.config_id as config4_11_10_, unit0_.created_date as created2_11_10_, unit0_.menu_id as menu5_11_10_, unit0_.updated_date as updated3_11_10_,
    config1_.id as id4_0_,     config1_.base_offset_x as base2_4_0_, config1_.base_offset_y as base3_4_0_, config1_.click_url_prefix as click4_4_0_, config1_.click_url_suffix as click5_4_0_,
        config1_.created_date as created6_4_0_, config1_.customer_id as customer12_4_0_, config1_.offset_x as offset7_4_0_, config1_.offset_y as offset8_4_0_, config1_.open as open4_0_, config1_.open_left_icon_content_id as open13_4_0_,
        config1_.open_right_icon_content_id as open14_4_0_, config1_.position as position4_0_, config1_.updated_date as updated11_4_0_,
    customer2_.id as id6_1_, customer2_.created_date as created2_6_1_, customer2_.name as name6_1_, customer2_.short_name as short4_6_1_, customer2_.updated_date as updated5_6_1_,
    content3_.id as id5_2_, content3_.click_url as click2_5_2_, content3_.click_url_nw as click3_5_2_, content3_.content as content5_2_, content3_.created_date as created5_5_2_, content3_.label as label5_2_,
        content3_.offset_x as offset7_5_2_, content3_.offset_y as offset8_5_2_, content3_.parent_menu_id as parent12_5_2_, content3_.sequence_num as sequence9_5_2_, content3_.sub_menu_id as sub13_5_2_,
        content3_.type as type5_2_, content3_.updated_date as updated11_5_2_,
    content4_.id as id5_3_, content4_.click_url as click2_5_3_, content4_.click_url_nw as click3_5_3_, content4_.content as content5_3_, content4_.created_date as created5_5_3_, content4_.label as label5_3_,
        content4_.offset_x as offset7_5_3_, content4_.offset_y as offset8_5_3_, content4_.parent_menu_id as parent12_5_3_, content4_.sequence_num as sequence9_5_3_, content4_.sub_menu_id as sub13_5_3_,
        content4_.type as type5_3_, content4_.updated_date as updated11_5_3_,
    menu5_.id as id9_4_, menu5_.background_content_id as background8_9_4_, menu5_.created_date as created2_9_4_, menu5_.customer_id as customer7_9_4_, menu5_.description as descript3_9_4_,
        menu5_.foreground_content_id as foreground6_9_4_, menu5_.node_type as node4_9_4_, menu5_.updated_date as updated5_9_4_,
    content6_.id as id5_5_, content6_.click_url as click2_5_5_, content6_.click_url_nw as click3_5_5_, content6_.content as content5_5_, content6_.created_date as created5_5_5_,
        content6_.label as label5_5_, content6_.offset_x as offset7_5_5_, content6_.offset_y as offset8_5_5_, content6_.parent_menu_id as parent12_5_5_, content6_.sequence_num as sequence9_5_5_,
        content6_.sub_menu_id as sub13_5_5_, content6_.type as type5_5_, content6_.updated_date as updated11_5_5_,
    customer7_.id as id6_6_, customer7_.created_date as created2_6_6_, customer7_.name as name6_6_, customer7_.short_name as short4_6_6_, customer7_.updated_date as updated5_6_6_,
    content8_.id as id5_7_, content8_.click_url as click2_5_7_, content8_.click_url_nw as click3_5_7_, content8_.content as content5_7_, content8_.created_date as created5_5_7_, content8_.label as label5_7_,
        content8_.offset_x as offset7_5_7_, content8_.offset_y as offset8_5_7_, content8_.parent_menu_id as parent12_5_7_, content8_.sequence_num as sequence9_5_7_, content8_.sub_menu_id as sub13_5_7_,
        content8_.type as type5_7_, content8_.updated_date as updated11_5_7_,
    styles9_.unit_id as unit1_12_, style10_.id as style2_12_, style10_.id as id10_8_,
    style10_.created_date as created2_10_8_, style10_.customer_id as customer6_10_8_, style10_.renderer as renderer10_8_, style10_.updated_date as updated4_10_8_, style10_.url as url10_8_,
    customer11_.id as id6_9_, customer11_.created_date as created2_6_9_, customer11_.name as name6_9_, customer11_.short_name as short4_6_9_, customer11_.updated_date as updated5_6_9_
from unit unit0_ inner join config config1_ on unit0_.config_id=config1_.id
    left outer join customer customer2_ on config1_.customer_id=customer2_.id
    left outer join content content3_ on config1_.open_left_icon_content_id=content3_.id
    left outer join content content4_ on config1_.open_right_icon_content_id=content4_.id
    inner join menu menu5_ on unit0_.menu_id=menu5_.id
    left outer join content content6_ on menu5_.background_content_id=content6_.id
    left outer join customer customer7_ on menu5_.customer_id=customer7_.id
    left outer join content content8_ on menu5_.foreground_content_id=content8_.id
    left outer join unit_style styles9_ on unit0_.id=styles9_.unit_id
    left outer join style style10_ on styles9_.style_id=style10_.id
    left outer join customer customer11_ on style10_.customer_id=customer11_.id
where unit0_.id='someId'


and when I run this query, I do see that content6_ has all its values present (
Code:
content6_
is joined with
Code:
menu5_.background_content_id=content6_.id
)

So, this shows that the query is returning proper values for the background (Content) object, but the object supplied to
Code:
Menu.setBackground()
only has its id set.

But when I access
Code:
background.getType()
in
Code:
Menu.setBackground(Content background)
, I get the value as null.

I did find some threads on this forum addressing 'similar' issues, but I did not fully get a satisfactory answer, why this is happening (proxy object?) and how to resolve this (??).

Any direction in this regard will be helpful.

Thanks
MRather


Top
 Profile  
 
 Post subject: hint
PostPosted: Fri Jan 04, 2008 2:19 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Mrather,

I don't quite understand your model, but one thing comes to mind: you evidently intend that the relationship between Menu and Content to be bi-directional, however, I don't see a "mappedBy" annotation parameter anywhere. Without this, Hibernate doesn't know which side of the relationship is in charge of writing the SQL.

Look at your resulting mapping files. Do you see any "inverse"?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 10, 2008 1:40 pm 
Newbie

Joined: Wed Jan 02, 2008 5:04 pm
Posts: 2
Gonao_diaz,

I apologize for a late reply.

In this instance there is no bi-directional refrence between Menu and Content.
There is a
Code:
@ManyToOne
relationship between Menu and Content, i.e. A Content can be related to more than one menu, but a menu can have only one Background (Content) using
Code:
@JoinColumn ( name="background_content_id")


The issue is a bit subtle.
If I did
Code:
Insertion insertion = insertionManager.getInsertionById('ABC')

and if I used the following method to access the insertion's menu's content properties, I would 'get' the values.
Code:
System.out.println(insertion.getUnit().getMenu().getBackground().getABC();

I would get the value properly.

But, if was to put this SOP in Menu's setBackground() method: (note, background is a Content object)
Code:
setBackground(Content background){
    this.background = background;
    System.out.println(background.getABC());
    System.out.println(background.getId());
}


I would find that getABC() would return null, but getId() will return a proper value.

NOTE: if the insertion is fully loaded, and accessed 'from a client' after the load is complete, getABC() as well as getId() return proper values.

I think it has to do with how hibernate loads objects into child objects. Since our original load is insertion, it loads all child objects as 'proxies', and only their id's are loaded. and their rest of properties get loaded when a client accesses them.

[NOTE: I think I may be wrong here, but this seems to be most logical explanation of behavior].

Even if such was the case, then why would the following on Unit work?

Code:
setMenu(Menu menu){
    this.menu = menu;
    System.out.println(menu.getXYZ());
    System.out.println(menu.getId());
}



Here getXYZ() as well as getId() return values.

Please advise.

Thanks
MRather


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 9:03 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Whatever the way Hibernate loads children and their properties, by the time you do a reading on that property, the value, if exists, has to be retrieved. Doing a sysout certainly involved a reading.

So your problem must lie in the fact that those two scenarios are (the one returning you a null background and the one not returning but the Id) are not as equal as you think they are.

Probably more than one line is returned for the same Menu, and some of the lines are null on the right side of the join, or some other "object tree" problem along that line.

_________________
Gonzalo Díaz


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.