简体   繁体   中英

Persistence.xml can't find hbm.xml

I have a persistence.xml file that I use for integration tests and for that reason is located in src/test/resources/META-INF/ . It looks like this:

<?xml version="1.0" encoding="utf-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="mysql-test"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.blabla.model.User</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <property name="javax.persistence.jdbc.user" value="mysql" />
            <property name="javax.persistence.jdbc.password" value="mysql" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:${db.port}/dbname" />
            <property
                name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
            <property name="javax.persistence.sql-load-script-source" value="data.sql" />

        </properties>
    </persistence-unit>
</persistence>

I also have a User.hbm.xml file located in com.blabla.model for hibernate xml mapping. When I try to execute the tests (which are in the src/it/java/com.blabla/ folder) I get the following error:

java.lang.IllegalArgumentException: Unknown entity: com.blabla.model.User
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:808)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
...

I think that the problem is related to the fact that for some reasons it's not able to find the hbm.xml file. Because if I try to move:

  • The hbm.xml file to the src/test/resources/META-INF/ folder, then everything works. However I don't like this solution because I want to be able to add another persistence.xml for production that needs to access to the same hbm.xml file.

  • The persistence.xml to the src/main/resources/META-INF/ again it works. But this is not good for me either because as I said I want to we able to add another persistence file for production.

Any ideas? Thanks.

Note : I'm not using Spring Boot.

At the end I found a configuration that seems to work as I wish. I have positioned:

  • persistence.xml for tests at src/test/resources/META-INF/ ,
  • the hbm.xml mapping files at src/main/resources/META-INF/ ,

Also in the persistence.xml for tests I added:

...
<persistence-unit name="mysql-test" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <mapping-file>/META-INF/User.hbm.xml</mapping-file>
    <class>com.blabla.model.User</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM