alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (FetchProfileBinderTest.java)

This example Hibernate source code file (FetchProfileBinderTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

annotationbindingcontextimpl, baseunittestcase, basicserviceregistryimpl, fetchprofile, fetchprofile, fetchprofiles, foo, index, index, metadataimpl, test, test, util, wrong, wrong

The Hibernate FetchProfileBinderTest.java source code

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2011, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.hibernate.metamodel.source.annotations.global;

import java.util.Iterator;

import org.jboss.jandex.Index;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.hibernate.MappingException;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.FetchProfile;
import org.hibernate.annotations.FetchProfiles;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.annotations.AnnotationBindingContextImpl;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;

import static junit.framework.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author Hardy Ferentschik
 */
public class FetchProfileBinderTest extends BaseUnitTestCase {

	private BasicServiceRegistryImpl serviceRegistry;
	private ClassLoaderService service;
	private MetadataImpl meta;

	@Before
	public void setUp() {
		serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry();
		service = serviceRegistry.getService( ClassLoaderService.class );
		meta = (MetadataImpl) new MetadataSources( serviceRegistry ).buildMetadata();
	}

	@After
	public void tearDown() {
		serviceRegistry.destroy();
	}

	@Test
	public void testSingleFetchProfile() {
		@FetchProfile(name = "foo", fetchOverrides = {
				@FetchProfile.FetchOverride(entity = Foo.class, association = "bar", mode = FetchMode.JOIN)
		})
		class Foo {
		}
		Index index = JandexHelper.indexForClass( service, Foo.class );

		FetchProfileBinder.bind( new AnnotationBindingContextImpl( meta, index ) );

		Iterator<org.hibernate.metamodel.binding.FetchProfile> mappedFetchProfiles = meta.getFetchProfiles().iterator();
		assertTrue( mappedFetchProfiles.hasNext() );
		org.hibernate.metamodel.binding.FetchProfile profile = mappedFetchProfiles.next();
		assertEquals( "Wrong fetch profile name", "foo", profile.getName() );
		org.hibernate.metamodel.binding.FetchProfile.Fetch fetch = profile.getFetches().iterator().next();
		assertEquals( "Wrong association name", "bar", fetch.getAssociation() );
		assertEquals( "Wrong association type", Foo.class.getName(), fetch.getEntity() );
	}

	@Test
	public void testFetchProfiles() {
		Index index = JandexHelper.indexForClass( service, FooBar.class );
		FetchProfileBinder.bind( new AnnotationBindingContextImpl( meta, index ) );

		Iterator<org.hibernate.metamodel.binding.FetchProfile> mappedFetchProfiles = meta.getFetchProfiles().iterator();
		assertTrue( mappedFetchProfiles.hasNext() );
		org.hibernate.metamodel.binding.FetchProfile profile = mappedFetchProfiles.next();
		assertProfiles( profile );

		assertTrue( mappedFetchProfiles.hasNext() );
		profile = mappedFetchProfiles.next();
		assertProfiles( profile );
	}

	private void assertProfiles(org.hibernate.metamodel.binding.FetchProfile profile) {
		if ( profile.getName().equals( "foobar" ) ) {
			org.hibernate.metamodel.binding.FetchProfile.Fetch fetch = profile.getFetches().iterator().next();
			assertEquals( "Wrong association name", "foobar", fetch.getAssociation() );
			assertEquals( "Wrong association type", FooBar.class.getName(), fetch.getEntity() );
		}
		else if ( profile.getName().equals( "fubar" ) ) {
			org.hibernate.metamodel.binding.FetchProfile.Fetch fetch = profile.getFetches().iterator().next();
			assertEquals( "Wrong association name", "fubar", fetch.getAssociation() );
			assertEquals( "Wrong association type", FooBar.class.getName(), fetch.getEntity() );
		}
		else {
			fail( "Wrong fetch name:" + profile.getName() );
		}
	}

	@Test(expected = MappingException.class)
	public void testNonJoinFetchThrowsException() {
		@FetchProfile(name = "foo", fetchOverrides = {
				@FetchProfile.FetchOverride(entity = Foo.class, association = "bar", mode = FetchMode.SELECT)
		})
		class Foo {
		}
		Index index = JandexHelper.indexForClass( service, Foo.class );

		FetchProfileBinder.bind( new AnnotationBindingContextImpl( meta, index ) );
	}

	@FetchProfiles( {
			@FetchProfile(name = "foobar", fetchOverrides = {
					@FetchProfile.FetchOverride(entity = FooBar.class, association = "foobar", mode = FetchMode.JOIN)
			}),
			@FetchProfile(name = "fubar", fetchOverrides = {
					@FetchProfile.FetchOverride(entity = FooBar.class, association = "fubar", mode = FetchMode.JOIN)
			})
	})
	class FooBar {
	}
}


Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate FetchProfileBinderTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.