views:

35

answers:

2

Hi all, im using hibernate and i maps my entities with annotations (so no xml files).

I finally decided to try spring framework but I encountered some problems to make it work.

All tutorials i found are very dispersive and most of them use xml file to map an entity... Can you help me to correctly write a xml config file for spring+hibernate?

Thanks.

A: 

If you use JPA (EntityManager), the @Entity classes are discovered automatically.

In spring you obtain the EntityManager via the LocalEntityManagerFactoryBean, and then injecting @PersistenceContext into your classes. Check here for more info

Bozho
+4  A: 

Hi! The following is a working example from one of my apps. They should all go in the applicationContext or in a .xml loaded by the appContext.

The first snippet is the configuration of the datasource, using connection pooling:

<bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
    p:driverClass="${jdbc.driverClassName}"
    p:jdbcUrl="${jdbc.url}"
    p:user="${jdbc.username}"
    p:password="${jdbc.password}" />

Next up is a Property bean. If you are unsure about any of these settings, please refer to the corresponding APIs.

<bean id="hibernateProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.c3p0.minPoolSize">5</prop>
            <prop key="hibernate.c3p0.maxPoolSize">20</prop>
            <prop key="hibernate.c3p0.idleTestPeriod">300</prop>
            <prop key="hibernate.c3p0.timeout">600</prop>
            <prop key="hibernate.c3p0.max_statement">50</prop>
            <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
            <prop key="hibernate.c3p0.preferredTestQuery">select 1;</prop>
        </props>
    </property>
</bean>

Now this is the interesting part. Here you wire it all together and tell the sessionfactory where to look for annotated Classes (packagesToScan).

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:packagesToScan="your.package.path"
      p:hibernateProperties-ref="hibernateProps" />

To make this example work, you should use the following dependencies (given for maven):

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.2.7.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>

    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1</version>
    </dependency>

When you have setup your project like this, the following @Entity mappings are managed by spring automatically:

package your.package.path;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "table_name")
public class DomainObject implements Serializable {

.
.
.
}

If you have any further questions, please let me know.

greetings

chzbrgla
+1 as you need it more than Bozho :)
willcodejavaforfood
@chzbrgla: thank you very much! I have a question about dataSource, you choose "com.mchange.v2.c3p0.ComboPooledDataSource" but in another example i see other datasource class, like "org.springframework.jdbc.datasource.DriverManagerDataSource".How has it been choose?Thank again.
blow
Easy: Have a look at the API :phttp://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/datasource/DriverManagerDataSource.html"NOTE: This class is not an actual connection pool; it does not actually pool Connections."So choose c3p0 if you want pooling in your application.
chzbrgla