Pessoal,
Não estou conseguindo fazer o lazy do many to one funcionar. Mesmo como lazy (estou usando anotações do JPA e Hibernate 3.2 como provider) ele ainda carrega o cidadão, sem fazer o proxy.
Após alguma pesquisa encontrei a razão aqui numa pergunta sobre o NHibernate:
http://stackoverflow.com/questions/604373/lazy-loading-not-working-for-many-to-one-relationship-when-mapping-to-a-non-key-f
The issue is caused by the property-ref attribute. Lazy loading only works when the many-to-one reference is using the other object's primary key since NHibernate assumes there's a foreign key constraint enforcing the validity of such a value. With a non-primary key (indicated by the property-ref), NHibernate does not make this assumption and thus does not assume the related object must exist. Since it does not want to create a proxy for an object that does not exist (i.e. should be null instead of a proxy), it eagerly fetches the remote object. This same issue exists when not-found="ignore" is specified since this indicates that the foreign key relationship is not enforced and may result in a null reference.
A diferença, é que o cara ta usando XML, e eu anotação pura, pelo que pesquisei o property-ref é o @JoinColumn, mas eu preciso ter ele para mapear o nome da coluna e o nome da coluna referenciada, não tem outro jeito de não usar ele.
O que faço? Ou não tem cura?