简体   繁体   English

Persistence.xml找不到hbm.xml

[英]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/ .我有一个用于集成测试的persistence.xml文件,因此位于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. 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:当我尝试执行测试(位于src/it/java/com.blabla/文件夹中)时,出现以下错误:

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.我认为问题与由于某些原因无法找到hbm.xml文件这一事实有关。 Because if I try to move:因为如果我尝试移动:

  • The hbm.xml file to the src/test/resources/META-INF/ folder, then everything works.hbm.xml文件放到src/test/resources/META-INF/文件夹中,然后一切正常。 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.但是我不喜欢这个解决方案,因为我希望能够为需要访问相同hbm.xml文件的生产添加另一个persistence.xml

  • The persistence.xml to the src/main/resources/META-INF/ again it works.src/main/resources/META-INF/persistence.xml再次起作用。 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.注意:我没有使用 Spring 引导。

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/ , persistence.xml用于在src/test/resources/META-INF/进行测试,
  • the hbm.xml mapping files at src/main/resources/META-INF/ , hbm.xml映射文件位于src/main/resources/META-INF/

Also in the persistence.xml for tests I added:同样在我添加的测试的persistence.xml中:

...
<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>
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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