Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2007 Daniel Spiewak
   * Licensed under the Apache License, Version 2.0 (the "License"); 
   * you may not use this file except in compliance with the License. 
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  * See the License for the specific language governing permissions and
  * limitations under the License.
 import java.util.Map;
 import java.util.Set;

Boiler-plate implementation of PolymorphicTypeMapper which requires all mappings to be manually specified. This type mapper will not attempt to do anything clever like caching or storing of mapping results. Rather, it depends entirely upon the developer to explicitly specify the map values for every polymorphic type in use.

If a type which has not been explicitly specified, this converter will default to the fully qualified classname. Thus the inverse conversion will also default to a Class.forName(String) invocation in case of no valid mapping in the relevant class hierarchy.

You should not attempt to use the same instance of this class with more than one instance of EntityManager. This is because EntityManager performs some initialization on the mapper when it is set. While this initialization is in fact thread-safe, it may cause undesired behavior if using two separate entity managers with different table name converters.

Daniel Spiewak
 public final class DefaultPolymorphicTypeMapper implements PolymorphicTypeMapper {
 	private final Map<Class<? extends RawEntity<?>>, Stringmappings;
 	private final Map<StringSet<Class<? extends RawEntity<?>>>> reverse;
 	private Class<? extends RawEntity<?>>[] types;

Convenience constructor which will construct a set of mappings on the fly based on the specified entity types. Actual construction of the mappings is delayed and invoked directly by the EntityManager instance which receives the instance of this class. This is to allow EntityManager to pass the relevant table name converter to be used in the initialization process.

It is very important that an instance created using this constructor is not used with more than one EntityManager.

types The polymorphic types which must be mapped.
 	public DefaultPolymorphicTypeMapper(Class<? extends RawEntity<?>>... types) {
 		this(new HashMap<Class<? extends RawEntity<?>>, String>());
 		this. = types;

Creates a new instance with the specified {type => flag} mappings.

mappings The mappings from entity type to polymorphic flag value.
 	public DefaultPolymorphicTypeMapper(Map<Class<? extends RawEntity<?>>, Stringmappings) {
 		this. = mappings;
 		this. = new HashMap<StringSet<Class<? extends RawEntity<?>>>>();
 	private void createReverseMappings() {
 		for (Class<? extends RawEntity<?>> type : .keySet()) {
 			String value = .get(type);
 			Set<Class<? extends RawEntity<?>>> set = .get(value);
 			if (set == null) {
 				set = new HashSet<Class<? extends RawEntity<?>>>();
 	void resolveMappings(TableNameConverter converter) {
 		if ( == null) {
		while (converter instanceof PluralizedTableNameConverter) {
			converter = ((PluralizedTableNameConverterconverter).getDelegate();
		for (Class<? extends RawEntity<?>> type : ) {
		 = null;
	public String convert(Class<? extends RawEntity<?>> type) {
		String back = .get(type);
		if (back == null) {
			return type.getName();		// sane default
		return back;
	public Class<? extends RawEntity<?>> invert(Class<? extends RawEntity<?>> parentString type) {
		Set<Class<? extends RawEntity<?>>> set = .get(type);
		if (set != null && set.size() != 0) {
			for (Class<? extends RawEntity<?>> clazz : set) {
                if (parent.isAssignableFrom(clazz)) {
					return clazz;
		try {
			return (Class<? extends RawEntity<?>>) Class.forName(type);		// classname fallback
catch (Throwable t) {
		throw new IllegalArgumentException("No valid inverse mapping for type value \"" + type + '"');
New to GrepCode? Check out our FAQ X