Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peas/523717 to your computer and use it in GitHub Desktop.
Save peas/523717 to your computer and use it in GitHub Desktop.
package br.com.caelum;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Persistence;
import org.hibernate.Hibernate;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@Entity
class Cat {
@Id
@GeneratedValue
int id;
@ManyToOne
Shop shop;
}
@Entity
class Shop {
@Id
@GeneratedValue
int id;
}
public class GetReferenceProxyBeingLoadedWhenNotNeeded {
private EntityManager em;
@Before
public void setUp() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("jpa2");
this.em = factory.createEntityManager();
this.em.getTransaction().begin();
this.em.persist(new Shop()); // shop of id 1
this.em.getTransaction().commit();
this.em = factory.createEntityManager();
}
@Test
public void shouldNotInitilizeGetReferenceProxy() {
Shop shop = this.em.getReference(Shop.class, 1);
Cat c = new Cat();
c.shop = shop;
this.em.getTransaction().begin();
this.em.persist(c);
this.em.getTransaction().commit();
Assert.assertFalse(Hibernate.isInitialized(shop));
}
}
@peas
Copy link
Author

peas commented Aug 13, 2010

getReference now working fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment