-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Bi-directional Mapping
PostPosted: Sat Sep 08, 2012 3:29 pm 
Newbie

Joined: Sat Sep 08, 2012 3:21 pm
Posts: 2
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);
      

   }


Top
 Profile  
 
 Post subject: Re: Bi-directional Mapping
PostPosted: Tue Sep 25, 2012 10:00 am 
Newbie

Joined: Sat Sep 08, 2012 3:21 pm
Posts: 2
bump? Anyone know?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.