Announcement Announcement Module
No announcement yet.
Hibernate + JPA Configuration Troubles Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate + JPA Configuration Troubles


    I'm fairly comfortable using Hibernate outside of Spring with Annotations. I personally prefer using annotations to map my POJOs (@Entity, @Id, etc.), but I just can't seem to figure out how to configure Hibernate/JPA.

    How do I have the Spring container recognize that I'm using JPA annotations to map my POJOs? The following is my data source and session factory bean in application.xml:

            <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost/_reviews" />
    		<property name="username" value="xxxx" />
    		<property name="password" value="xxxx" />
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="mappingResources">
    			<!-- No hbm.xml files. Just annotations. How do I configure it here? -->
    		<property name="hibernateProperties">
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    				<prop key="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
    				<prop key="current_session_context_class">thread</prop>
    	<bean id="bookServiceDAO" class="">
    		<property name="sessionFactory" ref="sessionFactory" />
    Finally, am I correct to assume that my DAO bean will be injected with the same Hibernate Session Factory object?

    I started programming about six months ago, so I'm still a little shaky on using documentations to solve all of my problems. As such, if you could point to a place where I could have found a solution to this particular problem, I would greatly appreciate it.

    Thank you so much for your help.

  • #2
    You want to use annotations thus use the AnnotationSessionFactoryBean. Next Don't mess around with the transaction.factory_class and current_session_context_class (unless you use JTA!).

    I also suggest a read of the reference guide especially the hibernate section it is pretty clear and consise.