gpt4 book ai didi

hibernate - 非法参数异常 : Not an entity - distributed model definitions in hibernate/jpa and Maven

转载 作者:行者123 更新时间:2023-12-02 23:07:34 28 4
gpt4 key购买 nike

我们正在使用 Spring、Hibernate/JPA 和 Maven 开发一个 Web 应用程序。我们有几个定义不同实体的模块,所有模块都基于定义数据访问的核心模块和部署为 tomcat 上的 war 的 Web View 项目。

现在我在 persistence.xml 放置方面遇到了一些麻烦。实际上我根本不想使用它,因为我正在所有 java/test/resources 文件夹以及实际的 web 中存在的一些 datasource.xml 文件中进行所有 hibernate (即 jpa)设置项目。然而, hibernate 似乎无论如何都需要它,因为我不断收到

...
Caused by: java.lang.IllegalArgumentException: Not an entity: class ....model.Customer
at org.hibernate.ejb.metamodel.MetamodelImpl.entity(MetamodelImpl.java:160)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:58)
at org.springframework.data.jpa.repository.support.JpaPersistableEntityInformation.<init>(JpaPersistableEntityInformation.java:44)
at org.springframework.data.jpa.repository.utils.JpaClassUtils.getMetadata(JpaClassUtils.java:100)
at ...JpaProvider.init(JpaProvider.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:340)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:293)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:130)
... 58 more

适用于我的所有模型。(例如:)

@Entity
@Table(name="customer")
public class Customer extends AbstractPersistable<Long> {

所以我的包的设置如下:

src/main/java - contains the model classes
src/main/resources - contains application context
src/test/java - contains unit tests
src/test/resources - contains test context and datasource (JPA setup)

如果我将 META-INF/persistence.xml 放在 main/resources 中,一切都很好,这意味着 Maven 将构建项目,运行测试,其中 hibernate 将自动创建表。然而,由于我有许多模块项目在我的网络应用程序中组合在一起,我将得到一个

IllegalStateException: No single default persistence unit defined

我的 Web 项目设置是

src/main/resources/META-INF - containing the persistence.xml  being deployed to WEB-INF/classes 
src/main/webapp/WEB-INF - containing web.xml, faces-config, application-context, ... being deployed to /
src/main/java - containing controller classes being deployed to WEB-INF/classes

所以这几乎是提到的第一个设置 here应该没问题。

如果在我的“模型 jar ”中,我将 META-INF/persistence.xml 放入测试/资源中,我会得到上面提到的“不是实体”异常。

那么...如果我想在我的“模块”jar 中定义(和测试)实体对象并在我的“ View ” war 中使用它们,我该如何设置我的项目?

非常感谢!

编辑:找出原因

IllegalStateException: No single default persistence unit defined

看来,我需要在模型 jar 中将所有持久性单元命名为相同的名称。然而现在,我的网络应用程序提示一个 jar ,而另一个 jar 很好,而两者都有完全相同的设置。我只是无法摆脱“不是实体”异常(exception)。

编辑2:

奇怪的是,我现在有一个项目将构建和单元测试自己的模型,但在 Web 容器内使用时会提示这些模型不是实体,而具有 100% 完全相同设置的第二个项目将可以工作正好。让我觉得我的网络应用程序中有什么地方出了问题,但我就是看不出是什么。

有人遇到过这种情况吗?我在 Google 上找不到任何适合我的设置或提出适用解决方案的有用内容。

编辑3:

似乎是maven集成问题。在我的 Webapp pom 中,我有以下设置:

    <dependency>
<groupId>...</groupId>
<artifactId>view-jsf</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>


<dependency>
<groupId>...</groupId>
<artifactId>prototype</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>...</groupId>
<artifactId>security</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

</dependencies>

view-jsf 提供了 jsf 特定的东西,在这里并不重要。

原型(prototype)包含一些原型(prototype)模型(实体)定义,包括管理器和提供者。

security 包含安全模型定义,如帐户、角色等,以及仍待实现的 spring-security 特定内容;)

原型(prototype)和安全性都引用了核心项目,这些项目又定义了数据访问以及更多依赖项,例如 spring。

现在有趣的部分是:如果在我的 webapp pom 中我将安全性移到原型(prototype)之前,原型(prototype)实体将正常加载,并且安全实体将抛出 IllegalArgumentException 并说“不是实体”。如果我将原型(prototype)放在安全性之前,原型(prototype)实体将抛出异常。

因此,maven 似乎覆盖了两个项目中相同的内容,因此只有第二个项目能够成功加载。将进一步调查...

编辑4:进一步调查表明,问题在于一个持久性单元内有多个定义 JPA 实体的 jar (example) 。将针对该问题提出一个新问题,并在此处发布链接,因为该线程变得有点长且令人困惑。

最佳答案

将问题压缩成单独的帖子here

当两个 jars 在一个 persistence-unit 中定义了一个 persistence.xml 实体(显式或隐式),然后加载到一个 war 中时,就会导致异常(在我的例子中是由 Maven 提供的)。

解决方案似乎是在 Web 应用程序中只有一个 persistence.xml,并列出其中的所有实体(jar 单元测试除外,它们的资源中必须有一个 persistence.xml)。

仍然想知道是否有更动态的解决方案......

关于hibernate - 非法参数异常 : Not an entity - distributed model definitions in hibernate/jpa and Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7432070/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com