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

  • Hibernate + JPA Configuration Troubles

    Hi,

    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:

    Code:
            <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>
    	
    	<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>
    		<property name="hibernateProperties">
    			<props>
    				<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>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="bookServiceDAO" class="com.springbookstore.data.BookServiceDAOImplementation">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    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.

    Comment

    Working...
    X