Skip to content

JPA without persistence.xml fails when all classpath items are jars [SPR-8832] #13474

Closed

Description

@spring-projects-issues

Artem Troitskiy opened SPR-8832 and commented

LocalContainerEntityManagerFactoryBean with setPackagesToScan() and without persistence.xml fails when all classpath items are jars with the following exception:

Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
        at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
        at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:426)

It doesn't fail if at least one classpath item is a folder.

It's caused by attempt to resolve a pattern pointing to the classpath root ("classpath:") that doesn't work for jars.


Affects: 3.1 RC1

Issue Links:

Referenced from: commits 62e5b9d

9 votes, 11 watchers

Activity

  1. spring-projects-issues commented on Jan 19, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Torsten Krah commented

    This does still apply to 3.1.0.RELEASE and i would rate this one at major, because the complete classpath scan feature is broken which does render the app unusable if packaged via jar file.

  2. spring-projects-issues commented on Feb 7, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    As of 3.1.1, we are using the containing jar as the persistence unit root URL for the default unit now, based on the location of the packages that we are scanning. We'll only fall back to the classpath root strategy if the jar determination doesn't work out.

    Juergen

  3. spring-projects-issues commented on Sep 5, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Cristi Vulpe commented

    It seems that the problem is still there with 3.1.1. Here are some details:
    If I use this:

    <bean id="myEntityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource"defaultDataSource" ref="myDataSource" />
      <property name="persistenceUnitName"defaultPersistenceUnitName" value="myDb" />
      <property name="mappingResources" value="META-INF/my-orm-mapping.xml" />    
      <property name="jpaVendorAdapter">
        <bean
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        </bean>
      </property>
      <property name="jpaProperties" ref="myOracleHibernatePoperties" />
    </bean>

    … I bump into the same error as originally reported…
    However, I managed to circumvent it by using this:

      <bean id="myPUManager"
        class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="defaultDataSource" ref="myDataSource" />
        <property name="defaultPersistenceUnitName" value="myDb" />
        <property name="mappingResources" value="META-INF/my-orm-mapping.xml" />
        <property name="defaultPersistenceUnitRootLocation"
          value="file://C:/tmp/jpa" />
      </bean>
    
      <bean id="myEntityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="myPUManager" />
        <property name="jpaVendorAdapter">
          <bean
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
          </bean>
        </property>
        <property name="jpaProperties" ref="myOracleHibernatePoperties" />
      </bean>

    Some details here:

    • We don’t want to use persistence.xml at all and neither package scanning. What we want to do is to get the resources from mapping files (the reason is that we want to stay away from annotations because we might have two different legacy databases having different table names and column names)
    • It seems that this line:
    <property name="defaultPersistenceUnitRootLocation" value="file://C:/tmp/jpa" />
    

    … fixes the situation (note that the C:/tmp/jpa is an empty folder).
    I can leave with this workaround but I am not sure whether this will have some negative consequences. Can somebody provide a quick explanation here?

    • Generally the approach we use is to stay away as much as we can from resources that reside in CLASSPATH (the reason is that we have many deployments and they all have different databases, folder structures etc.)

    Let me know if you need more details about this.

  4. added
    type: bug
    in: data
    on Jan 11, 2019
  5. added this to the 3.1.1 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: datatype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions