How to configure Spring along with persistence.xml to scan for entities












0















I have a test-persistence.xml file in the src/test/resources location. The content of this file is:



<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="mmTest" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
</properties>
</persistence-unit>
</persistence>


My Spring configuration looks like below:



<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.sample"/>
<property name="persistenceUnitName" value="mmTest"/>
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
</bean>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocation" value="classpath*:test-persistence.xml"/>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>


When I try to run a JUnit test with this context configuration set I get the exception:




java.lang.IllegalArgumentException: Unknown entity:
com.sample.SampleEntity




Is it possible to have persistence.xml file for tests and configure Spring to scan for classes with @Entity? I do not want to use JPA specific class discovery like jar-file or listed classes. I want to use Spring to discover those classes.



The strange thing is when I move test-persistence.xml to the location: /src/main/resources/META-INF then everything works correctly. However, I do not want to have test specific configuration inside src/main.



Currently I solved the problem by specifying DataSource instead of persistenceUnitManager and persistenceUnitName.



But I would like to know how to solve it without using DataSource.










share|improve this question



























    0















    I have a test-persistence.xml file in the src/test/resources location. The content of this file is:



    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="mmTest" transaction-type="RESOURCE_LOCAL">
    <properties>
    <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
    <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/>
    <property name="javax.persistence.jdbc.user" value="sa"/>
    <property name="javax.persistence.jdbc.password" value=""/>
    <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
    </properties>
    </persistence-unit>
    </persistence>


    My Spring configuration looks like below:



    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="com.sample"/>
    <property name="persistenceUnitName" value="mmTest"/>
    <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    </bean>
    <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocation" value="classpath*:test-persistence.xml"/>
    </bean>
    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>


    When I try to run a JUnit test with this context configuration set I get the exception:




    java.lang.IllegalArgumentException: Unknown entity:
    com.sample.SampleEntity




    Is it possible to have persistence.xml file for tests and configure Spring to scan for classes with @Entity? I do not want to use JPA specific class discovery like jar-file or listed classes. I want to use Spring to discover those classes.



    The strange thing is when I move test-persistence.xml to the location: /src/main/resources/META-INF then everything works correctly. However, I do not want to have test specific configuration inside src/main.



    Currently I solved the problem by specifying DataSource instead of persistenceUnitManager and persistenceUnitName.



    But I would like to know how to solve it without using DataSource.










    share|improve this question

























      0












      0








      0








      I have a test-persistence.xml file in the src/test/resources location. The content of this file is:



      <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
      xmlns="http://java.sun.com/xml/ns/persistence"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
      <persistence-unit name="mmTest" transaction-type="RESOURCE_LOCAL">
      <properties>
      <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
      </properties>
      </persistence-unit>
      </persistence>


      My Spring configuration looks like below:



      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="packagesToScan" value="com.sample"/>
      <property name="persistenceUnitName" value="mmTest"/>
      <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
      <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
      </bean>
      <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
      <property name="persistenceXmlLocation" value="classpath*:test-persistence.xml"/>
      </bean>
      <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
      <property name="showSql" value="true"/>
      </bean>
      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory"/>
      </bean>


      When I try to run a JUnit test with this context configuration set I get the exception:




      java.lang.IllegalArgumentException: Unknown entity:
      com.sample.SampleEntity




      Is it possible to have persistence.xml file for tests and configure Spring to scan for classes with @Entity? I do not want to use JPA specific class discovery like jar-file or listed classes. I want to use Spring to discover those classes.



      The strange thing is when I move test-persistence.xml to the location: /src/main/resources/META-INF then everything works correctly. However, I do not want to have test specific configuration inside src/main.



      Currently I solved the problem by specifying DataSource instead of persistenceUnitManager and persistenceUnitName.



      But I would like to know how to solve it without using DataSource.










      share|improve this question














      I have a test-persistence.xml file in the src/test/resources location. The content of this file is:



      <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
      xmlns="http://java.sun.com/xml/ns/persistence"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
      <persistence-unit name="mmTest" transaction-type="RESOURCE_LOCAL">
      <properties>
      <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
      </properties>
      </persistence-unit>
      </persistence>


      My Spring configuration looks like below:



      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="packagesToScan" value="com.sample"/>
      <property name="persistenceUnitName" value="mmTest"/>
      <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
      <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
      </bean>
      <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
      <property name="persistenceXmlLocation" value="classpath*:test-persistence.xml"/>
      </bean>
      <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
      <property name="showSql" value="true"/>
      </bean>
      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory"/>
      </bean>


      When I try to run a JUnit test with this context configuration set I get the exception:




      java.lang.IllegalArgumentException: Unknown entity:
      com.sample.SampleEntity




      Is it possible to have persistence.xml file for tests and configure Spring to scan for classes with @Entity? I do not want to use JPA specific class discovery like jar-file or listed classes. I want to use Spring to discover those classes.



      The strange thing is when I move test-persistence.xml to the location: /src/main/resources/META-INF then everything works correctly. However, I do not want to have test specific configuration inside src/main.



      Currently I solved the problem by specifying DataSource instead of persistenceUnitManager and persistenceUnitName.



      But I would like to know how to solve it without using DataSource.







      java spring hibernate jpa






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 20:37









      AdamAdam

      16819




      16819
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471712%2fhow-to-configure-spring-along-with-persistence-xml-to-scan-for-entities%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471712%2fhow-to-configure-spring-along-with-persistence-xml-to-scan-for-entities%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin