3

I'm studying Spring MVC and Hibernate. I had no problems handling database connections and queries with Spring mvc (MySql DB).

Now, I'm trying to use Hibernate and I found it intricate: create a hibernate configuration file, create a class for retrieving SessionFactory, create a xml file for any persistent object etc.

I'm sure there's a simplest way that allow me to do an easy configuration using just:

  1. the Spring xml configuration file
  2. annotation (in persistent object classes)

What iI want to reach is something like the following. I saw a similar code in an example, but now I'm no more able to find it on the internet

xxxx-servlet.xml

<bean id="SessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.springgestioneerrori.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.connection.autocommit">true</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.use_sql_comments">false</prop>
            </props>
        </property>       
    </bean>





<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
         <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbName" />
        <property name="user" value="root" />
        <property name="password" value="root" />                   
 </bean>

In the code above, I suppose, that what it's not correct is my dataSource bean. Does anybody know the way to reach my goal?

Thank everybody!

MDP
  • 4,177
  • 21
  • 63
  • 119

2 Answers2

1

i think your properties names inside the 'data source' bean should be like the following :

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/dbName" />
        <property name="username" value="root" />
        <property name="password" value="root" />

so instead of driverClass , it should be driverClassName and so on ..

and please refer to this answer here , it talks about using spring DriverManagerDataSource vs apache BasicDataSource .

Hope that Helps

Community
  • 1
  • 1
  • The property names in your answer are correct from what I can tell from the project I'm working on. – Tobb Oct 06 '14 at 09:01
  • Yes man, thank you. Now when i start eclipse i don't get any error. – MDP Oct 06 '14 at 09:50
1

Everything must be correct, but I'd want to offer you to use *.properties to keep connection configuration to your DB like that

in appContext.xml:

<!-- JDBC DataSource bean -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:properties/database.properties</value>
        </list>
    </property>
</bean>

in database.properties:

jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.schema = schema
jdbc.url = jdbc:mysql://localhost:3306/schema
jdbc.username = root
jdbc.password = password

And what interferes you to use annotations in your classes?

barbariania
  • 499
  • 8
  • 19
  • Thank you man. Actually i already use a properties file, I deleted its code just to make it easier reading this post. Well, nothing keep me from using annotations, what I was trying to say is that I want to use just spring configuration file and annotation, instead of a class for retrieving SessionFactory, an xml file for any persistent object etc. – MDP Oct 06 '14 at 09:49