org.hibernate.Hibernate.isInitialized()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(303)

本文整理了Java中org.hibernate.Hibernate.isInitialized()方法的一些代码示例,展示了Hibernate.isInitialized()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Hibernate.isInitialized()方法的具体详情如下:
包路径:org.hibernate.Hibernate
类名称:Hibernate
方法名:isInitialized

Hibernate.isInitialized介绍

[英]Check if the proxy or persistent collection is initialized.
[中]检查代理或持久集合是否已初始化。

代码示例

代码示例来源:origin: hibernate/hibernate-orm

protected boolean reassociateIfUninitializedProxy(Object object, SessionImplementor source) {
  if ( !Hibernate.isInitialized(object) ) {
    throw new PersistentObjectException("uninitialized proxy passed to save()");
  }
  else {
    return false;
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
@SuppressWarnings({ "unchecked" })
public final String toLoggableString(Object value, SessionFactoryImplementor factory) {
  if ( value == LazyPropertyInitializer.UNFETCHED_PROPERTY || !Hibernate.isInitialized( value ) ) {
    return  "<uninitialized>";
  }
  return javaTypeDescriptor.extractLoggableRepresentation( (T) value );
}

代码示例来源:origin: dropwizard/dropwizard

/**
   * Force initialization of a proxy or persistent collection.
   * <p/>
   * Note: This only ensures initialization of a proxy object or collection;
   * it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.
   *
   * @param proxy a persistable object, proxy, persistent collection or {@code null}
   * @throws HibernateException if we can't initialize the proxy at this time, eg. the {@link Session} was closed
   */
  protected <T> T initialize(T proxy) throws HibernateException {
    if (!Hibernate.isInitialized(proxy)) {
      Hibernate.initialize(proxy);
    }
    return proxy;
  }
}

代码示例来源:origin: hibernate/hibernate-orm

public boolean isReachable(Object traversableObject,
    Path.Node traversableProperty,
    Class<?> rootBeanType,
    Path pathToTraversableObject,
    ElementType elementType) {
  //lazy, don't load
  return Hibernate.isInitialized( traversableObject )
      && Hibernate.isPropertyInitialized( traversableObject, traversableProperty.getName() );
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException {
  if ( !Hibernate.isInitialized( value ) ) {
    final HibernateProxy proxy = (HibernateProxy) value;
    final LazyInitializer li = proxy.getHibernateLazyInitializer();
    reassociateProxy( li, proxy );
    return true;
  }
  else {
    return false;
  }
}

代码示例来源:origin: hibernate/hibernate-orm

private boolean isFoundInParent(
    String property,
    Object childEntity,
    EntityPersister persister,
    CollectionPersister collectionPersister,
    Object potentialParent) {
  final Object collection = persister.getPropertyValue( potentialParent, property );
  return collection != null
      && Hibernate.isInitialized( collection )
      && collectionPersister.getCollectionType().contains( collection, childEntity, session );
}

代码示例来源:origin: hibernate/hibernate-orm

private Object getIndexInParent(
    String property,
    Object childEntity,
    EntityPersister persister,
    CollectionPersister collectionPersister,
    Object potentialParent){
  final Object collection = persister.getPropertyValue( potentialParent, property );
  if ( collection != null && Hibernate.isInitialized( collection ) ) {
    return collectionPersister.getCollectionType().indexOf( collection, childEntity );
  }
  else {
    return null;
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException {
  //TODO: terrible implementation!
  if ( value == null ) {
    return "null";
  }
  if ( value == LazyPropertyInitializer.UNFETCHED_PROPERTY || !Hibernate.isInitialized( value ) ) {
    return  "<uninitialized>";
  }
  Class valueClass = HibernateProxyHelper.getClassWithoutInitializingProxy( value );
  return factory.getTypeHelper().entity( valueClass ).toLoggableString( value, factory );
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testOneToOneLazyLoading() {
  doInHibernate( this::sessionFactory, s -> {
    PostDetails post = (PostDetails) s.createQuery("select a from PostDetails a").getResultList().get(0);
    assertFalse(isInitialized(post.post));
  } );
}

代码示例来源:origin: hibernate/hibernate-orm

public void check(Object results) {
    List resultList = ( List ) results;
    assertEquals( 2, resultList.size() );
    assertEquals( courseMeetingExpected1, resultList.get( 0 ) );
    assertEquals( courseMeetingExpected2, resultList.get( 1 ) );
    assertTrue( Hibernate.isInitialized( ((CourseMeeting) resultList.get( 0 )).getCourse() ) );
    assertTrue( Hibernate.isInitialized( ((CourseMeeting) resultList.get( 1 )).getCourse() ) );
    assertEquals( courseExpected, ((CourseMeeting) resultList.get( 0 )).getCourse() );
    assertEquals( courseExpected, ((CourseMeeting) resultList.get( 1 )).getCourse() );
  }
};

代码示例来源:origin: hibernate/hibernate-orm

public void check(Object results) {
    List resultList = ( List ) results;
    assertEquals( 2, resultList.size() );
    assertEquals( yogiExpected, resultList.get( 0 ) );
    assertEquals( shermanExpected, resultList.get( 1 ) );
    assertNotNull( ((Student) resultList.get( 0 )).getEnrolments() );
    assertFalse( Hibernate.isInitialized( ((Student) resultList.get( 0 )).getEnrolments() ) );
    assertNotNull( ( ( Student ) resultList.get( 1 ) ).getEnrolments() );
    assertFalse( Hibernate.isInitialized( ( ( Student ) resultList.get( 1 ) ).getEnrolments() ) );
  }
};

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testRefreshLockInitializedProxy() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  DataPoint dp = newPersistentDataPoint( s );
  dp = ( DataPoint ) s.load( DataPoint.class, new Long( dp.getId() ) );
  dp.getX();
  assertTrue( Hibernate.isInitialized( dp ) );
  s.refresh( dp, LockOptions.UPGRADE );
  assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) );
  s.delete( dp );
  t.commit();
  s.close();
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  public void testLockUninitializedProxy() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    DataPoint dp = newPersistentDataPoint( s );

    dp = ( DataPoint) s.load( DataPoint.class, new Long( dp.getId() ) );
    assertFalse( Hibernate.isInitialized( dp ) );
    s.buildLockRequest( LockOptions.UPGRADE ).lock( dp );
    assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) );

    s.delete( dp );
    t.commit();
    s.close();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
@FailureExpected( jiraKey = "HHH-1645", message = "Session.refresh with LockOptions does not work on uninitialized proxies" )
public void testRefreshLockUninitializedProxy() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  DataPoint dp = newPersistentDataPoint( s );
  dp = ( DataPoint ) s.load( DataPoint.class, new Long( dp.getId() ) );
  assertFalse( Hibernate.isInitialized( dp ) );
  s.refresh( dp, LockOptions.UPGRADE );
  assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) );
  s.delete( dp );
  t.commit();
  s.close();
}

代码示例来源:origin: hibernate/hibernate-orm

public void perform(TestData data) {
    Session session = openSession();
    session.beginTransaction();
    session.enableFetchProfile( "offering.details" );
    CourseOffering section = ( CourseOffering ) session.get( CourseOffering.class, data.getSectionId() );
    assertEquals( 3, sessionFactory().getStatistics().getEntityLoadCount() ); // section + (enrollments + course)
    assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
    assertTrue( Hibernate.isInitialized( section.getEnrollments() ) );
    session.getTransaction().commit();
    session.close();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

public void perform(TestData data) {
    Session session = openSession();
    session.beginTransaction();
    session.enableFetchProfile( "course.details" );
    Course course = ( Course ) session.get( Course.class, data.getCourseId() );
    assertEquals( 2, sessionFactory().getStatistics().getEntityLoadCount() ); // course + department
    assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
    assertTrue( Hibernate.isInitialized( course.getCode().getDepartment() ) );
    session.getTransaction().commit();
    session.close();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

private void checkProxyReadOnly(Session s, Object proxy, boolean expectedReadOnly) {
  assertTrue( proxy instanceof HibernateProxy );
  LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer();
  assertSame( s, li.getSession() );
  assertEquals( expectedReadOnly, s.isReadOnly( proxy ) );
  assertEquals( expectedReadOnly, li.isReadOnly() );
  assertEquals( Hibernate.isInitialized( proxy ), ! li.isUninitialized() );
  if ( Hibernate.isInitialized( proxy ) ) {
    assertEquals( expectedReadOnly, s.isReadOnly( li.getImplementation() ) );
  }
}

代码示例来源:origin: hibernate/hibernate-orm

private void checkReadOnly(Session s, Object proxy, boolean expectedReadOnly) {
    assertTrue( proxy instanceof HibernateProxy );
    LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer();
    assertSame( s, li.getSession() );
    assertEquals( expectedReadOnly, s.isReadOnly( proxy ) );
    assertEquals( expectedReadOnly, li.isReadOnly() );
    assertEquals( Hibernate.isInitialized( proxy ), ! li.isUninitialized() );
    if ( Hibernate.isInitialized( proxy ) ) {
      assertEquals( expectedReadOnly, s.isReadOnly( li.getImplementation() ) );
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

public void check(Object results) {
    assertTrue( results instanceof Course );
    assertEquals( courseExpected, results );
    assertTrue( Hibernate.isInitialized( courseExpected.getCourseMeetings() ) );
    assertEquals( courseExpected.getCourseMeetings(), courseExpected.getCourseMeetings() );
  }
};

代码示例来源:origin: hibernate/hibernate-orm

public void check(Object results) {
    assertTrue( results instanceof Student );
    assertEquals( shermanExpected, results );
    assertNotNull( ((Student) results).getEnrolments() );
    assertFalse( Hibernate.isInitialized( ((Student) results).getEnrolments() ) );
    assertNull( ((Student) results).getPreferredCourse() );
  }
};

相关文章