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

Hibernate example source code file (DynamicMapOneToOneTest.java)

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

address, dynamicmaponetoonetest, entitystatistics, hashmap, main, map, map, object, object, override, person, person, string, string, util

The Hibernate DynamicMapOneToOneTest.java source code

//$Id: DynamicMapOneToOneTest.java 10977 2006-12-12 23:28:04Z steve.ebersole@jboss.com $
package org.hibernate.test.onetoone.nopojo;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.stat.EntityStatistics;

import org.junit.Test;

import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * @author Gavin King
 */
public class DynamicMapOneToOneTest extends BaseCoreFunctionalTestCase {
	@Override
	public String[] getMappings() {
		return new String[] { "onetoone/nopojo/Person.hbm.xml" };
	}

	@Override
	public void configure(Configuration cfg) {
		cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
		cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
		cfg.setProperty( Environment.DEFAULT_ENTITY_MODE, EntityMode.MAP.toString() );
	}

	@Test
	public void testOneToOneOnSubclass() {
		Map person = new HashMap();
		person.put( "name", "Steve" );
		Map address = new HashMap();
		address.put( "zip", "12345" );
		address.put( "state", "TX" );
		address.put( "street", "123 Main St" );

		person.put( "address", address );
		address.put( "owner", person );

		Session s = openSession();
		s.beginTransaction();
		s.persist( "Person", person );
		s.getTransaction().commit();
		s.close();

		s = openSession();
		s.beginTransaction();

		EntityStatistics addressStats = sessionFactory().getStatistics().getEntityStatistics( "Address" );

		person = ( Map ) s.createQuery( "from Person p join fetch p.address" ).uniqueResult();
		assertNotNull( "could not locate person", person );
		assertNotNull( "could not locate persons address", person.get( "address" ) );
		s.clear();

		Object[] tuple = ( Object[] ) s.createQuery( "select p.name, p from Person p join fetch p.address" ).uniqueResult();
		assertEquals( tuple.length, 2 );
		person = ( Map ) tuple[1];
		assertNotNull( "could not locate person", person );
		assertNotNull( "could not locate persons address", person.get( "address" ) );

		s.delete( "Person", person );

		s.getTransaction().commit();
		s.close();

		assertEquals( addressStats.getFetchCount(), 0 );
	}

}

Other Hibernate examples (source code examples)

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