org.hibernate.Hibernate类的使用及代码示例

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

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

Hibernate介绍

[英]* Provides access to the full range of Hibernate built-in types. Type instances may be used to bind values to query parameters.

  • A factory for new Blobs and Clobs.
  • Defines static methods for manipulation of proxies.
    [中]*提供对全部Hibernate内置类型的访问。类型实例可用于将值绑定到查询参数。
    *制造新的污点和污点的工厂。
    *定义用于操纵代理的静态方法。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public void initialize(Object proxy) throws DataAccessException {
  try {
    Hibernate.initialize(proxy);
  }
  catch (HibernateException ex) {
    throw SessionFactoryUtils.convertHibernateAccessException(ex);
  }
}

代码示例来源: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: shopizer-ecommerce/shopizer

@Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("entity.");
    builder.append(Hibernate.getClass(this).getSimpleName());
    builder.append("<");
    builder.append(getId());
    builder.append("-");
    builder.append(super.toString());
    builder.append(">");
    
    return builder.toString();
  }
}

代码示例来源: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

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

s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) );
assertTrue( dp instanceof HibernateProxy );
checkReadOnly( s, dp, false );
s.setReadOnly( dp, true );
checkReadOnly( s, dp,true );
assertFalse( Hibernate.isInitialized( dp ));
Hibernate.initialize( dp );
assertTrue( Hibernate.isInitialized( dp ));
checkReadOnly( s, dp,true );
s.setReadOnly( dp, false );
checkReadOnly( s, dp, false );
dp.setDescription( "changed" );
assertTrue( Hibernate.isInitialized( dp ) );
assertEquals( "changed", dp.getDescription() );
s.flush();

代码示例来源: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

@Test
@TestForIssue( jiraKey = "HHH-9637")
public void testExplicitJoinAfterFetchJoins() {
  Session s = openSession();
  s.getTransaction().begin();
  Entity1 e1Queryied =
      (Entity1) s.createQuery(
          "select e1 from Entity1 e1 inner join fetch e1.entity2 e2 inner join fetch e2.entity3 inner join e1.entity2 e1Restrict where e1Restrict.value = 'entity2'" )
          .uniqueResult();
  assertEquals( "entity1", e1Queryied.getValue() );
  assertTrue( Hibernate.isInitialized( e1Queryied.getEntity2() ) );
  assertTrue( Hibernate.isInitialized( e1Queryied.getEntity2().getEntity3() ) );
  s.getTransaction().commit();
  s.close();
}

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

@Test
public void testInitializedProxySerializationAfterSessionClosed() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  DataPoint dp = new DataPoint();
  dp.setDescription("a data point");
  dp.setX( new BigDecimal(1.0) );
  dp.setY( new BigDecimal(2.0) );
  s.persist(dp);
  s.flush();
  s.clear();
  dp = (DataPoint) s.load( DataPoint.class, new Long( dp.getId() ) );
  assertFalse( Hibernate.isInitialized(dp) );
  Hibernate.initialize( dp );
  assertTrue( Hibernate.isInitialized(dp) );
  s.close();
  SerializationHelper.clone( dp );
  s = openSession();
  t = s.beginTransaction();
  s.delete( dp );
  t.commit();
  s.close();
}

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

public void perform(TestData data) {
    Session session = openSession();
    session.beginTransaction();
    session.enableFetchProfile( "enrollment.details" );
    Enrollment enrollment = ( Enrollment ) session.createCriteria( Enrollment.class ).uniqueResult();
    assertEquals( 3, sessionFactory().getStatistics().getEntityLoadCount() ); // enrollment + (section + student)
    assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
    assertTrue( Hibernate.isInitialized( enrollment.getOffering() ) );
    assertTrue( Hibernate.isInitialized( enrollment.getStudent() ) );
    assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
    session.getTransaction().commit();
    session.close();
  }
}

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

@Test
public void testProxiedBridgeMethod() throws Exception {
  //bridge methods should not be proxied
  Session s = openSession();
  Transaction tx = s.beginTransaction();
  Hammer h = new Hammer();
  s.save(h);
  s.flush();
  s.clear();
  assertNotNull( "The proxy creation failure is breaking things", h.getId() );
  h = (Hammer) s.load( Hammer.class, h.getId() );
  assertFalse( Hibernate.isInitialized( h ) );
  tx.rollback();
  s.close();
}

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

@Test
public void testEntityKeyElementTarget() throws Exception {
  Session s = openSession();
  Transaction tx = s.beginTransaction();
  Atmosphere atm = new Atmosphere();
  Gas o2 = new Gas();
  o2.name = "oxygen";
  atm.composition.put( o2, 94.3 );
  s.persist( o2 );
  s.persist( atm );
  s.flush();
  s.clear();
  atm = (Atmosphere) s.get( Atmosphere.class, atm.id );
  assertTrue( ! Hibernate.isInitialized( atm.composition ) );
  assertEquals( 1, atm.composition.size() );
  assertEquals( o2.name, atm.composition.keySet().iterator().next().name );
  tx.rollback();
  s.close();
}

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

sessionFactory().getStatistics().clear();
Session s = openSession();
Transaction t = s.beginTransaction();
Item i1 = new Item("foo");
Item i2 = new Item("bar");
s.persist("Item", i1);
s.persist("Item", i2);
t.commit();
s.close();
i1 = (Item) iter.next();
i2 = (Item) iter.next();
assertFalse( Hibernate.isInitialized(i1) );
assertFalse( Hibernate.isInitialized(i2) );
i1.getName();
i2.getName();
assertFalse( Hibernate.isInitialized(i1) );
assertFalse( Hibernate.isInitialized(i2) );
assertEquals( i1.getName(), "foo" );
assertEquals( i2.getName(), "bar" );
Hibernate.initialize(i1);
assertFalse( iter.hasNext() );
s.delete(i1);

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

@Test
public void test() {
  doInHibernate(
      this::sessionFactory, session -> {
        Lazy p = new Lazy();
        p.id = ID;
        User u = new User();
        u.id = ID;
        u.setLazy( p );
        session.persist( u );
      }
  );
  doInHibernate(
      this::sessionFactory, session -> {
        session.delete( session.get( Lazy.class, ID ) );
      }
  );
  doInHibernate(
      this::sessionFactory, session -> {
        User user = session.find( User.class, ID );
        assertFalse( Hibernate.isPropertyInitialized( user, "lazy" ) );
        assertNull( user.getLazy() );
        assertTrue( Hibernate.isPropertyInitialized( user, "lazy" ) );
      }
  );
}

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

@Test
public void testManyToOneSelectException() {
  setupTest( PersonManyToOneSelectException.class, 1L, false );
  doInHibernate(
      this::sessionFactory, session -> {
        Person pCheck = session.find( PersonManyToOneSelectException.class, 1L );
        assertNotNull( pCheck );
        assertFalse( Hibernate.isInitialized( pCheck.getCity() ) );
        try {
          Hibernate.initialize( pCheck.getCity() );
          fail( "Should have thrown ObjectNotFoundException" );
        }
        catch (ObjectNotFoundException expected) {
          session.getTransaction().setRollbackOnly();
        }
      }
  );
}

代码示例来源: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
public void testManagedWithUninitializedAssociation() {
  // Delete the Parent
  doInHibernate( this::sessionFactory, s -> {
    Parent loadedParent = (Parent) s.createQuery( "SELECT p FROM Parent p WHERE name=:name" )
        .setParameter( "name", "PARENT" )
        .uniqueResult();
    checkInterceptor( loadedParent, false );
    assertFalse( Hibernate.isPropertyInitialized( loadedParent, "children" ) );
    s.delete( loadedParent );
  } );
  // If the lazy relation is not fetch on cascade there is a constraint violation on commit
}

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

@Test
@TestForIssue(jiraKey = "HHH-13129")
public void testDetachedWithInitializedAssociation() {
  final Parent detachedParent = doInHibernate( this::sessionFactory, s -> {
     Parent parent = s.get( Parent.class, originalParent.getId() );
     assertFalse( Hibernate.isPropertyInitialized( parent, "children" ) );
     // initialize collection before detaching
     parent.getChildren();
     return parent;
  } );
  assertTrue( Hibernate.isPropertyInitialized( detachedParent, "children" ) );
  checkInterceptor( detachedParent, false );
  // Delete the detached Parent with initialized children
  doInHibernate( this::sessionFactory, s -> {
    s.delete( detachedParent );
  } );
  // If the lazy relation is not fetch on cascade there is a constraint violation on commit
}

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

@Test
public void testBlob() throws SQLException {
  final int id = doInHibernate( this::sessionFactory, s -> {
    FileBlob file = new FileBlob();
    file.setBlob( s.getLobHelper().createBlob( "TEST CASE".getBytes() ) );
    // merge transient entity
    file = (FileBlob) s.merge( file );
    return file.getId();
  } );
  doInHibernate( this::sessionFactory, s -> {
    FileBlob file = s.get( FileBlob.class, id );
    assertFalse( Hibernate.isPropertyInitialized( file, "blob" ) );
    Blob blob = file.getBlob();
    try {
      assertTrue(
        Arrays.equals( "TEST CASE".getBytes(), blob.getBytes( 1, (int) file.getBlob().length() ) )
      );
    }
    catch (SQLException ex) {
      fail( "could not determine Lob length" );
    }
  });
}

相关文章