Hi!
I'm running into a problem that I honestly don't know how to work around. I would really apreciate any hint you guys can give me.
I have to entities. Town and Country.
Country is straight foward, but Town has a many-to-one relation to country. (a town belongs to a country, and a country can be associated to many towns).
I've decided to make the relation lazy, because most of the times I don't need to countries when I query the towns. But from time to time I will require them.
This is the model simplified:
Code:
@Entity
@Table(name="COUNTRIES")
public class Country {
@Id
private Long id;
@Column(nullable=false)
private String name;
}
Code:
@Entity
@Table(name="TOWNS")
public class Town{
@Id
private Long id;
@Column(nullable=false)
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, name = "country_id")
private Country country;
}
Now, when I want to retrieve towns with countries I do something like this:
Code:
@Transactional(propagation = Propagation.SUPPORTS, readOnly = false)
public Town getTownWithCountry(final Long id){
Town town = townDAO.find(id);
org.hibernate.Hibernate.initialize(town.getCountry());
return town;
}
Aja, that code runs with no errors. But after, if I try to retrieve any field from the country (like country.getid()) everything is null !!! I mean, the fields are null, but the country itself is not.
I debugged it, and this is what I found:
Code:
country Country_$$_javassist_10 (id=100)
handler JavassistLazyInitializer (id=111) -- this is an object
id null
name null
Uhmm... that's a proxy I guess, but not very useful to me.
What am I doing wrong? Does it have to do with the fact that the transaction is in SUPPORT?
I would really appreciate any help here. I can't find documentation about this.
Thanks!