Created
August 13, 2010 23:33
-
-
Save peas/523717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
getReference now working fine!