use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class SerializableProxy method readResolve.
private Object readResolve() {
HibernateProxy proxy = ByteBuddyProxyFactory.deserializeProxy(this);
setReadOnlyBeforeAttachedToSession((ByteBuddyInterceptor) proxy.getHibernateLazyInitializer());
return proxy;
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class JavassistProxyFactory method deserializeProxy.
public static HibernateProxy deserializeProxy(SerializableProxy serializableProxy) {
final JavassistLazyInitializer initializer = new JavassistLazyInitializer(serializableProxy.getEntityName(), serializableProxy.getPersistentClass(), serializableProxy.getInterfaces(), serializableProxy.getId(), resolveIdGetterMethod(serializableProxy), resolveIdSetterMethod(serializableProxy), serializableProxy.getComponentIdType(), null, ReflectHelper.overridesEquals(serializableProxy.getPersistentClass()));
final javassist.util.proxy.ProxyFactory factory = buildJavassistProxyFactory(serializableProxy.getPersistentClass(), serializableProxy.getInterfaces());
// note: interface is assumed to already contain HibernateProxy.class
try {
final Class proxyClass = factory.createClass();
final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance();
((Proxy) proxy).setHandler(initializer);
initializer.constructed();
return proxy;
} catch (Throwable t) {
final String message = LOG.bytecodeEnhancementFailed(serializableProxy.getEntityName());
LOG.error(message, t);
throw new HibernateException(message, t);
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class JavassistProxyFactory method getProxy.
@Override
public HibernateProxy getProxy(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
final JavassistLazyInitializer initializer = new JavassistLazyInitializer(entityName, persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session, overridesEquals);
try {
final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance();
((Proxy) proxy).setHandler(initializer);
initializer.constructed();
return proxy;
} catch (Throwable t) {
LOG.error(LOG.bytecodeEnhancementFailed(entityName), t);
throw new HibernateException(LOG.bytecodeEnhancementFailed(entityName), t);
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class GetIdentifierTest method testProxyObject.
@Test
@TestForIssue(jiraKey = "HHH-7561")
public void testProxyObject() {
EntityManager em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
try {
Book book = new Book();
em.persist(book);
em.flush();
// Clear persistence context to receive proxy object below.
em.clear();
Book proxy = em.getReference(Book.class, book.getId());
assertTrue(proxy instanceof HibernateProxy);
assertEquals(book.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(proxy));
} finally {
em.getTransaction().rollback();
em.close();
}
em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
try {
Author author = new Author();
Article article = new Article(author);
em.persist(author);
em.persist(article);
em.flush();
// Clear persistence context to receive proxy relation below.
em.clear();
article = em.find(Article.class, article.getId());
assertTrue(article.getAuthor() instanceof HibernateProxy);
assertEquals(author.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(article.getAuthor()));
} finally {
em.getTransaction().rollback();
em.close();
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class AbstractQueryCacheResultTransformerTest method multiPropProjectionNoTransformerDynNonLazy.
private void multiPropProjectionNoTransformerDynNonLazy(CacheMode sessionCacheMode, boolean isCacheableQuery) {
Session s = openSession();
s.setCacheMode(sessionCacheMode);
Transaction t = s.beginTransaction();
List resultList = s.createCriteria(Enrolment.class).setCacheable(isCacheableQuery).setFetchMode("student", FetchMode.JOIN).setProjection(Projections.projectionList().add(Property.forName("student"), "student").add(Property.forName("semester"), "semester").add(Property.forName("year"), "year").add(Property.forName("course"), "course")).addOrder(Order.asc("studentNumber")).list();
t.commit();
s.close();
assertEquals(2, resultList.size());
Object[] yogiObjects = (Object[]) resultList.get(0);
Object[] shermanObjects = (Object[]) resultList.get(1);
assertEquals(4, yogiObjects.length);
assertTrue(yogiObjects[0] instanceof Student);
assertTrue(Hibernate.isInitialized(yogiObjects[0]));
assertEquals(yogiEnrolmentExpected.getSemester(), ((Short) yogiObjects[1]).shortValue());
assertEquals(yogiEnrolmentExpected.getYear(), ((Short) yogiObjects[2]).shortValue());
assertEquals(courseExpected, yogiObjects[3]);
assertTrue(shermanObjects[0] instanceof Student);
assertTrue(Hibernate.isInitialized(shermanObjects[0]));
assertEquals(shermanEnrolmentExpected.getSemester(), ((Short) shermanObjects[1]).shortValue());
assertEquals(shermanEnrolmentExpected.getYear(), ((Short) shermanObjects[2]).shortValue());
assertTrue(!(shermanObjects[3] instanceof HibernateProxy));
assertTrue(shermanObjects[3] instanceof Course);
assertEquals(courseExpected, shermanObjects[3]);
}
Aggregations