Configure ObjectDB in spring context

#1

I have a application that initializes with spring, now I'd like to change my data access tier to ObjectDB (from Hibernate : HSQL), but I couldn't find any useful information on it.

My goal is to configure entire ObjectDB (optionally objectdb.conf too) in springs context.xml file so that I'd be able to inject EntityManagerFactory to my beans.

Could you help me on this?

#2

You can see how to use ObjectDB with Spring Framework in this tutorial.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Yes I've seen that option, but my application is not an MVC, its just pure Spring application and I need to avoid creating persistance.xml file.

My previous database.xml (which is included in application-context.xml) look like this

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <!--Defining DataSource-->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="url" value="jdbc:hsqldb:file:db/jminer"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

    <!--Defining Hibernate Configurations-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.jminer.core.persistence"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.connection.pool_size">10</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            </props>
        </property>
    </bean>
</beans>

I need to keep this structure, and define all my database related properties in this file... Can you help me on this? 

I've tried this

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="$objectdb/db/guests.odb"/>
        <property name="username" value="admin"/>
        <property name="password" value="admin"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="datasource"/>
        <property name="persistenceProviderClass" value="com.objectdb.jpa.Provider"/>
    </bean>

for this i need to know ObjectDB data source driver class name... If I don't understand something or missing anything here please fix me.

Also I need to have there ObjectDB specific properties 

<objectdb>
  <general> ... </general>
  <database> ... </database>
  <entities>  ... </entities>
  <schema> ... </schema>
  <server> ... </server>
  <users> ... </users>
  <ssl> ... </ssl>
</objectdb>

if possible at all...

#4

ObjectDB does not have a data source driver class name. Sometimes when it is requested, any data source driver class name (which is not related to ObjectDB) can be specified, and if passed to ObjectDB it is simply ignored. But I am not sure how it will work with these Spring Framework beans.

Regarding the ObjectDB configuration, it must be placed in an objectdb.conf file. But you can also replace the objectdb.conf resource that is embedded in the objectdb.jar file, and then no external file will be needed.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#5

Hi, I've found my solution partially. In case if anyone will need something like this...

@Configuration
@PropertySources(
        value = {
                @PropertySource("classpath:properties/objectdb.properties"),
                @PropertySource("classpath:properties/markets/cryptsy.properties")
        }
)
@ImportResource({
        "classpath:currencies.xml",
        "classpath:market-types.xml",
        "classpath:exchanges.xml",
        "classpath:controllers.xml",
        "classpath:adapters.xml",
        "classpath:aspects.xml"
})
public class ApplicationConfig {

    @Resource
    private Environment environment;

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        ctx.getBean("entityManagerFactory");
    }

    @Bean
    public JpaTransactionManager transactionManager() throws ClassNotFoundException {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory());
        return transactionManager;
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() throws ClassNotFoundException {
        System.setProperty("objectdb.home", environment.getRequiredProperty("objectdb.home"));
        System.setProperty("objectdb.conf", environment.getRequiredProperty("objectdb.conf"));
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(environment.getRequiredProperty("objectdb.db.url"));
        return emf;
    }

} 

I've used JavaConfiguration style of spring and included my xml configurations... Looks even better.


Post Reply

To post a reply and/or subscribe to update notifications - please login