Hi all,
Can someone explain why the following test fails? I am using HSQL in memory db and the fact that I am re-querying the repository leads me to believe that this should work.
Please tell me what I got wrong.
Thanks,
Bashusr
Code:
@Entity
@Table(name="site")
public class Site extends PersistentObject {
@javax.persistence.Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="site_id")
private Integer id;
@OneToMany(mappedBy = "site", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
@Cascade( org.hibernate.annotations.CascadeType.SAVE_UPDATE )
//@Fetch(FetchMode.JOIN)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Asset> assets;
.....
}
===============================================================
@Entity
@Table(name = "asset")
@Inheritance(strategy=InheritanceType.JOINED)
public class Asset extends PersistentObject {
@javax.persistence.Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="asset_id", nullable=false)
private Integer id;
@ManyToOne(fetch=FetchType.EAGER, cascade= { CascadeType.ALL})
@JoinColumn(name="site_id")
private Site site;
.....
}
============================ My Test =======================================
/**
* Note behavior, when a Site saves the relationship, asset.getSite() returns null and visa versa
* an Asset is set with a Site, site.getAssets() returns an empty list.
*/
@Test
public void testSiteRelationshipCreate() {
Site gsTest = new Site();
Site gsTest2 = new Site();
gsTest.setStationName("Test Site");
gsTest2.setStationName("Test Site2");
Asset asset1 = new Asset();
Asset asset2 = new Asset();
asset2.setSite(gsTest2);
asset1.setDescription("A new test Asset");
gsTest.addAsset(asset1);
gsRepository.add(gsTest);
assetRepository.add(asset2);
sessionFactory.getCurrentSession().flush();
// basically just calls Session.get()
Asset queriedAsset=assetRepository.selectById(asset1.getId());
Site queriedSite=gsRepository.selectById(gsTest2.getId());
org.testng.Assert.assertEquals(queriedAsset,asset1);
//List<Asset> allAssets = assetRepository.all();
//List<Site> allGSs = gsRepository.all();
//log.debug("just a debug point");
// Both Fails
org.testng.Assert.assertEquals(queriedAsset.getSite(),gsTest);
org.testng.Assert.assertEquals(queriedSite.getAssets().get(0),asset2);
}