I have a ‘Login’ domain object:
public class Login extends AuditEntity {
private static final long serialVersionUID = -309839789761736747L;
private String userName;
private String password;
private Timestamp lastLogin;
private Set customers = new HashSet(0);
private Set addresses = new HashSet(0);
public Set getCustomers() {
return this.customers;
}
public void setCustomers(Set customers) {
this.customers = customers;
}
public Set getAddresses() {
return this.addresses;
}
public void setAddresses(Set addresses) {
this.addresses = addresses;
}
//Other setter, getters
Here’s the snippet from hibernate mapping file :
<set name="customers" fetch="select" lazy="false">
<key>
<column name="LoginID" />
</key>
<one-to-many class="com.domain.Customer" />
</set>
<set name="addresses" fetch="join" lazy="false">
<key>
<column name="LoginID" />
</key>
<one-to-many class="com.domain.Address" />
</set>
The Address.hbm.xml mapping for the login reference looks like this :
<many-to-one name="login" class="com.domain.Login"
fetch="join" lazy="false">
<column name="LoginID" />
</many-to-one>
Here’s my spring test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class LoginDaoTest {
@Autowired
private BaseDao<Login, Integer> loginDao;
@Autowired
private BaseDao<Address, Integer> addressDao;
@Test
public void testLoginDao() {
Login login = loginDao.load(1);
Set<Address> addresses = login.getAddresses();
System.out.println("size Before save..." + addresses.size());
Address address = new Address();
address.setCity("Kew gardens");
address.setCreatedBy("user");
address.setCreatedDate(new Timestamp(Calendar.getInstance().getTime().getTime()));
address.setHouseNumber("1-2-3");
address.setLogin(login);
address.setLocality("Queens blvd");
address.setState("NY");
address.setStreetName("Some Street");
address.setZipCode("11415");
addressDao.save(address);
System.out.println("size After save..." + addresses.size());
}
}
Here’s the output:
size Before save...14
size After save...14
Question/problem I was expecting to see the address set in the login object grow by one after i add a new address since I am not loading lazyly. Why isn’t the size of my set growing? Is it because I am running the queries in the same transaction?
Thanks.
You are responsible for maintaining the in-memory state of your objects. Hibernate will not automagically stick that address into the list for you. To write strictly correct code you should add the new address to the list when you create it, so that your in memory state matches what you intend to have persisted.
You can then use Transitive Persistence to have the new address created, no need to call ‘save’ on the address at all.
Conceptually it works the other way around from what you are expecting. The purpose of the framework is not to magically ‘bind’ the database onto your objects. It’s to let you use ordinary objects in the same way you would if there were no database (e.g., putting things into the lists they go in them), and have the persisting of your object states happen magically.