what is the property name for registering an user type in hibernate
-
14-07-2021 - |
문제
Accoriding to hibernate document: section 6.5. Type registry: http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/types.html
We can create a new user type and override existing hibernate's basic types. To use the new user type we need to register it using (see above link):
Configuration cfg = ...;
cfg.registerTypeOverride( new SuperDuperStringType() );
But never in document mentioned how we can register it in hibernate.cfg.xml? I can't believe they forgot to add this to hibernate.cfg.xml, Does anybody know about this? Thanks
해결책
Take a look at that post: Mapping A Custom Type In Hibernate
It worked for me with the following little modification:
the hibernate-mapping closing tag is wrongly formed, it misses the /
(slash).
Appart from that, it explains everything.
다른 팁
Sorry, for the omission. The documentation describes both the registry and the keys, but to an extent expects users to be able to deduce that 1+1=2. I'll make that more explicit.
http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/types.html#types-value-basic Describes the built-in registration keys.
As you can see in that section, the default mapping in the registry for java.lang.String
is to org.hibernate.type.StringType
. So when Hibernate sees an attribute whose java type is java.lang.String
it looks into this registry using java.lang.String
as the "registration key". Again, this is all by default; you can still give explicit type information on each attribute if you wish. That is described elsewhere in the documentation. Basically you would use @Type
or <type/>
.
If you want Hibernate to use your SuperDuperStringType
anytime there is no explicit type information supplied, you'd use "java.lang.String" as SuperDuperStringType
's getRegistrationKeys()
:
public class SuperDuperStringType implements BasicType {
...
@Override
public String[] getRegistrationKeys() {
// lets use delegation and register ourselves under all of StringType's keys
return org.hibernate.type.StringType.INSTANCE.getRegistrationKeys();
}
}
In that case nothing explicit is needed in the mappings.