Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:51:04+00:00 2026-06-11T10:51:04+00:00

I’m setting up a maven-spring-hibernate-mysql-tomcat environment. I would like to inject my Entity manager

  • 0

I’m setting up a maven-spring-hibernate-mysql-tomcat environment.

I would like to inject my Entity manager using the @PersistenceContext annotation.
As I understand to do this, I should have PersistenceAnnotationBeanPostProcessor defined in my application context, pointing to my persistence.xml file.

In this case my application context could be looking like this more or less:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:ox="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
   http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
   http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-3.1.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <!-- Context -->
    <context:component-scan base-package="com.yl.mypack" />

    <!-- AOP -->
    <aop:aspectj-autoproxy />

    <!-- Properties -->

    <context:property-placeholder
        location="classpath:db.connection.properties,applicationProperties.properties" />

    <!-- DB Connection Pool -->
    <bean id="dataSourceGlobal" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">

        <!-- Data Source -->
        <property name="driverClass" value="${driverClass}" />
        <property name="jdbcUrl" value="${jdbcUrl}" />
        <property name="user" value="${user}" />
        <property name="password" value="${password}" />

        <!-- C3P0 Connection pool properties -->
        <property name="minPoolSize" value="${c3p0.min_pool_size}" />
        <property name="maxPoolSize" value="${c3p0.max_pool_size}" />
        <property name="unreturnedConnectionTimeout" value="${c3p0.timeout}" />
        <property name="idleConnectionTestPeriod" value="${c3p0.idle_test_period}" />
        <property name="maxStatements" value="${c3p0.max_statements}" />
        <property name="automaticTestTable" value="${c3p0.automatic_test_table}" />
    </bean>

    <!-- JPA -->
    <!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
            <property name="persistenceUnitName" value="myPU" />
            <property name="dataSource" ref="dataSourceGlobal" />
    </bean>



    <!-- In order to enable EntityManager injection -->
    <bean id="persistenceAnnotation"
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
        <property name="persistenceUnits">
            <map>
                <entry key="myPU" value="classpath:META-INF/persistence.xml" />
            </map>
        </property>
    </bean>

    <!-- Transactions -->
    <tx:annotation-driven />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSourceGlobal" />
    </bean>

</beans>

My persistence.xml could look like this:

<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.transaction.flush_before_completion"
            value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

So now I guess I should be able to inject my entity manager using the @PersistenceContext annotation, at list this is what I get from the documentation. Before I’mm getting to JpaVendorAdapter, my first question would refer the entity manager factory definition:
The following properties:

        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
        <property name="persistenceUnitName" value="myPU" />

Are these required to be referred from the entity manager factory? Is it a must?

My second question would be regarding the JpaVendorAdapter, and in my case HibernateJpaVendorAdapter.

If I use HibernateJpaVendorAdapter, do I still need to have a persistence.xml file? Since some of the properties are overlapping. Moreover, do I still need to have PersistenceAnnotationBeanPostProcessor defined, pointing to my persistence.xml file? Can these 2 go together (PersistenceAnnotationBeanPostProcessor and HibernateJpaVendorAdapter)? Should they go together? What is the best practice, assuming I’m avoiding any JDNI style definitions?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:51:05+00:00Added an answer on June 11, 2026 at 10:51 am

    The combination of data source bean and HibernateJpaVendorAdapter allows you to get rid of persistence.xml with Spring 3.1. Check out this article:

    http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/

    So the answers:

    1. They are not a must.
    2. No, you don’t need to have a persistence.xml.
    3. I am not exactly sure of what use PersistenceAnnotationBeanPostProcessor is.

      <context:component-scan base-package="com.demo.dao" />
      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
          <property name="persistenceUnitName" value="hibernate-resourceLocal"/>
      </bean>
      
      <tx:annotation-driven/>
      
      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
          <property name="entityManagerFactory" ref="entityManagerFactory" />
      </bean>
      

    My Spring application context xml looks as simple as this. And it is working.

    I don’t what the best practice is, but I will keep persistence.xml so that in case I need to expose my service as session bean, it can be easily done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am using JSon response to parse title,date content and thumbnail images and place
I've got a string that has curly quotes in it. I'd like to replace
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.