Unable to Import persistence.xml within applicationContext.xml file

后端 未结 3 1489
既然无缘
既然无缘 2021-01-17 02:58

I\'m using eclipse juno IDE I have Java application which have src folder. within the folder I have:

1) applicationContext.xml

2) persistence.xml

I a

相关标签:
3条回答
  • 2021-01-17 03:11

    Ok, I Solved this..

    What i did is to put the persistence.xml file within the META-INF folder as vikdor and axtavt suggested. but in the application context i didn't import any file.. just wrote this

    <bean id="JPA" class="pack.jpa.JPAQueries"/>
    

    and its work!

    0 讨论(0)
  • 2021-01-17 03:13

    It should be imported as follows:

    <import resource="classpath:META-INF/persistence.xml"/>
    

    assuming the persistence.xml is present in META-INF directory which is a top-level directory in one of your jars on the classpath.

    0 讨论(0)
  • 2021-01-17 03:30

    Your attempt to use persistence.xml as a Spring config makes absolutely no sense, because persistence.xml is not a Spring config.

    If you want to use JPA with Spring, you need to put persistence.xml into META-INF folder inside your source folder, and declare LocalContainerEntityManagerFactory in applicationContext.xml:

    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name = "persistenceUnitName" value = "MyJPA" />
    </bean>
    

    Then you can inject EntityManager into your Spring bean using @PersistenceContext:

    @PersistenceContext
    private EntityManager em;
    

    See also:

    • 13.5 JPA
    0 讨论(0)
提交回复
热议问题