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

Hibernate example source code file (AccessMappingTest.java)

This example Hibernate source code file (AccessMappingTest.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

annotationconfiguration, annotationconfiguration, class, entitytuplizer, entitytuplizer, exception, exception, field, id, mappingexception, onetomany, property, sessionfactoryimplementor, sessionfactoryimplementor

The Hibernate AccessMappingTest.java source code

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2008-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.test.annotations.access.jpa;

import org.hibernate.MappingException;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.property.BasicPropertyAccessor;
import org.hibernate.property.DirectPropertyAccessor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tuple.entity.EntityTuplizer;

import junit.framework.TestCase;

import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.testing.TestForIssue;


/**
 * Tests verifying the correct behaviour for the usage of {@code @javax.persistence.Access}.
 *
 * @author Hardy Ferentschik
 */
@SuppressWarnings( {"deprecation"})
public class AccessMappingTest extends TestCase {
	private ServiceRegistry serviceRegistry;

	protected void setUp() {
		serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() );
	}

	protected void tearDown() {
		if ( serviceRegistry != null ) {
			ServiceRegistryBuilder.destroy( serviceRegistry );
		}
	}

	public void testInconsistentAnnotationPlacement() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		cfg.addAnnotatedClass( Course1.class );
		cfg.addAnnotatedClass( Student.class );
		try {
			cfg.buildSessionFactory( serviceRegistry );
			fail( "@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail." );
		}
		catch ( MappingException e ) {
			// success
		}
	}

	public void testFieldAnnotationPlacement() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		Class<?> classUnderTest = Course6.class;
		cfg.addAnnotatedClass( classUnderTest );
		cfg.addAnnotatedClass( Student.class );
		SessionFactoryImplementor factory = ( SessionFactoryImplementor ) cfg.buildSessionFactory( serviceRegistry );
		EntityTuplizer tuplizer = factory.getEntityPersister( classUnderTest.getName() )
				.getEntityMetamodel()
				.getTuplizer();
		assertTrue(
				"Field access should be used.",
				tuplizer.getIdentifierGetter() instanceof DirectPropertyAccessor.DirectGetter
		);
	}

	public void testPropertyAnnotationPlacement() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		Class<?> classUnderTest = Course7.class;
		cfg.addAnnotatedClass( classUnderTest );
		cfg.addAnnotatedClass( Student.class );
		SessionFactoryImplementor factory = ( SessionFactoryImplementor ) cfg.buildSessionFactory( serviceRegistry );
		EntityTuplizer tuplizer = factory.getEntityPersister( classUnderTest.getName() )
				.getEntityMetamodel()
				.getTuplizer();
		assertTrue(
				"Property access should be used.",
				tuplizer.getIdentifierGetter() instanceof BasicPropertyAccessor.BasicGetter
		);
	}

	public void testExplicitPropertyAccessAnnotationsOnProperty() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		Class<?> classUnderTest = Course2.class;
		cfg.addAnnotatedClass( classUnderTest );
		cfg.addAnnotatedClass( Student.class );
		SessionFactoryImplementor factory = ( SessionFactoryImplementor ) cfg.buildSessionFactory( serviceRegistry );
		EntityTuplizer tuplizer = factory.getEntityPersister( classUnderTest.getName() )
				.getEntityMetamodel()
				.getTuplizer();
		assertTrue(
				"Property access should be used.",
				tuplizer.getIdentifierGetter() instanceof BasicPropertyAccessor.BasicGetter
		);
	}

	public void testExplicitPropertyAccessAnnotationsOnField() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		cfg.addAnnotatedClass( Course4.class );
		cfg.addAnnotatedClass( Student.class );
		try {
			cfg.buildSessionFactory( serviceRegistry );
			fail( "@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail." );
		}
		catch ( MappingException e ) {
			// success
		}
	}

	public void testExplicitPropertyAccessAnnotationsWithHibernateStyleOverride() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		Class<?> classUnderTest = Course3.class;
		cfg.addAnnotatedClass( classUnderTest );
		cfg.addAnnotatedClass( Student.class );
		SessionFactoryImplementor factory = ( SessionFactoryImplementor ) cfg.buildSessionFactory( serviceRegistry );
		EntityTuplizer tuplizer = factory.getEntityPersister( classUnderTest.getName() )
				.getEntityMetamodel()
				.getTuplizer();
		assertTrue(
				"Field access should be used.",
				tuplizer.getIdentifierGetter() instanceof DirectPropertyAccessor.DirectGetter
		);

		assertTrue(
				"Property access should be used.",
				tuplizer.getGetter( 0 ) instanceof BasicPropertyAccessor.BasicGetter
		);
	}

	public void testExplicitPropertyAccessAnnotationsWithJpaStyleOverride() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		Class<?> classUnderTest = Course5.class;
		cfg.addAnnotatedClass( classUnderTest );
		cfg.addAnnotatedClass( Student.class );
		SessionFactoryImplementor factory = ( SessionFactoryImplementor ) cfg.buildSessionFactory( serviceRegistry );
		EntityTuplizer tuplizer = factory.getEntityPersister( classUnderTest.getName() )
				.getEntityMetamodel()
				.getTuplizer();
		assertTrue(
				"Field access should be used.",
				tuplizer.getIdentifierGetter() instanceof DirectPropertyAccessor.DirectGetter
		);

		assertTrue(
				"Property access should be used.",
				tuplizer.getGetter( 0 ) instanceof BasicPropertyAccessor.BasicGetter
		);
	}

	public void testDefaultFieldAccessIsInherited() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		Class<?> classUnderTest = User.class;
		cfg.addAnnotatedClass( classUnderTest );
		cfg.addAnnotatedClass( Person.class );
		cfg.addAnnotatedClass( Being.class );
		SessionFactoryImplementor factory = ( SessionFactoryImplementor ) cfg.buildSessionFactory( serviceRegistry );
		EntityTuplizer tuplizer = factory.getEntityPersister( classUnderTest.getName() )
				.getEntityMetamodel()
				.getTuplizer();
		assertTrue(
				"Field access should be used since the default access mode gets inherited",
				tuplizer.getIdentifierGetter() instanceof DirectPropertyAccessor.DirectGetter
		);
	}

	public void testDefaultPropertyAccessIsInherited() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		cfg.addAnnotatedClass( Horse.class );
		cfg.addAnnotatedClass( Animal.class );

		SessionFactoryImplementor factory = ( SessionFactoryImplementor ) cfg.buildSessionFactory( serviceRegistry );
		EntityTuplizer tuplizer = factory.getEntityPersister( Animal.class.getName() )
				.getEntityMetamodel()
				.getTuplizer();
		assertTrue(
				"Property access should be used since explicity configured via @Access",
				tuplizer.getIdentifierGetter() instanceof BasicPropertyAccessor.BasicGetter
		);

		tuplizer = factory.getEntityPersister( Horse.class.getName() )
				.getEntityMetamodel()
				.getTuplizer();
		assertTrue(
				"Property access should be used since the default access mode gets inherited",
				tuplizer.getGetter( 0 ) instanceof BasicPropertyAccessor.BasicGetter
		);
	}

	@TestForIssue( jiraKey = "HHH-5004")
	public void testAccessOnClassAndId() throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		cfg.addAnnotatedClass( Course8.class );
		cfg.addAnnotatedClass( Student.class );
		cfg.buildSessionFactory( serviceRegistry );
	}
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate AccessMappingTest.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.