Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register a custom type in Hibernate using org.springframework.orm.hibernate4.LocalSessionFactoryBean

Tags:

spring

I'm migrating from hibernate 3 to hibernate 4, In hibernate 3 the way I was registering a custom type was:

public class AnnotationSessionFactoryBean extends org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean {
private Collection<? extends BasicType> customTypes;

public Collection<? extends BasicType> getCustomTypes() {
    return customTypes;
}

public void setCustomTypes(Collection<? extends BasicType> customTypes) {
    this.customTypes = customTypes;
}

@Override
protected Configuration newConfiguration() throws HibernateException {
    Configuration configuration = super.newConfiguration();
    if (CollectionUtils.hasEntries(customTypes)) {
        for (BasicType customType:customTypes) {
            configuration.registerTypeOverride(customType);
        }
    }
    return configuration;
}}

I'm now trying to do the same operation but using hibernate 4, my question is how is the best way to do this? since I dont have access do change configuration when using "org.springframework.orm.hibernate4.LocalSessionFactoryBean" instead of "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean".

Thanks

like image 748
Luis Avatar asked Nov 17 '12 11:11

Luis


People also ask

What is LocalSessionFactoryBean?

LocalSessionFactoryBean is a FactoryBean<SessionFactory> . It means that it allows creating instances of SessionFactory.

Which one of the listed properties is configured under the LocalSessionFactoryBean?

LocalSessionFactoryBean uses three properties: datasource, mappingResources, and hibernateProperties.

What configuration is supported by the LocalSessionFactoryBean?

Configuration settings can either be read from a Hibernate XML file, specified as "configLocation", or completely via this class. A typical local configuration consists of one or more "mappingResources", various "hibernateProperties" (not strictly necessary), and a "dataSource" that the SessionFactory should use.

What is HibernateTemplate in spring?

Spring's HibernateTemplate provides an abstract layer over a Hibernate Session. It converts Hibernate-specific exceptions to one of the Spring's unchecked data-access exception. It also provides many convenience methods that help you in querying and persisting objects.


1 Answers

In one of your entity classes, annotate the class as follows:

@TypeDefs({
  @TypeDef(name="type1",typeClass=me.sample.Type1.class),
  @TypeDef(name="type2",typeClass=me.sample.Type2.class)
})

Spring 3.1 merged the AnnotationSessionFactoryBean with the LocalSessionFactoryBean, delegating to the Hibernate configuration whether or not to use annotations vs. XML wiring.

like image 125
Peter Bratton Avatar answered Nov 03 '22 00:11

Peter Bratton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!