Hi,
I am using Hibernate version 3.5.6-Final (I know that is is too old already but this is a legacy project).
and have an issue as the subject described, the lazy option for the ManyToOne relationship does not work at all.
E.g: I have two entities WebUser and Contact with the definition as following:
Code:
@Entity
@Table(name = "web_user")
public class WebUser implements Serializable
{
....
//bi-directional many-to-one association to Contact
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "contact_id")
@LazyToOne(LazyToOneOption.PROXY)
@Fetch(FetchMode.SELECT)
private Contact contact;
}
and
@Entity
@Table(name = "contact")
public class Contact implements Serializable
{
....
// no definition of the WebUser here since I don't really need to get WebUser thru Contact
}
Then whenever I run a DAO service e.g WebUser.findByPasswordAndEmailCaseInsensitive(1L); it always call to another query for the related Contact
Code:
2015-01-07 17:12:02 ICT LOG: execute <unnamed>: select webuser0_.web_id as web1_92_, webuser0_.date_created as date2_92_, webuser0_.date_updated as date3_92_, webuser0_.version as version92_, webuser0_.main_address_id as main13_92_, webuser0_.main_branch_id as main14_92_, webuser0_.business_entity_id as business15_92_, webuser0_.contact_id as contact16_92_, webuser0_.enterprise_id as enterprise17_92_, webuser0_.main_department_id as main18_92_, webuser0_.description as descript5_92_, webuser0_.email as email92_, webuser0_.external_key as external7_92_, webuser0_.password as password92_, webuser0_.photo_file_id as photo9_92_, webuser0_.reset_token as reset10_92_, webuser0_.status as status92_, webuser0_.web_user_number as web12_92_ from web_user webuser0_ where lower(webuser0_.email)=lower($1) and webuser0_.password=$2
2015-01-07 17:12:02 ICT DETAIL: parameters: $1 = 'thanhdc@qsoftvietnam.com', $2 = '25d55ad283aa400af464c76d713c07ad'
2015-01-07 17:12:02 ICT LOG: execute <unnamed>: select contact0_.web_id as web1_18_0_, contact0_.date_created as date2_18_0_, contact0_.date_updated as date3_18_0_, contact0_.version as version18_0_, contact0_.address_id as address15_18_0_, contact0_.birthday as birthday18_0_, contact0_.enterprise_id as enterprise16_18_0_, contact0_.external_key as external6_18_0_, contact0_.first_name as first7_18_0_, contact0_.gender as gender18_0_, contact0_.language_id as language17_18_0_, contact0_.last_name as last9_18_0_, contact0_.middle_name as middle10_18_0_, contact0_.nickname as nickname18_0_, contact0_.notes as notes18_0_, contact0_.suffix as suffix18_0_, contact0_.title as title18_0_ from contact contact0_ where contact0_.web_id=$1
2015-01-07 17:12:02 ICT DETAIL: parameters: $1 = '149832473'
It seems that the lazy option does not work, even when I followed the specification written here https://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-singleassoc
Did I wrong somewhere or it is a bug of Hibernate? Any comments/suggestions is appreciated.
Thanks.