Java Code Examples for org.hibernate.Criteria#setFetchMode()

The following examples show how to use org.hibernate.Criteria#setFetchMode() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: maven-framework-project   File: AppResource.java    License: MIT License 6 votes vote down vote up
/**
 * The @GET annotation causes this method to be invoked whenever an HTTP GET request is received for 
 * the registered URL. 
 */
@GET
public App getAppData( @PathParam("appId") Long appId ) {
	
	// Start a Hibernate session, using the Hibernate SessionFactory created by MasterNodeInitializer or SlaveNodeInitializer.
	String mode = (String) context.getAttribute("mode");
	Session session = mode.equals("master") ? MasterNodeInitializer.openSession() : SlaveNodeInitializer.openSession();
	session.beginTransaction();
	
	// Fetch an App for the given ID, using eager fetching.  The conversion to JSON happens after the 
	// Hibernate Session is closed... so if lazy fetching were used, then the JSON converter would fail 
	// when trying to access associated objects.
	Criteria criteria = session.createCriteria(App.class);
	criteria.add( Restrictions.eq("id", appId) );
	criteria.setFetchMode("supportedDevices", FetchMode.SELECT);
	criteria.setFetchMode("customerReviews", FetchMode.SELECT);
	App app = (App) criteria.uniqueResult();
	
	// Cleanup Hibernate
	session.getTransaction().commit();
	session.clear();
	session.close();
	
	return app;
}
 
Example 2
Source Project: maven-framework-project   File: AppResource.java    License: MIT License 6 votes vote down vote up
/**
 * The @GET annotation causes this method to be invoked whenever an HTTP GET request is received for 
 * the registered URL. 
 */
@GET
public App getAppData( @PathParam("appId") Long appId ) {
	// Initialize Hibernate
	Session session = StartupDataLoader.openSession();
	session.beginTransaction();
	
	// Fetch an App for the given ID, using eager fetching.  The conversion to JSON happens after the 
	// Hibernate Session is closed... so if lazy fetching were used, then the JSON converter would fail 
	// when trying to access associated objects.
	Criteria criteria = session.createCriteria(App.class);
	criteria.add( Restrictions.eq("id", appId) );
	criteria.setFetchMode("supportedDevices", FetchMode.SELECT);
	criteria.setFetchMode("customerReviews", FetchMode.SELECT);
	App app = (App) criteria.uniqueResult();
	
	// Cleanup Hibernate
	session.getTransaction().commit();
	session.clear();
	session.close();
	
	return app;
}
 
Example 3
@SuppressWarnings("unchecked")
public Mpp getInfos(MppId medecinePackageID) {
    log.debug("Getting infos for Mpp " + medecinePackageID);
    Session sess = this.getSessionFactory().getCurrentSession();
    Criteria c = sess.createCriteria(Mpp.class);
    c.add(Restrictions.eq("id", medecinePackageID));
    c.setFetchMode("mp", FetchMode.JOIN);
    List<Mpp> result = c.list();
    if (result.size() == 0) {
        return null;
    }
    Validate.isTrue(result.size() == 1, "More than One Mpp found!");
    return result.get(0);
}
 
Example 4
@SuppressWarnings("unchecked")
public Mpp getInfos(MppId medecinePackageID) {
	log.debug("Getting infos for Mpp " + medecinePackageID);
	Session sess = getSessionFactory().getCurrentSession();
	Criteria c = sess.createCriteria(Mpp.class);
	c.add(Restrictions.eq("id", medecinePackageID));
	c.setFetchMode("mp", FetchMode.JOIN);
	List<Mpp> result = c.list();
	if (result.size() == 0) {
		return null;
	}
	Validate.isTrue(result.size() == 1, "More than One Mpp found!");
	return result.get(0);
}
 
Example 5
Source Project: maven-framework-project   File: AppResource.java    License: MIT License 5 votes vote down vote up
/**
 * The @GET annotation causes this method to be invoked whenever an HTTP GET request is received for 
 * the registered URL. 
 */
@GET
public App getAppData( @PathParam("appId") Long appId ) {
	// Initialize Hibernate
	Configuration configuration = new Configuration();
	configuration.configure();
	ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
	SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	Session session = sessionFactory.openSession();
	session.beginTransaction();
	
	// Fetch an App for the given ID, using eager fetching.  The conversion to JSON happens after the 
	// Hibernate Session is closed... so if lazy fetching were used, then the JSON converter would fail 
	// when trying to access associated objects.
	Criteria criteria = session.createCriteria(App.class);
	criteria.add( Restrictions.eq("id", appId) );
	criteria.setFetchMode("supportedDevices", FetchMode.SELECT);
	criteria.setFetchMode("customerReviews", FetchMode.SELECT);
	App app = (App) criteria.uniqueResult();
	
	// Cleanup Hibernate
	session.getTransaction().commit();
	session.clear();
	session.close();
	sessionFactory.close();
	
	return app;
}
 
Example 6
private void buildCriteria(final Criteria criteria)
{
  for (final Object obj : filterSettings) {
    if (obj instanceof Criterion) {
      criteria.add((Criterion) obj);
    } else if (obj instanceof Order) {
      criteria.addOrder((Order) obj);
    } else if (obj instanceof Alias) {
      final Alias alias = (Alias) obj;
      criteria.createAlias(alias.arg0, alias.arg1, alias.joinType);
    } else if (obj instanceof QueryFilter) {
      final QueryFilter filter = (QueryFilter) obj;
      Criteria subCriteria;
      if (StringUtils.isEmpty(filter.getAlias()) == true) {
        subCriteria = criteria.createCriteria(filter.getName());
      } else {
        subCriteria = criteria.createCriteria(filter.getName(), filter.getAlias());
      }
      filter.buildCriteria(subCriteria);
    }
  }
  if (associationPath != null) {
    criteria.setFetchMode(associationPath, fetchMode);
  }
  if (maxResults > 0) {
    criteria.setMaxResults(maxResults);
  }
}
 
Example 7
public void testSortables() throws Exception {
	Session s = openSession();
	Baz b = new Baz();
	b.setName("name");
	SortedSet ss = new TreeSet();
	ss.add( new Sortable("foo") );
	ss.add( new Sortable("bar") );
	ss.add( new Sortable("baz") );
	b.setSortablez(ss);
	s.save(b);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	Criteria cr = s.createCriteria(Baz.class);
	cr.setFetchMode("topGlarchez", FetchMode.LAZY);
	List result = cr
		.addOrder( Order.asc("name") )
		.list();
	assertTrue( result.size()==1 );
	b = (Baz) result.get(0);
	assertTrue( b.getSortablez().size()==3 );
	assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" );
	s.connection().commit();
	s.close();

	s = openSession();
	result = s.createQuery("from Baz baz left join fetch baz.sortablez order by baz.name asc")
		.list();
	b = (Baz) result.get(0);
	assertTrue( b.getSortablez().size()==3 );
	assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" );
	s.connection().commit();
	s.close();

	s = openSession();
	result = s.createQuery("from Baz baz order by baz.name asc")
		.list();
	b = (Baz) result.get(0);
	assertTrue( b.getSortablez().size()==3 );
	assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" );
	s.delete(b);
	s.flush();
	s.connection().commit();
	s.close();

}
 
Example 8
public void testManyToManyOrdering() {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	User gavin = new User( "gavin", "jboss" );
	User steve = new User( "steve", "jboss" );
	User max = new User( "max", "jboss" );
	User emmanuel = new User( "emmanuel", "jboss" );
	s.persist( gavin );
	s.persist( steve );
	s.persist( max );
	s.persist( emmanuel );
	Group hibernate = new Group( "hibernate", "jboss" );
	hibernate.addUser( gavin );
	hibernate.addUser( steve );
	hibernate.addUser( max );
	hibernate.addUser( emmanuel );
	s.persist( hibernate );
	t.commit();
	s.close();

	// delayed collection load...
	s = openSession();
	t = s.beginTransaction();
	hibernate = ( Group ) s.get( Group.class, hibernate.getId() );
	assertFalse( Hibernate.isInitialized( hibernate.getUsers() ) );
	assertEquals( 4, hibernate.getUsers().size() );
	assertOrdering( hibernate.getUsers() );
	t.commit();
	s.close();

	// HQL (non eager)
	s = openSession();
	t = s.beginTransaction();
	hibernate = ( Group ) s.createQuery( "from Group" ).uniqueResult();
	assertFalse( Hibernate.isInitialized( hibernate.getUsers() ) );
	assertEquals( 4, hibernate.getUsers().size() );
	assertOrdering( hibernate.getUsers() );
	t.commit();
	s.close();

	// HQL (eager)
	s = openSession();
	t = s.beginTransaction();
	hibernate = ( Group ) s.createQuery( "from Group g inner join fetch g.users" ).uniqueResult();
	assertTrue( Hibernate.isInitialized( hibernate.getUsers() ) );
	assertEquals( 4, hibernate.getUsers().size() );
	assertOrdering( hibernate.getUsers() );
	t.commit();
	s.close();

	// criteria load (forced eager fetch)
	s = openSession();
	t = s.beginTransaction();
	Criteria criteria = s.createCriteria( Group.class );
	criteria.setFetchMode( "users", FetchMode.JOIN );
	hibernate = ( Group ) criteria.uniqueResult();
	assertTrue( Hibernate.isInitialized( hibernate.getUsers() ) );
	assertEquals( 4, hibernate.getUsers().size() );
	assertOrdering( hibernate.getUsers() );
	t.commit();
	s.close();

	// criteria load (forced non eager fetch)
	s = openSession();
	t = s.beginTransaction();
	criteria = s.createCriteria( Group.class );
	criteria.setFetchMode( "users", FetchMode.SELECT );
	hibernate = ( Group ) criteria.uniqueResult();
	assertFalse( Hibernate.isInitialized( hibernate.getUsers() ) );
	assertEquals( 4, hibernate.getUsers().size() );
	assertOrdering( hibernate.getUsers() );
	t.commit();
	s.close();

	// clean up
	s = openSession();
	t = s.beginTransaction();
	s.delete( gavin );
	s.delete( steve );
	s.delete( max );
	s.delete( emmanuel );
	s.delete( hibernate );
	t.commit();
	s.close();
}