[jboss-user] [EJB 3.0] - Re: onetomany relationship question

lpiccoli do-not-reply at jboss.com
Wed Jul 8 00:07:15 EDT 2009


ok it seems that my test cases were erroneous.

i finally got the onetomany to fetch the 'many' by refreshing the entity. This then invoked the  following query


  | hibernate: select childbean0_.ID as ID3_, childbean0_.parent_ID as parent2_3_ from ChildBean childbean0_ where childbean0_.parent_ID=?
  | 


my test code also shows that the relationship only needs to be set on the many side.


  | 
  | @Test
  | 	public void one2manyTestWithSettingManySide() {
  | 		
  | 		ParentBean parent = new ParentBean();
  | 		entityManager.persist(parent);
  | 		long id = parent.getId();
  | 		
  | 		ChildBean child1 = new ChildBean();
  | 		child1.setParent(parent);
  | 		entityManager.persist(child1);
  | 				
  | 		ChildBean child2 = new ChildBean();
  | 		child2.setParent(parent);
  | 		entityManager.persist(child2);
  | 					
  | 		Query query = entityManager.createQuery("from ChildBean child where parent.id=:id");
  | 		query.setParameter("id", id);
  | 		List resultList = query.getResultList();
  | 		assertEquals( 2, resultList.size() );
  | 		
  | 		//check refresh
  | 		entityManager.refresh(parent);
  | 		assertEquals( 2, parent.getChilds().size() );
  | 		
  | 		ParentBean parentBean = entityManager.find(ParentBean.class, parent.getId());
  | 		
  | 		assertEquals( 2, parentBean.getChilds().size() );
  | 		
  | 	}
  | 
  | 

thanks for your help wolfgang.

-lp

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4242316#4242316

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4242316



More information about the jboss-user mailing list