我有一个“登录”域对象:
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
这是休眠映射文件的片段:
<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>
登录引用的 Address.hbm.xml 映射如下所示:
<many-to-one name="login" class="com.domain.Login"
fetch="join" lazy="false">
<column name="LoginID" />
</many-to-one>
这是我的春季测试:
@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());
}
}
这是输出:
size Before save...14
size After save...14
问题/问题我希望在添加新地址后看到登录对象中设置的地址增加一个,因为我没有延迟加载。为什么我的集合的大小没有增长?是因为我在同一个事务中运行查询吗?
谢谢。