[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-6927) Cannot register a type through configuration if the type is parametrized

Stephane Nicoll (JIRA) noreply at atlassian.com
Fri Dec 30 12:13:19 EST 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-6927?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=44778#comment-44778 ] 

Stephane Nicoll commented on HHH-6927:
--------------------------------------

Hi Steve,

Thanks for the quick reply. I had a a look to these methods once again to make sure I did not miss anything and I don't really understand how registration keys are related to my problem.

Consider the following use case:

{code:xml}
@Type(type = "MyUserType", parameters =
    @Parameter(name = "myParam", value = "myValue")
)
private SomeObject foo;
{code}

Actually the user type needs the actual class of the object it is mapping. Consider SomeObject a subclass of some base class, the user type is generic for any subclass of that base class but it needs to know the actual subclass type to work properly when retrieving the instance. Since nullSafeGet or the userType itself does not provide that value we have to pass it as a parameter.

Anyway, I want to register a type "MyUserType" for that user type. I can't use these methods because If I do that, an instance is not injected with the parameter I need. I believe that you're saying I should register one user type per subclass of the type I have and assign a registration key to that specific instance. If that's what you mean, fine considering that you can have all those combinations. 

What if if you don't? After all, if you look at my sample above, I just want to do what I can do through hbm or annotation configuration. 

> Cannot register a type through configuration if the type is parametrized
> ------------------------------------------------------------------------
>
>                 Key: HHH-6927
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6927
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.6.5
>            Reporter: Stephane Nicoll
>
> Since I don't want to hardcode @TypeDefs in each class hierarchy that uses a custom type, I am using the {{cfg.registerTypeOverride}} method. This works fine for simple user type but I have a parameterized user type. 
> In that case, using this method leads to a single user type instance being used and shared for all my entities and no parameter is injected at all. Looking a bit deeper I think I found the culprit in TypeResolver.
> {code:java}
> public Type heuristicType(String typeName, Properties parameters) throws MappingException {
> 		Type type = basic( typeName );
> 		if ( type != null ) {
> 			return type;
> 		}
> 		try {
> 			Class typeClass = ReflectHelper.classForName( typeName );
> 			if ( typeClass != null ) {
> 				return typeFactory.byClass( typeClass, parameters );
> 			}
> 		}
> 		catch ( ClassNotFoundException ignore ) {
> 		}
> 		return null;
> 	}
> {code}
> base(typeName) will actually return the type that I have registered. Notice that basic does not take the {{parameters}} argument into account. Since it's not null it just returns that type. 
> Maybe I did something wrong. I was surprised that cfg actually registers *instances* and not classes. Both @TypeDefs and hbm.xml specifies classes. Maybe adding an option to register a class programmatically? 

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list