We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
开发者_高级运维 Improve this questionI'm developing a dynamic website using JSP & Spring MVC 3.0 and I can't get hibernate to work with it. Also, I'm using PostgreSQL as my database. I don't know which libraries should I put on. I'm quite new to this technology. can someone help me with this? thanks
The libraries depend on the functionalities you may want to implement.
<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>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.3-603.jdbc3</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>
If you're using maven, these should give you a good start to work with. c3p0 is for connection pooling on your postgres connection.
I'd strongly advise to go for the hibernate annotation approach (as you can see by the annotation packages added via maven). So to make use of your postgresql db + hibernate, add the following to your applicationContext:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<!-- Uses mchange 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}" />
<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>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="package.with.your.domain.objects.in.it"
p:hibernateProperties-ref="hibernateProps" />
What you basically did here was to configure the jdbc connection using a jdbc.properties file (you need to make one - if you need help with it, gimme a comment. Most important thing is the jdbc driver, which is org.postgresql.Driver). Then you wired these properties into a c3p0 connection pool and pass this to a AnnotationSessionfactory.
To use this sessionfactory in your application, you could extend the HibernateDaoSupport in your Dao class (the HibernateDaoSupport comes with Spring and basically does the transaction management for you) and there inject the sessionfactory on the sessionFactory parameter. However, it is advised to not use the HibernateDaoSupport any longer, but instead go for the @Repository Annotation - check that on google.
I hope this gives you something to start with. You could always google for further tutorials like http://www.vaannila.com/hibernate/hibernate-example/hibernate-annotations-1.html
cheers
精彩评论