-->
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.  [ 1 post ] 
Author Message
 Post subject: persist and detach for lazy collections
PostPosted: Fri Dec 10, 2010 12:31 pm 
Newbie

Joined: Fri Dec 10, 2010 12:07 pm
Posts: 1
I have a question concerning the lazy fetch function in Hibernate JPA. My test structure is quite simple: a company object which has products. Those products shall be fetched lazily.

Now what happens is that after a persist and an immediate find on the EntityManager a company object is returned which does not have a products proxy (products is simply null). Only after I detach the company object and then read it via find does it contain a proxied collection. I find it strange that the EntityManager returns an object which does not have proxied property objects where laziness is required.

The second test case is similar but here I use merge to persist the object. The javadoc for merge states that merge returns a “managed instance” so here I would expect the object to have the proxies set. Nonetheless the test fails because products is null.
I’m using hibernate 3.6.0-final for this test. Is my understanding wrong or is this a problem with hibernate?

Code:
@Entity
public class Company {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int companyId;

   private String name;

   @OneToMany(fetch = FetchType.LAZY, mappedBy = "company")
   private List<Product> produts;

}

@Entity
public class Product {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int productId;

   @ManyToOne
   private Company company;

}

public class Test2Jpa {

   @PersistenceContext
   private EntityManager em;

   @Test
   public void testProductsPersistNewCompany() throws Exception {

      Company company = new Company();
      company.setName("test");
      em.persist(company);
      em.detach(company); // doesn't work without this

      Company companyFromDb = em.find(Company.class, company.getCompanyId());

      assertNotNull(companyFromDb.getProduts());
   }

   @Test
   public void testProductsMergeNewCompany() throws Exception {

      Company company = new Company();
      company.setName("test");
      Company newCompany = em.merge(company);
      assertNotNull(newCompany.getProduts()); // fails
   }
}


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

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.